zoukankan      html  css  js  c++  java
  • SQL语句优化一例 row_number not in or

    select [id],[poster],[fid],[title],[editername],[modifytime] from(
    select [id],[poster],[fid],[title],[editername],[modifytime], row_number() over
    ( order by id desc) as row from [news] WITH (NOLOCK) 
     where   path like 'A_B%' and  id not in 
     (select id from [news] where layer=7 or layer=5)) a  
     where row between 1 and 15
     

     上面的查询很慢。如果只执行子查询就很快,但是做 where row between 1 and 15 分页就很慢很慢,原来是因为查询里面用了or,优化成下面的就不超时了

     
     select [id],[poster],[fid],[title],[editername],[modifytime] from(
    select [id],[poster],[fid],[title],[editername],[modifytime], row_number() over
    ( order by id desc) as row from [news] WITH (NOLOCK) 
     where   path like 'A_B%' and  id not in 
     (select id from [news] where layer=7 union select id from [news] where layer=5)) a  
     where row between 1 and 15
  • 相关阅读:
    作业呢
    留言板
    题解 lg2480 古代猪文
    题解 lg4139 上帝与集合的正确用法
    AFO
    题解 lg2946 [USACO09MAR]Cow Frisbee Team S
    题解 lg4158 [SCOI2009]粉刷匠
    题解 HDU4035 Maze
    题解 lg3232 [HNOI2013]游走
    题解 HDU4652 Dice
  • 原文地址:https://www.cnblogs.com/nasdaqhe/p/2007460.html
Copyright © 2011-2022 走看看