有没有办法减少以下查询的执行时间,执行需要将近2分钟。我已经占用了大部分键并将其与表格相匹配。任何建议表示赞赏。
select cv.idcode as 'RHRN',
cv.visitidcode as 'Encounter',
cv.CurrentLocation ,
cpGrp.displayname as 'AttendingGroup' ,
ar.Description as 'AlertDescription',
ar.createdwhen as 'AlertCreatedWhen',
bo.value as 'CreatinineLevel',
bo.CreatedWhen as 'LabResultCreatedWhen',
ctv.value as 'ClassTypeValue',
isnull (o.ordersetname,'') as 'OrderSetName',
o.idcode as 'OrderID',
o.name as 'OrderName',
me.DosageLow as 'Dose',
me.Uom as 'UOM',
o.RequestedDtm as 'OrderRequestedDTM' ,
o.status as 'OrderStatus',
o.StopDtm 'OrderStopDTM',
me.ClientGUID
into #tmp1
from cv3order o (nolock)
inner join cv3clientvisit cv (nolock) on o.ClientGUID = cv.ClientGUID ` `and o.ChartGUID = cv.ChartGUID
inner join cv3CareProviderVisitRole cpvrGrp (nolock) on ``cpvrGrp.clientVisitGuid = cv.guid and cpvrGrp.clientguid=cv.ClientGUID
inner join cv3CareProvider cpGrp (nolock)on cpGrp.guid = ` `cpvrGrp.providerGuid and cpvrGrp.rolecode = 'attending group'
inner join cv3alertRepository ar (nolock) on ar.ClientVisitGUID = cv.guid
inner join cv3CatalogClassTypeValue ctv (nolock) on ctv.CatalogMasterGUID = o.OrderCatalogMasterItemGUID
inner join cv3ClassType ct (nolock) on ct.GUID = ctv.ClassTypeGUID
inner join CV3MedicationExtension me(nolock) on me.guid = o.guid and me.ClientGUID=o.ClientGUID
inner join CV3BasicObservation bo (nolock) on bo.ClientVisitGUID = cv.guid and bo.ChartGUID=o.ChartGUID and bo.ClientGUID=o.ClientGUID
where ar.mlmname = 'AHS_AKI_Creatinine_Result'
and ct.code = 'Acute Kidney Injury - AKI' and ctv.value in ('renally eliminated', 'nephrotoxicity')
and bo.ItemName = 'Creatinine LEVEL'
and cv.CurrentLocation like 'schc%'
--and cv.CurrentLocation not LIKE 'FMC-55%'
--and cv.CurrentLocation not like 'FMC-37B%'
--and cv.CurrentLocation not like 'ACH%'
select
RHRN,
Encounter,
max(LabResultCreatedWhen) as 'lastResultWhen'
into #tmp2 -- get the most recent lab result
from #tmp1
group by
RHRN,
Encounter
select
RHRN,
Encounter,
max(AlertCreatedWhen) as 'lastAlertWhen'
into #tmp3 -- get the most recent alert
from #tmp1
group by
RHRN,
Encounter
select t1.*
from #tmp1 t1
inner join #tmp2 t2 on t1.LabResultCreatedWhen = t2.lastResultWhen
inner join #tmp3 t3 on t1.AlertCreatedWhen = t3.lastAlertWhen
order by t1.RHRN
drop table #tmp1
drop table #tmp2
drop table #tmp3