mysql - 为什么Mysql不在带有索引列的INNER JOIN表上使用索引?

标签 mysql join database-performance

首先我有以下表结构。

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/

相关文章:

sql - 在类似语句上加入 SQL Server 表

MySQL 性能 : letting a UNIQUE field generate an error or manually checking it

mysql - rails : How to split write/read query across master/slave database

php - PDO 准备语句。选择一切

mysql - 替换 MySQL 字符串中的第一个字符

mysql - 使用子查询通过 JOIN 选择行

c# - 在内存集合和 EntityFramework 之间加入

Mysql 的 Express 查询返回 null

mysql - 无法添加外键约束 - MySQL ERROR 1215 (HY000)

postgresql - PostgreSQL 计数查询优化