mysql中select的count星和count1有区别么

http://my.oschina.net/zimingforever/blog/76457


之前一直都任务select count(1) from tab的效率要高于count(*),今天看了下执行计划才发现原来一直都是一个误解,mysql的优化器会自动转换。

直接上例子

?
1
explain SELECT count (*) FROM `employees`;

?
1
1   SIMPLE  employees   index       index_firstName 44      299809  Using index

执行到这里我发现本机的mysql不显示wraning,我这里直接贴一个同事的测试结果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql> explain extended select count(*) from t1;
+----+-------------+-------+-------+---------------+------+---------+------+------+----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+-------+---------------+------+---------+------+------+----------+-------------+
| 1 | SIMPLE | t1 | index | NULL | ind2 | 6 | NULL | 4096 | 100 | Using index |
+----+-------------+-------+-------+---------------+------+---------+------+------+----------+-------------+
1 row in set
 
mysql> show warnings;
+-------+------+---------------------------------------------------------------+
| Level | Code | Message |
+-------+------+---------------------------------------------------------------+
| Note | 1003 | /* select #1 */ select count(0) AS `count(*)` from `test`.`t1` |
+-------+------+---------------------------------------------------------------+

show waring时发现select count(*) 会自动转换成count(0), 但是count某一个字段的时候就不会有这种优化了。

另外这里在贴下sql审核时什么时候要waring下

1、全表扫描
2、count(字段名) 非 count(*)
3、like
4、select *
5、oracle:用 outer join
6、查询时使用timestamp变量

总结一下,count星效率不低,mysql优化器会自动优化,另外有个waring要在自动审核时check下。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值