gpt4 book ai didi

MySQL EXPLAIN 输出解释

转载 作者:行者123 更新时间:2023-11-29 07:10:28 26 4
gpt4 key购买 nike

我有一个缓慢的 MySQl 查询,大约需要 15 秒才能运行。所以我做了一些调查,发现我可以使用 EXPLAIN 语句来查看瓶颈在哪里。所以我这样做了,但真的无法破译这些结果。

如果我不得不尝试一下,我会说第一行有问题,因为键有空值。但是,如果是这样,我不明白为什么 classType1 表在适当的列上建立索引。

有人可以解释一下问题出在哪里吗?非常感谢。

编辑:好的,我也添加了查询,希望它能为问题提供更多信息。不幸的是,我无法向您解释它在做什么,所以如果可以根据所提供的内容提供任何帮助,那就太好了。

   id, select_type, table, type, possible_keys, key, key_len, ref, rows, Extra
1, 'PRIMARY', 'classType1', 'system', 'PRIMARY', '', '', '', 1, 'Using temporary; Using filesort'
1, 'PRIMARY', 'user', 'const', 'PRIMARY', 'PRIMARY', '4', 'const', 1, 'Using index'
1, 'PRIMARY', 'class1', 'ref', 'IX_classificationType,IX_classificationValue,IX_classificationObjectType,IX_classificationObjectId', 'IX_classificationObjectId', '8', 'const', 3, 'Using where'
1, 'PRIMARY', 'classVal1', 'eq_ref', 'PRIMARY', 'PRIMARY', '4', 'ccms.class1.classificationValue', 1, 'Using where; Using index'
1, 'PRIMARY', 'class2', 'ref', 'IX_classificationType,IX_classificationValue,IX_classificationObjectType,IX_classificationObjectId', 'IX_classificationValue', '4', 'ccms.class1.classificationValue', 368, 'Using where'
1, 'PRIMARY', 'album', 'eq_ref', 'PRIMARY,IX_albumType,IX_albumIsDisabled,IX_albumIsActive,IX_albumCSI,IX_albumOwner,IX_albumPublishDate', 'PRIMARY', '4', 'ccms.class2.classificationObjectId', 1, 'Using where'
1, 'PRIMARY', 'profile', 'eq_ref', 'PRIMARY,IX_profileUserId', 'PRIMARY', '4', 'ccms.album.albumOwnerId', 1, 'Using where'
1, 'PRIMARY', 'albumUser', 'eq_ref', 'PRIMARY,IX_userIsAccountPublic', 'PRIMARY', '4', 'ccms.profile.profileUserId', 1, 'Using where'
1, 'PRIMARY', 'photo', 'eq_ref', 'PRIMARY,FK_photoAlbumId', 'PRIMARY', '8', 'ccms.album.albumCoverPhotoId', 1, 'Using where'
2, 'DEPENDENT SUBQUERY', 'class3', 'ref', 'IX_classificationObjectType,IX_classificationObjectId', 'IX_classificationObjectId', '8', 'ccms.class2.classificationObjectId', 1, 'Using where'
3, 'DEPENDENT SUBQUERY', 'class4', 'ref', 'IX_classificationType,IX_classificationValue,IX_classificationObjectType,IX_classificationObjectId', 'IX_classificationObjectId', '8', 'const', 3, 'Using where'

查询是...

SELECT   profileDisplayName,albumPublishDate,profileId,albumId,albumPath,albumName,albumCoverPhotoId,photoFilename,fnAlbumGetNudityClassification(albumId) AS albumNudityClassification,fnAlbumGetNumberOfPhotos(albumId,1,0) AS albumNumberOfPhotos,albumDescription,albumCSD,albumUSD,photoId,fnGetAlbumPhotoViewCount(albumId) AS albumNumberOfPhotoViews
FROM user
-- Join User Classifications
INNER JOIN classification class1
ON class1.classificationObjectId = user.userId AND class1.classificationObjectType = 1
INNER JOIN classificationType classType1
ON class1.classificationType = classType1.classificationTypeId
INNER JOIN classificationTypeValue classVal1
ON class1.classificationValue = classVal1.classificationTypeValueId
-- Join Album Classifications
INNER JOIN classification class2
ON class2.classificationObjectType = 3
AND class1.classificationType = class2.classificationType AND class1.classificationValue = class2.classificationValue
INNER JOIN album
ON album.albumId = class2.classificationObjectId
AND albumIsActive = 1
AND albumIsDisabled = 0
LEFT JOIN profile
ON albumOwnerId = profileId AND albumOwnerType = 0
LEFT JOIN user albumUser
ON albumUser.userId = profileUserId
AND albumUser.userIsAccountPublic = 1
LEFT JOIN photo
ON album.albumId = photo.photoAlbumId AND photo.photoId = album.albumCoverPhotoId
WHERE 0 =
(
SELECT COUNT(*)
FROM classification class3
WHERE class3.classificationObjectType = 3
AND class3.classificationObjectId = class2.classificationObjectId
AND NOT EXISTS
(
SELECT 1
FROM classification class4
WHERE class4.classificationObjectType = 1
AND class4.classificationObjectId = user.userId
AND class4.classificationType = class3.classificationType AND class4.classificationValue = class3.classificationValue
)
)
AND class1.classificationObjectId = 8
AND (albumPublishDate <= {ts '2011-01-28 20:48:39'} || albumCSI =

8)
AND album.albumType NOT IN (1)


AND fnAlbumGetNumberOfPhotos(albumId,1,0) > 0

AND albumUser.userIsAccountPublic IS NOT NULL
ORDER BY albumPublishDate DESC
LIMIT 0, 15

最佳答案

在没有看到实际结构或查询的情况下,我会寻找两件事...

我知道你说过它们是......但是......确保所有适当的字段都已编入索引

示例:您在“事件”字段上有一个索引(仅过滤出事件记录),在 id_classType1 上有另一个索引(比如说主键索引)...除非您执行 unique index on "id_classType1, active", 类似这样的查询:

SELECT * FROM classType1 WHERE id_classType1 IN (1,2,3) AND active = 1

... 需要合并这些索引或单独查找它们。但是,如果您在 id_classType1 和 active 上都有一个索引(并且该索引是 UNIQUE 类型),SQL 将使用它并更快地找到组合。


其次,您的 EXPLAIN 语句中似乎有依赖子查询,这会大大降低您的查询速度...请查看此处的可能解决方法:http://forums.mysql.com/read.php?115,128477,128477

我的第一个尝试是用 JOINs 替换那些子查询,然后可能尝试通过完全删除它们(如果可能)或为 单独查询 来进一步优化它那些子查询


编辑

这个查询比我见过的任何其他查询都复杂,所以将这些作为某种程度上有限的提示:

  • 尝试删除子查询(只需放置任何您知道可以工作并暂时给出结果的内容)
  • 我在查询中看到很多INNER JOINS,这可能会很慢,因为它们需要连接两个表中的所有行(来源:http://dev.mysql.com/doc/refman/5.0/en/join.html)——也许有办法以某种方式替换它们?
  • 另外 - 这是我过去记得的事情(可能不是真的或相关的) - WHERE-like statements 不应该在 WHERE 子句中,而不是 JOIN 子句中吗?例如,我会将第一个 JOIN 中的以下内容放入 WHERE 部分:class1.classificationObjectType = 1

这就是全部 - 还有一个问题:这些表有多少行?不需要确切的数字,只是想看看大约。查询运行了多少条记录,因为它需要很长时间

关于MySQL EXPLAIN 输出解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4832221/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com