Heim >Datenbank >MySQL-Tutorial >mysqlcount(*)会选哪个索引?_MySQL
今天在查询一个表行数的时候,发现count(1)和count(*)执行效率居然是一样的。这跟Oracle还是有区别的。遂查看两种方式的执行计划:
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)
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)
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)
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)这里一看l_shipDATE并不是基数最小的呀,殊不知这个统计信息是不准确的。我们用sql看一下。
mysql> select count(distinct(l_shipDATE)) from lineitem; +-----------------------------+ | count(distinct(l_shipDATE)) | +-----------------------------+ | 2526 | +-----------------------------+ 1 row in set (0.01 sec)那么比他小的那些列呢?
mysql> select count(distinct(l_receiptDATE)) from lineitem; +--------------------------------+ | count(distinct(l_receiptDATE)) | +--------------------------------+ | 2554 | +--------------------------------+ 1 row in set (0.01 sec)