首先我有以下表结构。
Table Document
## DocID ## ##DocName ##
1 Doc1
2 Doc2
3 Doc3
Table FolderTree
## FolderID ## ## MemberDocID ##
1 1
1 2
1 3
我有 DocID、FolderID 和 MemberDocID 索引
我有以下查询。
SELECT DISTINCT d.* FROM Document d inner join FolderTree f on (d.DocID = f.MemberDocID ) where f.FolderID = 1
解释输出:
| select type | table | type | possible_keys | key | rows | extra |
simple d All PRIMARY NULL 83168 Using temporary
simple f ref MemberDocID MemberDocID 11 Using index
我的问题是,为什么 mysql 对我在 DocID 上有索引的表 d 使用表扫描?
最佳答案
这是因为您在文档表中的所有列上选择了 DISTINCT。 DocName 上没有索引,因此无法优化对不同值的搜索。
关于mysql - 为什么Mysql不在带有索引列的INNER JOIN表上使用索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15033655/