SQL语句优化

减少使用子查询

不要使用select *

使用select *的话会增加解析的时间,另外会把不需要的数据也给查询出来,数据传输也是耗费时间的,

谨慎使用模糊查询

当模糊匹配以%开头时,该列索引将失效,若不以%开头,该列索引有效。

不要使用列号

使用列号的话,将会增加不必要的解析时间。

in 和 not in 也要慎用,否则会导致全表扫描

1
2
3
select id from t where num in(1,2,3)	
对于连续的数值,能用 between 就不要用 in 了:
select id from t where num between 1 and 3

应尽量避免在 where 子句中对字段进行表达式操作

这将导致引擎放弃使用索引而进行全表扫描。如:

1
2
3
4
5
select id from t where num/2=100	

应改为:

select id from t where num=100*2

应尽量避免在where子句中对字段进行函数操作

这将导致引擎放弃使用索引而进行全表扫描。如:

1
2
3
select id from t where substring(name,1,3)='abc'--name以abc开头的id	
应改为:
select id from t where name like 'abc%'
打赏
  • 版权声明: 本博客所有文章除特别声明外,著作权归作者所有。转载请注明出处!
  • Copyrights © 2015-2023 高行行
  • 访问人数: | 浏览次数:

请我喝杯咖啡吧~

支付宝
微信