SQL优化 - 不存在的地方自加入

时间:2016-08-17 07:41:59

标签: sql sql-server query-optimization

请提供有关如何优化此sql过程的建议...不存在的地方subselect是我的主要问题。有人告诉我,我可以获得40%的性能提升。

   insert into #tmpTable (intID3,intID4,intID5,intID6, 
          StaticDate,NewDate, 
          [Description],Change,intSort) 
   select distinct 
          '' as intID3,           
          intID4,
          intID5, 
          intID6, 
          @dteDate1 as StaticDate, 
          '' as NewDate,           
          '16 character str' as [Description], 
          0 as Movement,
          0 as intSort

   from #tmpTable j 
   where not exists ( 
        select 
               1 
        from #tmpTable x 
        where
               x.intID1 = j.intID1 
               and x.intID2 = j.intID2 
               and x.[Description] = '16 character str' 
   ) 

1 个答案:

答案 0 :(得分:1)

尝试使用以下查询。

 INSERT INTO #tmpTable2 
     (intID3,intID4,intID5,intID6, 
      StaticDate,NewDate, 
      [Description],Change,intSort)
  SELECT DISTINCT 
      '' as intID3,           
      j.intID4,
      j.intID5, 
      j.intID6, 
      @dteDate1 as StaticDate, 
      '' as NewDate,           
      '16 character str' as [Description], 
      0 as Movement,
      0 as intSort
 FROM #tmpTable j 
       LEFT JOIN #tmpTable x  
             ON  x.intID1 = j.intID1 
                  AND x.intID2 = j.intID2 
                  AND x.[Description] = '16 character str' 
 WHERE x.intID1 IS NULL