count(*) 和 count(other) 的区别

探讨了MySQL InnoDB引擎中COUNT(A)与COUNT(*)查询优化差异,特别是在建立索引后的表现不同。通过具体案例分析,发现当数据量超过一定阈值时,COUNT(A)会从使用索引变为全表扫描。

后来


--------------------------------------------------------------------------------------------------------------------------

MySQL innodb table当中,select count(A) from table where B ...  , 我在B上建立了索引

当记录数小于173640 (173640 * 4 B = 694560 B,  我这里的B 是 4 bytes) 时,count(A) 和 count(*) 都走B的索引 

大于时,count(*) 都走B的索引 , count(A) 走全表扫描,实际上count(*)和count(B) 在此等效


mysql> explain select count(A) from t1                 where B        > "2001-11-2" and B        < "2002-2-8";
+----+-------------+-------------------+-------+---------------+----------+---------+------+--------+-------------+
| id | select_type | table             | type  | possible_keys | key      | key_len | ref  | rows   | Extra       |
+----+-------------+-------------------+-------+---------------+----------+---------+------+--------+-------------+
|  1 | SIMPLE      | t1                | range | B             | B        | 8       | NULL | 172918 | Using where |
+----+-------------+-------------------+-------+---------------+----------+---------+------+--------+-------------+
1 row in set (0.00 sec)

range, key ==> 显然走了索引


mysql> explain select count(A) from t1                 where B        > "2001-11-2" and B        < "2002-2-9";
+----+-------------+-------------------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table             | type | possible_keys | key  | key_len | ref  | rows    | Extra       |
+----+-------------+-------------------+------+---------------+------+---------+------+---------+-------------+
|  1 | SIMPLE      | t1                | ALL  | B             | NULL | NULL    | NULL | 1000122 | Using where |
+----+-------------+-------------------+------+---------------+------+---------+------+---------+-------------+
1 row in set (0.00 sec)

all ==> 显然走了索引 , 并且slow query 当中的Rows_examined 却是是 全表的行数,汗;select的时间也确实慢

--------------------------------------


mysql> explain select count(*) from t1                 where B        > "2001-11-2" and   B        < "2002-2-9";
+----+-------------+-------------------+-------+---------------+----------+---------+------+--------+--------------------------+
| id | select_type | table             | type  | possible_keys | key      | key_len | ref  | rows   | Extra                    |
+----+-------------+-------------------+-------+---------------+----------+---------+------+--------+--------------------------+
|  1 | SIMPLE      | t1                | range | B             | B        | 8       | NULL | 173640 | Using where; Using index |
+----+-------------+-------------------+-------+---------------+----------+---------+------+--------+--------------------------+
1 row in set (0.00 sec)




mysql> explain select count(*) from t1                 where B        > "2001-11-2" and B        < "2002-2-29";
+----+-------------+-------------------+-------+---------------+----------+---------+------+--------+--------------------------+
| id | select_type | table             | type  | possible_keys | key      | key_len | ref  | rows   | Extra                    |
+----+-------------+-------------------+-------+---------------+----------+---------+------+--------+--------------------------+
|  1 | SIMPLE      | t1                | range | B             | B        | 8       | NULL | 199870 | Using where; Using index |
+----+-------------+-------------------+-------+---------------+----------+---------+------+--------+--------------------------+

1 row in set (0.00 sec)


两个range, key ==> 显然都走了索引



结论就是在这种情况下,不要自作聪明写count(A),一时浅见,以后在研究出了什么问题


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值