一、count() 优化

误区:myisamcount()非常快

: 是比较快,.但仅限于查询表的”所有行”比较快, 因为Myisam对行数进行了存储.;一旦有条件的查询, 速度就不再快了.尤其是where条件的列上没有索引.

 

如:想查询id>=100的商家有多少?

select count(*) from store where id>=100;  (1000多万行用了6.X)

小技巧:

select count(*) from store;

select count(*) from store where id<100;

select (select count(*) from store) - (select count(*) from store where id<100)

 

二、 group by

注意:

1:分组用于统计,而不用于筛选数据;比如: 统计平均分,最高分,适合, 但用于筛选重复数据,则不适合,以及用索引来避免临时表和文件排序

 2:  A,B表连接为例 ,主要查询A表的列那么 group by ,order by 的列尽量相同,而且列应该显示声明为A的列

 

三、union优化

注意: union all 不过滤,效率提高,如非必须,请用union all

因为 union去重的代价非常高, 放在程序里去重.

 

posted on 2020-12-11 16:02  鸥海  阅读(343)  评论(0编辑  收藏  举报