码迷,mamicode.com
首页 > 其他好文 > 详细

count(1) count(*)

时间:2016-07-02 20:17:10      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

mysql> select 1 from t;
+---+
| 1 |
+---+
| 1 |
| 1 |
| 1 |
| 1 |
+---+
4 rows in set (0.00 sec)
mysql> select count(1) from t;    
+----------+
| count(1) |
+----------+
|        4 |
+----------+
1 row in set (0.00 sec)
mysql> select count(*) from t; 
+----------+
| count(*) |
+----------+
|        4 |
+----------+
1 row in set (0.00 sec)
mysql> select count(a) from t; 
+----------+
| count(a) |
+----------+
|        4 |
+----------+
1 row in set (0.00 sec)

如果表中没有主键,那么count(1)比count(*)快
如果有主键,那么count(主键,联合主键)比count(*)快
如果表中只有一个字段,count(*)最快

count(1) count(*)

标签:

原文地址:http://www.cnblogs.com/zengkefu/p/5635908.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!