mysql - 综合索引中的单独连接子句

标签 mysql composite-index

有一个综合索引对这样的事情有好处吗:

SELECT * FROM a INNER JOIN b ON(a.id=b.id)
                INNER JOIN c ON(a.bar=c.id)
                INNER JOIN d ON(a.foo=d.id)

索引将是:

(a.id, a.bar, a.foo)

最佳答案

只有索引的前沿会被使用 (a.id),所以只有 INNER JOINb 会从中受益索引...因此索引中的附加列(a.bara.foo)对发布的示例查询没有好处。

From the MySql documentation :

MySQL cannot use the index to perform lookups if the columns do not form a leftmost prefix of the index. Suppose that you have the SELECT statements shown here:

SELECT * 
FROM tbl_name 
WHERE col1=val1; 

SELECT * 
FROM tbl_name 
WHERE col1=val1 AND col2=val2;

SELECT * 
FROM tbl_name 
WHERE col2=val2; 

SELECT * 
FROM tbl_name 
WHERE col2=val2 AND col3=val3; 

If an index exists on (col1, col2, col3), only the first two queries use the index. The third and fourth queries do involve indexed columns, but (col2) and (col2, col3) are not leftmost prefixes of (col1, col2, col3).

关于mysql - 综合索引中的单独连接子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13165352/

相关文章:

MySQL 多个左连接在一张表上

php - Ubuntu/PHP/MySQL : mysql-functions not working?

java - 如何从 sql 查询中编写 hibernate 模板查询?

mysql查询INSERT两张表

mysql - 对使用索引时查看 50 万行的 mysql 感到困惑

mysql - MySql 中的复合索引可以双向工作吗?

mysql - "Using index",复合索引 : A=, B=, C<=

php - CodeIgniter - 从 n-2-n 关系表中获取并显示数据