SQL优化

SQL优化

表连接不宜超过三个,索引字段不宜超过五个

查询结果集合,使用exist替代in

提升group by 效率, 先筛选数据在group by

合理创建索引

 

参考:https://mp.weixin.qq.com/s?__biz=MzI2MjE3OTA1MA==&mid=2247497759&idx=1&sn=e2e4cdd3135b48b9a5ef91de4440bf76&chksm=ea4da99add3a208cd77444c428f691f7c203dbae3d05ded161b1aaa5a1ca7b45bf61272a55fb&scene=27

 

 

索引失效原因

创建测试表(userId为主键、username和age是联合索引)

使用like , where username like '%林' 不会使用索引;但是where username like '林%'可以使用索引

索引列有计算: where userId+ 1 > 3不会使用索引

索引有函数: where substr(name,1,2) = 'ja' 不会使用索引

没有索引首个字段: where age = 30 不会使用索引

 使用or: where userId = 1 or password = '123' 也不会使用索引

 

参考:https://blog.csdn.net/guoqi_666/article/details/122484441

https://www.cnblogs.com/xiaolincoding/p/15839040.html

https://blog.csdn.net/weixin_41156974/article/details/126319670

posted on 2019-04-15 16:28  周公  阅读(346)  评论(0编辑  收藏  举报

导航