如何解释 MySQL EXPLAIN 的输出?

2024-01-22

我想选择栏目内容text from entrytable.

EXPLAIN SELECT text
FROM entrytable
WHERE user = 'username' &&
`status` = '1' && (
    `status_spam_user` = 'no_spam'
    || (
        `status_spam_user` = 'neutral' &&
        `status_spam_system` = 'neutral'
    )
)
ORDER BY datum DESC
LIMIT 6430 , 10

该表有三个索引:

  • 索引用户(用户)
  • 索引数据(数据)
  • index_status_mit_spam(状态、status_spam_user、status_spam_system)

解释结果是:

id  select_type     table       type    possible_keys                       key         key_len     ref     rows    Extra
1   SIMPLE          entrytable  ref     index_user,index_status_mit_spam    index_user  32          const   7800    Using where; Using filesort
  • Is possible_keysMySQL 可能想要使用的索引和keysMySQL 实际使用的索引?
  • 为什么是指数index_status_mit_spam不曾用过?在查询中,列的顺序与索引中的顺序相同,...
  • 为什么是指数index_datum不用于ORDER BY?
  • 如何优化我的表索引或查询? (上面的查询最多需要 3 秒,表中有大约一百万个条目)

回答您的问题:

  • Is possible_keysMySQL 可能想要使用的索引和keysMySQL实际使用的索引是什么?是的,这是正确的。

  • 为什么是指数index_status_mit_spam不曾用过?在查询中,列的顺序与索引中的顺序相同。 SQL使用表索引的统计信息以确定使用哪个索引。这select 语句中的字段顺序没有影响使用哪个索引。有关索引的统计信息包括索引的唯一性等信息。可能会使用更多唯一索引。在这里阅读更多相关信息:http://dev.mysql.com/doc/innodb/1.1/en/innodb-other-changes-statistics-estimation.html http://dev.mysql.com/doc/innodb/1.1/en/innodb-other-changes-statistics-estimation.html或在这里:http://dev.mysql.com/doc/refman/5.0/en//myisam-index-statistics.html http://dev.mysql.com/doc/refman/5.0/en//myisam-index-statistics.html。这些因素决定了MySQL将如何选择一个索引使用。它只会使用一个索引.

  • 为什么是指数index_datum不用于ORDER BY? MySQL 在查询期间只会使用一个索引,而不是两个,因为使用第二个索引会进一步减慢查询速度。读取索引不是读取表。这关系到查询的运行效率。以下是可以解释一些概念的答案:https://dba.stackexchange.com/questions/18528/performance-difference- Between-clustered-and-non-clustered-index/18531#18531 https://dba.stackexchange.com/questions/18528/performance-difference-between-clustered-and-non-clustered-index/18531#18531 Or 向 MySQL 查询添加 limit 子句会显着减慢速度 https://stackoverflow.com/questions/13846691/adding-limit-clause-to-mysql-query-slows-it-down-dramatically/13874086#13874086 Or MySQL 索引以及何时对它们进行分组 https://stackoverflow.com/questions/10328343/mysql-indexes-and-when-to-group-them/10329106#10329106。这些答案有很多细节,将帮助您了解 MySQL 索引。

  • 如何优化我的表索引或查询? (上面的查询最多需要 3 秒,表中有大约一百万个条目)。这里有一个文件排序可能会减慢你的速度。可能是表的索引太多,MySQL 选择了错误的索引。

您需要了解索引会加快表的读取速度并减慢写入速度。因此,仅仅添加索引并不总是一个好主意。上述答案和指示应该可以帮助您获得扎实的理解。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何解释 MySQL EXPLAIN 的输出? 的相关文章

随机推荐