mysql - 为什么对行进行排序会减少解释中的行数?

标签 mysql sql-order-by explain mysql5 mysql-5.5

我有一个大约有两三百万行的表......

mysql> select count(*) from tbl;
+----------+
| count(*) |
+----------+
|  2615889 |
+----------+
1 row in set (1.23 sec)

mysql> show indexes from tbl;
+-------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name          | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| tbl   |          0 | PRIMARY           |            1 | tbl_id      | A         |     2284627 |     NULL | NULL   |      | BTREE      |         |               |
| ...
| tbl   |          1 | tbl_fld           |            1 | fld         | A         |     2284627 |     NULL | NULL   | YES  | BTREE      |         |               |
+-------+------------+-------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
6 rows in set (0.30 sec)

...对于下面的查询,如果我添加 order by 子句,我似乎会做得更好(即,我最终使用索引)...

mysql> explain select * from tbl
    -> where fld in (select fld from tbl group by fld having count(*)>1)
    -> limit 1000;
+----+--------------------+-------+-------+---------------+---------+---------+------+---------+-------------+
| id | select_type        | table | type  | possible_keys | key     | key_len | ref  | rows    | Extra       |
+----+--------------------+-------+-------+---------------+---------+---------+------+---------+-------------+
|  1 | PRIMARY            | tbl   | ALL   | NULL          | NULL    | NULL    | NULL | 2328333 | Using where |
|  2 | DEPENDENT SUBQUERY | tbl   | index | NULL          | tbl_fld | 15      | NULL |       1 | Using index |
+----+--------------------+-------+-------+---------------+---------+---------+------+---------+-------------+
2 rows in set (0.00 sec)

mysql> explain select * from tbl
    -> where fld in (select fld from tbl group by fld having count(*)>1)
    -> order by fld limit 1000;
+----+--------------------+-------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type        | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+--------------------+-------+-------+---------------+---------+---------+------+------+-------------+
|  1 | PRIMARY            | tbl   | index | NULL          | tbl_fld | 15      | NULL | 1000 | Using where |
|  2 | DEPENDENT SUBQUERY | tbl   | index | NULL          | tbl_fld | 15      | NULL |    1 | Using index |
+----+--------------------+-------+-------+---------------+---------+---------+------+------+-------------+
2 rows in set (0.00 sec)

……这是为什么?

最佳答案

乍一看,它似乎在第二个查询中使用了一个键,这是一件好事,这样可以防止对表进行完全扫描。

关于mysql - 为什么对行进行排序会减少解释中的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32210783/

相关文章:

php - MYSQL IF 语句按类别重新排序

mysql - 使用 GROUP BY、ORDER BY 和 GROUP_CONCAT 进行索引

Mysql 解释显示查询正在使用索引,而根据 Mysql 文档,它不应该使用索引

sql - Explain Analyze 上嵌套反循环的成本

php - MySql 查询两个表

php - 将数据库从一台服务器转移到另一台服务器

php - 如何在一行中显示签到表

php - 带有 PHP 和 MySql 的 Docker - $servername 错误

MySQL ORDER BY FIELD with %

sql - 重用计算的选择值