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

时间:2012-08-02 21:00:09

标签: sql sql-server-2008 tsql

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

enter image description here

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

1 个答案:

答案 0 :(得分: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