Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Mysql不在具有索引列的内部联接表上使用索引?_Mysql_Join_Database Performance - Fatal编程技术网

为什么Mysql不在具有索引列的内部联接表上使用索引?

为什么Mysql不在具有索引列的内部联接表上使用索引?,mysql,join,database-performance,Mysql,Join,Database Performance,首先,我有以下表格结构 Table Document ## DocID ## ##DocName ## 1 Doc1 2 Doc2 3 Doc3 Table FolderTree ## FolderID ## ## MemberDocID ## 1 1 1 2

首先,我有以下表格结构

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上没有索引,因此无法优化对不同值的搜索。

@bernie此处的根本原因与链接的可能副本中的根本原因不同。虽然它们听起来像类似的问题,但事实并非如此。我删除了DISTINCT关键字,并在DocName上添加了索引。它现在正在使用索引作为键,但要检查的行具有相同的值。是否有任何方法可以进一步优化我的查询,或者是否有任何需要添加的内容可以最好地优化我的查询。谢谢,如果您没有查询DISTINCT,您不需要DocName上的索引。文档表中有多少行?实际上,通过交换联接中表的顺序,您可能会获得更好的性能,因为您正在从folderTree表中筛选列。因此,这可能是您首先阅读的表。谢谢,我将查看此表。