0

これらのクエリはどちらも同じことを行い、一方は他方の約 2.5 倍の速度で実行されます。

遅いクエリ

select sc.countyName
        ,(
    --this adds the distinct number of people who received rehab    
    select count(distinct p2.patientid)
        from statecounties as sc2
        inner join Patient as p2 on p2.stateCode=sc2.stateCode
        and p2.countyCode = sc2.countyCode
        and sc2.stateCode='21'
        inner join Claims as c on c.patientid=p2.patientid
        and c.admissiondate = p2.admissiondate
        and c.dischargedate = p2.dischargedate
        where sc2.countyName=sc.countyname and c.hcpcs in ('97001', '9339', '97002')

     ) as rehabClaims
     --this is the total number of people from each county that visited a hospital with a DRG stroke admission
     ,count(*) as visitCounts
     ,(
    --this adds the distinct number of people who received rehab    
    select count(distinct p2.patientid)
        from statecounties as sc2
        inner join Patient as p2 on p2.stateCode=sc2.stateCode
        and p2.countyCode = sc2.countyCode
        and sc2.stateCode='21'
        inner join Claims as c on c.patientid=p2.patientid
        and c.admissiondate = p2.admissiondate
        and c.dischargedate = p2.dischargedate
        where sc2.countyName=sc.countyname and c.hcpcs in ('97001', '9339', '97002')

     )/count(*) as visitCounts
     from StateCounties as sc
    inner join Patient as p on p.stateCode=sc.stateCode
    and p.countyCode=sc.countyCode
    where sc.stateCode='21'
    group by sc.countyName
    having count(*) > 30

高速クエリ

select *, round(cast(rehabclaims as float)/VisitCounts *100,2) as percentRehab
from
(
select sc.countyName
        ,(
    --this adds the distinct number of people who received rehab    
    select count(distinct p2.patientid)
        from statecounties as sc2
        inner join Patient as p2 on p2.stateCode=sc2.stateCode
        and p2.countyCode = sc2.countyCode
        and sc2.stateCode='21'
        inner join Claims as c on c.patientid=p2.patientid
        and c.admissiondate = p2.admissiondate
        and c.dischargedate = p2.dischargedate
        where sc2.countyName=sc.countyname and c.hcpcs in ('97001', '9339', '97002')

     ) as rehabClaims
     --this is the total number of people from each county that visited a hospital with a DRG stroke admission
    ,count(*) as visitCounts
    from StateCounties as sc
    inner join Patient as p on p.stateCode=sc.stateCode
    and p.countyCode=sc.countyCode
    where sc.stateCode='21'
    group by sc.countyName
    having count(*) > 30
) t

主な違いは、最初の除算はsub selectsそれ自体で行われるのに対し、高速なクエリではサブクエリを使用することです (専門用語が間違っている場合はお詫びします)。式全体を入力するよりも columnName/otherColumnName を読む方がはるかに簡単なので、私は 2 番目の方法を好みます。これも非常に高速な場合が多いようです。これには理由がありますか、それとも単なる逸話ですか (適切なインデックスが適用されます)

4

0 に答える 0