数据库优化方案--sql语句优化

1.不轻易使用select * from t,用具体的字段列表代替 *,不要返回用不到的字段。

2.尽量避免在where子句中对字段进行null 值判断,否则将导致引擎放弃使用索引而进行全表扫描。

3.尽量避免在where语句中使用 !=<>操作符,否则将导致引擎放弃使用索引而进行全表扫描。

4.尽量避免在where子句中使用or来连接条件,如果一个字段有索引,一个字段没有索引,将导致引擎放弃使用索引而进行全表扫描。

如:

select id from t where num=10 or Name = 'admin'
可以替换为
select id from t where num = 10 union allselect id from t where Name = 'admin'

5.慎用in 和not in 否则会导致全表扫描。

6.尽量避免在where子句中对字段进行函数操作,这将导致引擎放弃使用索引而进行全表扫描。

如:

select id from t where substring(name,1,3) = ’abc’       -–name以abc开头的id
select id from t where datediff(day,createdate,’2005-11-30′) = 0    -–‘2005-11-30’    --生成的id

可以替换为:

select id from t where name like 'abc%'
select id from t where createdate >= '2005-11-30' and createdate < '2005-12-1'

等等。

详情参照:https://mp.weixin.qq.com/s?__biz=MzIxMjg4NDU1NA==&mid=2247483684&idx=1&sn=f5abc60e696b2063e43cd9ccb40df101&chksm=97be0c01a0c98517029ff9aa280b398ab5c81fa1fcfe0e746222a3bfe75396d9eea1e249af38&mpshare=1&scene=1&srcid=0606XGHeBS4RBZloVv786wBY#rd

猜你喜欢

转载自blog.csdn.net/Hepburn_li/article/details/81561099