标签:分享 技术 统计 tom line nal http 证明 span
转自 http://www.2cto.com/database/201508/433975.html
今天在查询一个表行数的时候,发现count(1)和count(*)执行效率居然是一样的。这跟Oracle还是有区别的。遂查看两种方式的执行计划:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
mysql> select count (1) from customer; + ----------+ | count (1) | + ----------+ | 150000 | + ----------+ 1 row in set (0.03 sec) mysql> flush tables; Query OK, 0 rows affected (0.00 sec) mysql> select count (*) from customer; + ----------+ | count (*) | + ----------+ | 150000 | + ----------+ 1 row in set (0.03 sec) |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
mysql> explain select count (1) from customer; + ----+-------------+----------+-------+---------------+---------------+---------+------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | + ----+-------------+----------+-------+---------------+---------------+---------+------+--------+-------------+ | 1 | SIMPLE | customer | index | NULL | i_c_nationkey | 5 | NULL | 151191 | Using index | + ----+-------------+----------+-------+---------------+---------------+---------+------+--------+-------------+ 1 row in set (0.00 sec) mysql> explain select count (*) from customer; + ----+-------------+----------+-------+---------------+---------------+---------+------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | + ----+-------------+----------+-------+---------------+---------------+---------+------+--------+-------------+ | 1 | SIMPLE | customer | index | NULL | i_c_nationkey | 5 | NULL | 151191 | Using index | + ----+-------------+----------+-------+---------------+---------------+---------+------+--------+-------------+ 1 row in set (0.00 sec) mysql> show index from customer; + ----------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | + ----------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | customer | 0 | PRIMARY | 1 | c_custkey | A | 150525 | NULL | NULL | | BTREE | | | | customer | 1 | i_c_nationkey | 1 | c_nationkey | A | 47 | NULL | NULL | YES | BTREE | | | + ----------+------------+---------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 2 rows in set (0.08 sec) |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
mysql> select count (*) from customer force index ( PRIMARY ); + ----------+ | count (*) | + ----------+ | 150000 | + ----------+ 1 row in set (0.68 sec) mysql> explain select count (*) from customer force index ( PRIMARY ); + ----+-------------+----------+-------+---------------+---------+---------+------+--------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | + ----+-------------+----------+-------+---------------+---------+---------+------+--------+-------------+ | 1 | SIMPLE | customer | index | NULL | PRIMARY | 4 | NULL | 150525 | Using index | + ----+-------------+----------+-------+---------------+---------+---------+------+--------+-------------+ 1 row in set (0.00 sec) |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
mysql> explain select count (*) from lineitem; + ----+-------------+----------+-------+---------------+--------------+---------+------+---------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | + ----+-------------+----------+-------+---------------+--------------+---------+------+---------+-------------+ | 1 | SIMPLE | lineitem | index | NULL | i_l_shipdate | 4 | NULL | 6008735 | Using index | + ----+-------------+----------+-------+---------------+--------------+---------+------+---------+-------------+ 1 row in set (0.00 sec) mysql> show index from lineitem; + ----------+------------+-----------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | + ----------+------------+-----------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ | lineitem | 0 | PRIMARY | 1 | l_orderkey | A | 2997339 | NULL | NULL | | BTREE | | | | lineitem | 0 | PRIMARY | 2 | l_linenumber | A | 5994679 | NULL | NULL | | BTREE | | | | lineitem | 1 | i_l_shipdate | 1 | l_shipDATE | A | 5208 | NULL | NULL | YES | BTREE | | | | lineitem | 1 | i_l_suppkey_partkey | 1 | l_partkey | A | 428191 | NULL | NULL | YES | BTREE | | | | lineitem | 1 | i_l_suppkey_partkey | 2 | l_suppkey | A | 1998226 | NULL | NULL | YES | BTREE | | | | lineitem | 1 | i_l_partkey | 1 | l_partkey | A | 461129 | NULL | NULL | YES | BTREE | | | | lineitem | 1 | i_l_suppkey | 1 | l_suppkey | A | 19213 | NULL | NULL | YES | BTREE | | | | lineitem | 1 | i_l_receiptdate | 1 | l_receiptDATE | A | 17 | NULL | NULL | YES | BTREE | | | | lineitem | 1 | i_l_orderkey | 1 | l_orderkey | A | 2997339 | NULL | NULL | | BTREE | | | | lineitem | 1 | i_l_orderkey_quantity | 1 | l_orderkey | A | 1998226 | NULL | NULL | | BTREE | | | | lineitem | 1 | i_l_orderkey_quantity | 2 | l_quantity | A | 5994679 | NULL | NULL | YES | BTREE | | | | lineitem | 1 | i_l_commitdate | 1 | l_commitDATE | A | 7836 | NULL | NULL | YES | BTREE | | | + ----------+------------+-----------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+ 12 rows in set (0.96 sec) |
1
2
3
4
5
6
7
|
mysql> select count ( distinct (l_shipDATE)) from lineitem; + -----------------------------+ | count ( distinct (l_shipDATE)) | + -----------------------------+ | 2526 | + -----------------------------+ 1 row in set (0.01 sec) |
1
2
3
4
5
6
7
|
mysql> select count ( distinct (l_receiptDATE)) from lineitem; + --------------------------------+ | count ( distinct (l_receiptDATE)) | + --------------------------------+ | 2554 | + --------------------------------+ 1 row in set (0.01 sec) |
标签:分享 技术 统计 tom line nal http 证明 span
原文地址:http://www.cnblogs.com/qcfeng/p/6222590.html