在此示例中,我只有两个正在使用的表。我正在检查哪个可能运行得更快并且执行计划是相同的。这些查询的目的是查找与一个记录关联的最小日期,该日期大于另一个记录的最大日期。第一个查询更简洁(尽管它不允许DATEDIFF
第二个查询的列)。从长远来看,我觉得第二种方式编写查询会在性能方面更加昂贵,也许当我有很多连接时。是否有关于哪种方法的一般规则,或者是逐案场景,您让优化器解决?
第一的
select c.patid,min(c.admitDate) as minDiabetesDate
from clm_extract as c
inner join icdClm as ic on ic.clmid=c.clmid
where ic.icd like '250%'
group by c.patid
having min(c.admitdate) >
(
select MAX(c2.admitDate) as maxPreDiabetesDate
from clm_extract as c2
inner join icdClm as ic2 on ic2.clmid = c2.clmid
where ic2.icd ='79029' and c2.patid=c.patid
group by c2.patid
)
第二
select distinct x.patid,x.minDiabetesDate,y.maxPreDiabetesDate
from
(
select c.patid, min(c.admitdate) as minDiabetesDate
from clm_extract as c
inner join icdClm as ic on ic.clmid=c.clmid
where ic.icd like '250%'
group by c.patid
)x
inner join
(
select c2.patid, MAX(c2.admitdate) as maxPreDiabetesDate
from clm_extract as c2
inner join icdClm as ic2 on ic2.clmid=c2.clmid
where ic2.icd ='79029'
group by c2.patid
)y on x.patid=y.patid
group by x.minDiabetesDate,y.maxPreDiabetesDate,x.patid
having DATEDIFF(dd,y.MaxPreDiabetesDate,x.minDiabetesDate) > 0