您的位置:首页 > 数据库 > MySQL

MySQL的COUNT()函数利用索引进行计算

2017-03-13 19:06 393 查看
以前写过一篇《select count(*) 和 select count(1)有什么区别?  》,参见: http://blog.163.com/li_hx/blog/static/18399141320146961258398/
现执行如下SQL并进一步分析如下:
----------------------------------------------
mysql> CREATE TABLE t_count (id INT PRIMARY KEY, uk INT, k1 INT, k2_1 INT, k2_2 INT, col INT, UNIQUE KEY (uk), KEY k1(k1), KEY k2 (k2_1, k2_2
Query OK, 0 rows affected (0.28 sec)

mysql> INSERT INTO t_count values (1,1,1,1,1,1),(2,2,2,2,2,2),(3,3,3,3,3,3),(4,4,4,4,4,4),(5,5,5,5,5,5),(6,6,6,6,6,6),(7,NULL,NULL,NULL,NULL,
Query OK, 7 rows affected (0.03 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> SELECT COUNT(*),COUNT(1),COUNT(id),COUNT(uk),COUNT(k1),COUNT(k2_1),COUNT(k2_2),COUNT(col) FROM t_count;
+----------+----------+-----------+-----------+-----------+-------------+-------------+------------+
| COUNT(*) | COUNT(1) | COUNT(id) | COUNT(uk) | COUNT(k1) | COUNT(k2_1) | COUNT(k2_2) | COUNT(col) |
+----------+----------+-----------+-----------+-----------+-------------+-------------+------------+
|        7 |        7 |         7 |         6 |         6 |           6 |           6 |          6 |
+----------+----------+-----------+-----------+-----------+-------------+-------------+------------+
1 row in set (0.01 sec)

MySQL---聚集函数的优化详解http://blog.163.com/li_hx/blog/static/183991413201523153748830/?COLLCC=858502883&COLLCC=3257644771&COLLCC=826142342&COLLCC=306048646&
mysql> EXPLAIN SELECT COUNT(uk) FROM t_count;
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type  | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | t_count | NULL       |    | 5       | NULL |    8 |   100.00 | Using index |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> EXPLAIN SELECT COUNT(k1) FROM t_count;
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type  | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | t_count | NULL       |    | 5       | NULL |    8 |   100.00 | Using index |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> EXPLAIN SELECT COUNT(k2_1) FROM t_count;
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type  | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | t_count | NULL       | index | NULL          | k2   | 10      | NULL |    8 |   100.00 | Using index |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> EXPLAIN SELECT COUNT(k2_2) FROM t_count;
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table   | partitions | type  | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | t_count | NULL       | index | NULL          | k2   | 10      | NULL |    8 |   100.00 | Using index |
+----+-------------+---------+------------+-------+---------------+------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

mysql> EXPLAIN SELECT COUNT(col) FROM t_count;
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table   | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
|  1 | SIMPLE      | t_count | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    8 |   100.00 | NULL  |
+----+-------------+---------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: