简体   繁体   English

MySql InnoDb 中的索引使用

[英]Index Usage in MySql InnoDb

I wonder that the index usage in Mysql InnoDb Engine.我想知道 Mysql InnoDb 引擎中的索引使用情况。

below is simplified example.下面是简化的例子。

  1. fields字段
    a, b, c, d a, b, c, d

  2. indexes指标

  • a(pk)一个(峰)
  • (a, b, d) (composite index) (a, b, d) (综合指数)
  1. Query询问
    Select... Where a = 3 And b >= 5 And c = 4 Order by d Limit 10; Select... 其中 a = 3 And b >= 5 And c = 4 Order by d Limit 10;

Q.1 in this case, how long index is used? Q.1在这种情况下,索引使用多长时间? (a, b?) (a, b, d?) (a, b?) (a, b, d?)

Q.2 How can i optimise the index for this case? Q.2如何针对这种情况优化索引?

Q.3 Is it okay to include a primary key (clustered index) in secondary indexes? Q.3可以在二级索引中包含主键(聚集索引)吗? Or is it better to compare the primary key field at the end of the WHERE clause?还是在 WHERE 子句末尾比较主键字段更好?

Thanks.谢谢。

Q1 Q1

Run your horses.跑你的马。 Probably no index will be used for this query, but you can get the execution plan and verify.该查询可能不会使用索引,但您可以获取执行计划并进行验证。

Q2 Q2

The following index can improve performance.以下索引可以提高性能。 Again, verify the execution plan:再次验证执行计划:

create index ix1 on t (a, c, b);

You can even try the "covering index":您甚至可以尝试“覆盖索引”:

create index ix1 on t (a, c, b, d);

For little bit of extra performance (at the cost of a heavier index).为了一点额外的性能(以更重的索引为代价)。

Q3 Q3

In InnoDB the PK is always included in all secondary indexes.在 InnoDB 中,PK 始终包含在所有二级索引中。 That's not the case with MyISAM, but this is not your use case. MyISAM 不是这种情况,但这不是您的用例。

It is rare that having the PK at the beginning of a secondary index is of any use.在二级索引的开头使用 PK很少有任何用处。 The one case where it might be useful is if all the columns needed by the entire SELECT are included in index.可能有用的一种情况是,如果整个SELECT所需的所有列都包含在索引中。 Your example was not that case.你的例子不是那种情况。 (Cf: "covering index") (参见:《覆盖指数》)

The order of clauses AND'd together in WHERE does not matter. WHERE中子句AND'd的顺序无关紧要 The order of columns in an INDEX does matter. INDEX列的顺序很重要。

Aside from "covering", no index will completely optimize Where a = 3 And b >= 5 And c = 4 Order by d Limit 10 .除了“覆盖”,没有索引会完全优化Where a = 3 And b >= 5 And c = 4 Order by d Limit 10 See Index Cookbook for an algorithm for finding the optimal index.请参阅Index Cookbook以了解用于查找最佳索引的算法。 As already suggested: (a,c,b) or (c,a,b) .正如已经建议的: (a,c,b)(c,a,b)

(a, c, b, d) will help only if it is "covering". (a, c, b, d)只有在“覆盖”时才会有帮助。 (What is in SELECT... ?? SELECT... ??

(a, b, d) may run faster, but it is risky. (a, b, d)可能运行得更快,但是有风险。 If the 10 desired rows are found soon, it will be fast.如果很快就找到所需的 10 行,它会很快。 But if there are not a full 10 rows in the table, it will be a slow table scan.但是如果表中没有完整的 10 行,这将是一个缓慢的表扫描。

"Using filesort" is a consequence of ORDER BY and/or GROUP BY . “使用文件排序”是ORDER BY和/或GROUP BY的结果。 An index may have been useful, but the query could not have been completed using only the index.索引可能有用,但使用索引无法完成查询。

Yes, the PK column(s) are always included in an InnoDB secondary key.是的,PK 列总是包含在 InnoDB 辅助键中。 But they will be tacked onto the end.但它们将被附加到最后。 Keep in mind that the order in the INDEX is important, and, as my cookbook indicates, = columns need to be listed first in the index definition.请记住, INDEX中的顺序很重要,并且正如我的食谱所示, =列需要在索引定义中首先列出。

"how can I cover WHERE and ORDER BY clause by using an index?" “如何使用索引覆盖 WHERE 和 ORDER BY 子句?” -- One more thing to finish answering that. - 完成回答的另一件事。 Although a "covering" index may "cover" both clauses, it can't prevent the "filesort" because of the mixture of a "range" test ( < ) and ORDER BY .尽管“覆盖”索引可以“覆盖”这两个子句,但它不能阻止“文件排序”,因为“范围”测试 ( < ) 和ORDER BY的混合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM