1、max()查询优化mysql
rows一行的值为16451说明须要查询16451行才能找到,当数据量很大的时候查询起来会很耗时,此时咱们能够在payment_date列创建索引sql
这里最后加了个\G 能使输出按列打印
函数
此时直接就能够找到了,大大减小了查询时间,因此当使用max()函数时咱们能够考虑使用索引。优化
2、count()查询优化
spa
首先区分count(*)、count(列名:如id)、count(1)(count(*)、count(id)、count(1))blog
用count(*) 好呢 仍是count(id)好, 仍是count(1)好? 这两种的选择有时候执行结果是不同的。
例如:
mysql>create table t(id int);
mysql>insert into t(id) values(1);
mysql>insert into t(id) values(2);
mysql>insert into t(id) values(null);
mysql>select * from t;
+------------+
| id |
+------------+
| 1 |
| 2 |
| NULL|
mysql>select count(*),count(1),count(id) from t;
结果:count(*)=3条 ; count(1)=3条 count(id)=2条
由于count(*) ,count(1)包括null值,count(id)忽略null值
索引