zoukankan      html  css  js  c++  java
  • sql语句优化:用join取代not in

    转载;  http://www.cnblogs.com/rising-fay/archive/2012/11/13/2767538.html

    写了好几个页面,速度都上不去,瓶颈在于SQL查询。太多的表,太多的not in,总是从一大推表和数据中筛选出一点数据。看了很多关于SQL优化的文章,都强烈要求不要太多使用not in查询,最好用表连接来取代它。如:


    select ID,name from Table_A where ID not in (select ID from Table_B)

    这句是最经典的not in查询了。改为表连接代码如下:

    select Table_A.ID,Table_A.name from Table_A left join Table_B on Table_A.ID=Table_B.ID and Table_B.ID is null
    或者:
    select Table_A.ID,Table_A.name from Table_A left join Table_B on Table_A.ID=Table_B.ID where Table_B.ID is null

    经试用,效果立竿见影。
  • 相关阅读:
    2020/3/12
    练习题1
    2020/3/26
    2020/3/25
    2020/3/24
    2020/3/23
    应用层
    bzoj3326[SCOI2013]数数
    HEOI2017游记
    bzoj4417[SHOI2013]超级跳马
  • 原文地址:https://www.cnblogs.com/dafei4/p/12939216.html
Copyright © 2011-2022 走看看