使用带有大表的WHERE EXISTS查询可扩展性

woo*_*gie 5 sql t-sql sql-server-2008

以下查询旨在查找去医院的人数,去医院的总人数以及将这两者划分为百分比.该表Claims是200多万行,并且具有正确的非聚集索引patientid, admissiondate, and dischargdate.查询运行得足够快,但我对如何使它更有用感兴趣.我希望能够在行中添加另一个代码,(hcpcs.hcpcs ='97001')percentRehabNotHomeHealth在另一列中进行更改.有没有可能没有写一个很大的胖连接语句,我将两个查询的结果连接在一起?我知道通过添加额外的列,数学看起来不正确,但我现在并不担心.所需的样本输出:http://imgur.com/BCLrd
数据库模式

在此输入图像描述

select  h.hospitalname
    ,count(*) as visitCounts
    ,hospitalcounts
    ,round(count(*)/cast(hospitalcounts as float) *100,2) as percentRehabNotHomeHealth
    from Patient p
    inner join statecounties as sc on sc.countycode = p.countycode
    and sc.statecode = p.statecode
    inner join hospitals as h on h.npi=p.hospitalnpi
    inner join
    --this join adds the hospitalCounts column
    (
        select h.hospitalname, count(*) as hospitalCounts
            from hospitals as h
            inner join patient as p on p.hospitalnpi=h.npi
            where p.statecode='21' and h.statecode='21'
            group by h.hospitalname
    ) as t on t.hospitalname=h.hospitalname
    --this where exists clause gives the visitCounts column
    where h.stateCode='21' and p.statecode='21'
    and exists
    (
        select distinct p2.patientid
            from Patient as p2
            inner join Claims as c on c.patientid = p2.patientid
            and c.admissiondate = p2.admissiondate
            and c.dischargedate = p2.dischargedate
            inner join hcpcs on hcpcs.hcpcs=c.hcpcs
            inner join hospitals as h on h.npi=p2.hospitalnpi
            where (hcpcs.hcpcs ='97001' or hcpcs.hcpcs='9339' or hcpcs.hcpcs='97002')
            and p2.patientid=p.patientid 
    ) 
    and hospitalcounts > 10
    group by h.hospitalname, t.hospitalcounts
    having count(*)>10
Run Code Online (Sandbox Code Playgroud)

小智 4

您可以查看 CTE(通用表表达式)来获取您需要的内容。它将允许您获取汇总数据并将其连接回公共密钥的详细信息。作为示例,我将子查询上的联接修改为 CTE。

;with hospitalCounts as (
    select h.hospitalname, count(*) as hospitalCounts
    from hospitals as h
    inner join patient as p on p.hospitalnpi=h.npi
    where p.statecode='21' and h.statecode='21'
    group by h.hospitalname
)
select  h.hospitalname
    ,count(*) as visitCounts
    ,hospitalcounts
    ,round(count(*)/cast(hospitalcounts as float) *100,2) as percentRehabNotHomeHealth
from Patient p
inner join statecounties as sc on sc.countycode = p.countycode
and sc.statecode = p.statecode
inner join hospitals as h on h.npi=p.hospitalnpi
inner join hospitalCounts on t.hospitalname=h.hospitalname
--this where exists clause gives the visitCounts column
where h.stateCode='21' and p.statecode='21'
and exists
(
    select p2.patientid
        from Patient as p2
        inner join Claims as c on c.patientid = p2.patientid
        and c.admissiondate = p2.admissiondate
        and c.dischargedate = p2.dischargedate
        inner join hcpcs on hcpcs.hcpcs=c.hcpcs
        inner join hospitals as h on h.npi=p2.hospitalnpi
        where (hcpcs.hcpcs ='97001' or hcpcs.hcpcs='9339' or hcpcs.hcpcs='97002')
        and p2.patientid=p.patientid 
) 
and hospitalcounts > 10
group by h.hospitalname, t.hospitalcounts
having count(*)>10
Run Code Online (Sandbox Code Playgroud)