当前位置:Gxlcms > 数据库问题 > sql 优化方法

sql 优化方法

时间:2021-07-01 10:21:17 帮助过:21人阅读

(2)用EXISTS替代IN、用NOT EXISTS替代NOT IN。

    exists 条件 返回的 是boolean型  只有 true 或者 false; 所以在exists 中的id 条件 应该是 外查询的查询id

    SELECT * FROM alumni_info t WHERE EXISTS(SELECT a_id FROM alumni_education e WHERE t.id_=‘6588‘ ),

  xeg:SELECT * FROM alumni_info t WHERE EXISTS(SELECT a_id FROM alumni_education e WHERE e.a_id=‘6588‘ ),返回的结果跟SELECT * FROM alumni_info t一样(a_id=6588存在),因为select语句先执行where条件后的语句,再筛选字段,当执行完where条件后,若这条结果集存在,则where表达式后面永远都是true,否则为false。也就是说要么查询所有,要么没有数据。

    卡券项目中我想查询卡券发行数量超过100张的卡券信息:   

    select * from wechatcarden wc where exists (select * from wechatcardsummary w where wc.id = w.weChatCardEnId and  skuQuantity>= 1000 )

等价于:select * from wechatcarden uw left join wechatcardsummary  ws on uw.id = ws.weChatCardEnId where ws.skuQuantity >= 1000

(3) 避免在索引列上使用计算

(4)避免在索引列上使用IS NULL和IS NOT NULL

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

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

(7)应尽量避免在 where 子句中对字段进行表达式操作,这将导致引擎放弃使用索引而进行全表扫描

sql 优化方法

标签:否则   justify   表达式   auto   card   port   扫描   表达   exist   

人气教程排行