MySQL统计数据count(*) 和 count(1) 什么区别??
1.查询数据结果集没有区别
cite : http://stackoverflow.com/questions/3003457/count-vs-countcolumn-name-which-is-more-correct
2.性能区别区别
记得看《sql 沉思者》中提及部分数据库对count(1)的优化
在部分数据库下,会对select count(1)做优化,导致select count(1) better than select count(*)
看到这个问题,我不清楚mysql下是否存在相同的问题,所以做了一个查询工作
cite :http://www.phpddt.com/db/mysql-count-1.html
然后我就继续google了,针对mysql镜像查询
cite :http://boards.straightdope.com/sdmb/showthread.php?t=455365
参看十五十六楼的回答,证实了 mysql 下 select (1) is faster than select(*)
建议查询的时候尽量使用select (1) 替代select (*)
没有区别.
mysql> explain extended select count(*) from tmp;
...
1 row in set, 1 warning (0.34 sec)
mysql> show warnings;
+-------+------+--------------------------------------------------+
| Level | Code | Message |
+-------+------+--------------------------------------------------+
| Note | 1003 | select count(0) AS `count(*)` from `test1`.`tmp` |
+-------+------+--------------------------------------------------+
1 row in set (0.00 sec)
http://dev.mysql.com/doc/refman/5.6/en/group-by-functions.html#function_count
COUNT(expr) Returns a count of the number of non-NULL values of expr in
the rows retrieved by a SELECT statement. ... COUNT(*) is somewhat different
in that it returns a count of the number of rows retrieved, whether or
not they contain NULL values.