【SQL改写】notexists-leftjoin(distinct)whereisnull改写_标量子查询

来源:互联网 发布:jquery调用js函数 编辑:程序博客网 时间:2024/06/05 14:19
  1. --原SQL 30min出不来结果  
  2. select a.*  
  3. from TeableA a  
  4. where is_open = '1'  
  5. and  ecd = 'xshg'  
  6. and  not exists  
  7. (select * from  bvaluation b  
  8.      where a.calendar_date = b.tdate)   
  9. and a.calendar_date >= '2011-09-06'  
  10. and a.calendar_date < convert( date, getdate())  
  11.    
  12. --表信息查看  
  13. select count(1)  
  14. from TeableA a  
  15. where is_open = '1'  
  16. and  ecd = 'xshg'  
  17. and a.calendar_date >= '2011-09-06'  
  18. and a.calendar_date < convert( date, getdate())  
  19. --1357  
  20. select count(1) from  bvaluation b  
  21. --8262721  
  22.    
  23. --改写SQL1  
  24. select  
  25.   a.*  
  26. from TeableA a left join tdate b on (a.calendar_date = b.tdate)  
  27. where  
  28.   a.is_open = '1' and  
  29.   a.ecd = 'xshg' and  
  30.   a.calendar_date >= '2011-09-06' and  
  31.   a.calendar_date < convert( date,getdate()) and  
  32.   b.tdate is null  
  33. --发现a.calendar_date: b.tdate=1:N ,join后会导致a表的重复,查询反馈很快,但是因为重复result set跑完耗时还是较长。  
  34. --sp_helpindex bvaluation  
  35. --bvaluation$BPK_AK_Key    nonclustered, unique, unique key located on DATA    SID, TSYMBOL, tdate, YEART_MAT  
  36. --唯一约束中包含tdate  
  37.    
  38. --改写SQL2(Final)  
  39. select  
  40.   a.*  
  41. from TeableA a left join (select distinct tdate from bvaluation)  
  42.  b on (a.calendar_date = b.tdate)  
  43. where  
  44.   a.is_open = '1' and  
  45.   a.ecd = 'xshg' and  
  46.   a.calendar_date >= '2011-09-06' and  
  47.   a.calendar_date < convert( date,getdate()) and  
  48.   b.tdate is null  
  49. --1s之内出结果  
原创粉丝点击