简体   繁体   English

case 表达式的查询性能

[英]Query Performance on case expression

I have a table (Not all columns shown) that shows type.我有一个显示类型的表(未显示所有列)。 Typeid is the PK. Typeid 是 PK。 There is some date overlap that should not be there.有一些不应该存在的日期重叠。 I want to remove those from my query.我想从我的查询中删除那些。

Custid    typeid      start_dt     end_dt
 101        352       3/28/2017    1/16/2019
 101        353       1/15/2018    1/15/2019    

Data shows the below (example of overlap).数据显示如下(重叠示例)。

Custid    typeid      start_dt     end_dt     overlap
 101        352       3/28/2017    1/16/2019    1
 101        353       1/15/2018    1/15/2019    1
 201        426       1/1/2019     12/31/9999   0

SQL. SQL。 (Index are on all of the above table columns) (索引在上表的所有列上)

select *
from (select
      Custid, 
      typeid,
      start_dt,
      case
      when end_dt   > lead (start_dt) over (partition by custid order by start_dt) then 1
      when start_dt < lag (end_dt) over (partition by custid order by start_dt)    then 1
      else 0
      end overlap)
   where overlap = 0

The filtering on the case expression slows down the query returning results.对 case 表达式的过滤会减慢查询返回结果的速度。 Is there any tips/tricks to improve the query performance?是否有任何提示/技巧可以提高查询性能?

It may help if you can show the execution plan, and more specifics on the existing indexes.如果您可以显示执行计划以及现有索引的更多细节,这可能会有所帮助。

My immediate thought is that an index on (custid, start_dt) might avoid the need to sort to implement the windowing logic.我的直接想法是(custid, start_dt)上的索引可能会避免需要排序来实现窗口逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM