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

  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之内出结果  
优化代码SELECT SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND is_new_customer = 1, 1, 0)) AS new_customer_count, SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND is_new_customer = 0, 1, 0)) AS old_customer_count, SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND is_new_customer = 1, payment_amount, 0)) AS new_customer_payment_amount, SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND is_new_customer = 0, payment_amount, 0)) AS old_customer_payment_amount, SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND category = 'A', 1, 0)) AS category_A_customer_count, SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND category = 'A', payment_amount, 0)) AS category_A_payment_amount, SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND category = 'B', 1, 0)) AS category_B_customer_count, SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND category = 'B', payment_amount, 0)) AS category_B_payment_amount, SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND product_id = 'P1', 1, 0)) AS product_P1_customer_count, SUM(IF(order_date BETWEEN '2022-10-31' AND '2022-11-11' AND product_id = 'P1', payment_amount, 0)) AS product_P1_payment_amount FROM orders o LEFT JOIN (SELECT DISTINCT order_date FROM orders WHERE order_date BETWEEN '2022-10-31' AND '2022-11-11') d1 ON o.order_date = d1.order_date LEFT JOIN (SELECT DISTINCT order_date FROM orders WHERE order_date BETWEEN '2021-10-31' AND '2022-10-30' AND order_date NOT IN (SELECT order_date FROM orders WHERE order_date BETWEEN '2022-10-31' AND '2022-11-11')) d2 ON o.order_date = d2.order_date LEFT JOIN (SELECT DISTINCT order_date FROM orders WHERE order_date BETWEEN '2021-11-12' AND '2022-10-30' AND order_date IN (SELECT order_date FROM orders WHERE order_date BETWEEN '2022-10-31' AND '2022-11-11')) d3 ON o.order_date = d3.order_date WHERE d1.order_date IS NOT NULL OR d2.order_date IS NOT NULL OR d3.order_date IS NOT NULL;
最新发布
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值