假设我有如下查询:
SELECT message.mid
FROM message
WHERE message.mid <= 100
据我所知,如果将查询更改为以下内容,它将执行得更快,因为列没有展开。
SELECT COUNT(message.mid)
FROM message
WHERE message.mid <= 100
但是下面的查询会有同样的好处吗?它还会那么快吗?
SELECT COUNT(*)
FROM (
SELECT message.mid,
message.something,
message.something2,
message.something3,
FROM message
WHERE message.mid <= 100
) AS A
最佳答案
We can ask MySQL what it will do .这是 5.7。
mysql> explain SELECT COUNT(*) FROM ( SELECT message.mid FROM message WHERE message.mid <= 100
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
| 1 | SIMPLE | message | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 100 | 100.00 | Using where; Using index |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> explain SELECT count(message.mid) FROM message WHERE message.mid <= 100;
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
| 1 | SIMPLE | message | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 100 | 100.00 | Using where; Using index |
+----+-------------+---------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)
看起来是一样的。 MySQL 已经优化掉了子查询。
这是当 MySQL 未优化子查询时我们会看到的示例。
mysql> explain SELECT * FROM ( SELECT message.mid FROM message where mid < 100 group by mid) m;
+----+-------------+------------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+------------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
| 1 | PRIMARY | <derived2> | NULL | ALL | NULL | NULL | NULL | NULL | 99 | 100.00 | NULL |
| 2 | DERIVED | message | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 99 | 100.00 | Using where; Using index |
+----+-------------+------------+------------+-------+---------------+---------+---------+------+------+----------+--------------------------+
"Optimizing Derived Tables and View References"有此优化如何工作的示例。
Example 1:
SELECT * FROM (SELECT * FROM t1) AS derived_t1;
With merging, that query is executed similar to:
SELECT * FROM t1;
该页面概述了 MySQL 用来提高子查询效率的许多其他优化技巧。
关于mysql - SQL - 嵌套查询的计数仍然很快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53286825/