臧富祥 ^回到顶部

您的当前位置:首页 > 最新IT资讯 > MySQL > mysql优化-sql语句优化

mysql优化-sql语句优化

所属分类: MySQL   2020-03-18 17:10:39  编辑:admin  浏览次数 483 次

找到问题(高并发低消耗)==》分析语句(profile/explain)==》相应的处理

Sql语句优化count()max()

Count(*)   改为辅助索引

Select count(*) from table2;

Select count(*) from table2 where course_id >=0;

image.png

image.png

image.png

如上图,如果字段id有一个为空的时候,count不会统计进去的,特别注意

子查询优化

image.png

image.png

groupBy优化

image.png

image.png

Limit优化

干掉或者利用 limit offset,size 中的offset

不是直接使用limit,而是首先获取到offset的id然后直接使用limit size来获取数据

image.png

image.png

当然,这样会出现一点问题,如果id不连续会造成数据错误。

SELECT * FROM xxx WHERE ID > =(select id from xxx limit 1000000, 1) limit 20;
SELECT * FROM xxx a JOIN (select id from xxx limit 1000000, 20) b ON a.ID = b.id

Not in子查询优化

Select * from table1 where id not in (select id from table2);

Select * from table1 where id not exists(select id from table2 where table2.id=table1.id);

Or条件优化

Select *  from table1 where a = ‘123’ or b = ‘456’;

Select *  from table1 where a = ‘123’ union all Select *  from table1  b = ‘456’;

insert优化

锁定表,一次插入多个值(  values (1,’zfx’,’帅得不行’),(2,’臧富祥’,’没有zfx帅’),(......) ),解除锁定

禁用索引(disable keys),禁用唯一性检查,禁用外键检查,禁止自动提交

随笔文章检索

随笔文章目录