【详解】SQL的优化

1、对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引(避免查询*   select * from table)

2、避免在 where 子句中使用!=或<>或者or或者in或者not in或者like,否则将引擎放弃使用索引而进行全表扫描,in用exist代替

不推荐:select id from table where num=10 or num = 20

          select num from a where num in(select num from b)

推荐:select id from table where num = 10 union all

          select id from table where num = 20

    select num from a where exists(select 1 from b where num=a.num)

3、不要对字段进行函数或者表达式操作,否则将引擎放弃使用索引而进行全表扫描

不推荐:select id from t where num/2=100  

select id from t where substring(name,1,3)='abc'

推荐:select id from t where num=100*2

          select id from t where name like 'abc%'

4、索引并不是越多越好,索引固然可以提高相应的 select 的效率,但同时也降低了 insert 及 update 的效率,因为 insert 或 update 时有可能会重建索引,一个表的索引数最好不要超过6个

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

在水一fang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值