因工作的需求,所以近期开始对数据库进行深入的研究,会慢慢的由浅到深的了解学习。
首先是常见的统计查询,之前只知道select count() from table,其实还有一种select count(1) from table,数据小时用时差不多,但大数据时差别就很明显了。这个已经做过实践用时:count()>count(1)
常用的数据统计有:count(),count(1),count(primary key)
统计时间从大到小:count()>count(1)>count(PRIMARY KEY)
注:只有在第一次统计时上面的理论才成立,如果是多次相同的统计,count(1)是最快的。
下面是一个实例:
describe billamount.bill_customer_amount; #查看库表结构
SELECT count() from billamount.bill_customer_amount; #count()
SELECT count(1) from billamount.bill_customer_amount; #count(1)
SELECT count(billamount.bill_customer_amount.id) from billamount.bill_customer_amount; #count(PRIMARY KEY)
库里一共有:249434条数据,库表结构如下:
执行多次查询后所用时间:
从上面很容易就看出,count(1)是用时最少的,除第一次查询外。