版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26440803/article/details/82459640
碎片整理
mysql数据一开始是在磁盘上顺序存放的,如果数据表有频繁的update改动,那么就会形很多数据碎片,降低查询效率。
- 重新改变引擎,mysql重新整理数据
alter table user engine innodb;
optimize
optimize table user
min,max优化
select min(age) from table where status=1;
这条语句会全表扫描,所有我们可以在age
字段上建立索引,然后更改语句。
select min(age) from table use index(age) where status=1 limit 1;
count优化
select count(*) from table;
通过explain可以看出已经被mysql优化到常亮级别了;非常快
select count(*) from table where id>100;
扫描前100行数据是十分快的,但是扫描100行后的数据,随着数据量的庞大会降低查询速度。
( select count(*) from table ) - ( select count(*) from table where id<100 )
union
连接查询结果的时候 如非必要 尽量使用 union all,因为union all 不过滤数据,效率高,而union要过滤数据,代价很大;