mysql> EXPLAIN SELECT fldjobitemid, fldstatus, tblbulkreportjobitems.fldparticipantid, CONCAT(fldFirstName, ' ', fldLastName) as full_name FROM tblbulkreportjobitems FORCE INDEX (fldparticipantid) JOIN tblparticipant ON tblparticipant.fldParticipantId = tblbulkreportjobitems.fldparticipantid WHERE fldjobid = 9 ORDER BY fldjobitemid
    -> ;
+----+-------------+-----------------------+--------+--------------------------+---------+---------+------------------------------------------------------+------+-----------------------------+
| id | select_type | table                 | type   | possible_keys            | key     | key_len | ref                                                  | rows | Extra                       |
+----+-------------+-----------------------+--------+--------------------------+---------+---------+------------------------------------------------------+------+-----------------------------+
|  1 | SIMPLE      | tblbulkreportjobitems | ALL    | fldparticipantid         | NULL    | NULL    | NULL                                                 |  869 | Using where; Using filesort |
|  1 | SIMPLE      | tblparticipant        | eq_ref | PRIMARY,fldParticipantId | PRIMARY | 4       | medicus_devel.tblbulkreportjobitems.fldparticipantid |    1 | Using where                 |
+----+-------------+-----------------------+--------+--------------------------+---------+---------+------------------------------------------------------+------+-----------------------------+
2 rows in set (0.05 sec)

为什么它仍然使用文件排序?

最佳答案

如果键的数目是表中行数的5%,MySQL查询优化器将始终否决您对索引的选择。
请运行以下查询:
从tblbulkreportjobitems中选择COUNT(1);
我想应该是869(根据解释计划)
从tblbulkreportjobitems中选择COUNT(1),其中fldjobid=9;
按fldjobid和ROLLUP从tblbulkreportjobitems组中选择COUNT(1)、fldjobid;
按fldjobid和ROLLUP从tblbulkreportjobitems组中选择COUNT(1)、fldjobid;
您将看到哪些行出现在总行的5%以上。在这种情况下,MySQL查询优化器将选择使用不平衡索引的完整表扫描。
如果在WHERE子句中有fldpartipantid,则会得到不同的结果。

10-05 21:32