为什么创建视图的查询比同一视图中的Select 1000运行得更快

时间:2015-01-21 21:41:37

标签: sql tsql

我构建了一个我已经变成视图的查询,视图中的查询大约运行9分钟,行数为160万。当我从该视图查询Select Top 1000时,它会连续运行而不提供任何结果。为什么会发生这样的事情?

CREATE VIEW [dbo].[v_BP_OP] 
AS  

--pulls ORCA BPs

with OBP as (
SELECT DISTINCT s.EPI 
            ,e.CONTACT_DATE 
            ,s.EventDtTm 
            ,s.SBP 
            ,d.dbp

FROM uwEpicClarity.dbo.PAT_ENC e 
with(nolock)
INNER JOIN [uwQAQI_DS].[dbo].[v_BP_Systolic]s
with(nolock)on e.EPI = s.EPI and CONVERT(date,e.CONTACT_DATE) =      CONVERT(date,s.EventDtTm)
INNER JOIN [uwQAQI_DS].[dbo].[v_BP_Diastolic]d
with(nolock)on s.EID = d.EID and s.EventDtTm = d.EventDtTm 

WHERE e.CONTACT_DATE >= '2014-01-01'
and e.ENC_TYPE_C in (50,101)
and e.APPT_STATUS_C_DESCR in ('Completed','Arrived')
and s.Src = 'ORCA_CE'
and d.Src = 'ORCA_CE'
        )

--combines ORCA and Epic BPS            
SELECT DISTINCT EPI 
,CONTACT_DATE 
,EventDtTm 
,CAST(SBP as varchar) + '/' + CAST(DBP as varchar) as Meas_Value
,'ORCA' as SrcSys
FROM obp

UNION

SELECT DISTINCT e.EPI 
        ,e.CONTACT_DATE
        ,fm.[RECORDED_TIME]
        ,CAST(MEAS_VALUE as varchar)as MEAS_VALUE
        ,'Epic' as SrcSys


  FROM [uwEpicClarity].[dbo].[IP_FLWSHT_MEAS] fm with (NoLock)
  INNER JOIN uwEpicClarity.dbo.[IP_FLWSHT_REC] fr with (NoLock) on fm.FSD_ID   = fr.FSD_ID
  INNER JOIN uwEpicClarity.dbo.PAT_ENC e with (NoLock) on   fr.INPATIENT_DATA_ID = e.INPATIENT_DATA_ID
  WHERE e.CONTACT_DATE >= '2014-01-01' 
  and FLO_MEAS_ID in ('5','116')
  and e.ENC_TYPE_C in (50,101)
and e.APPT_STATUS_C_DESCR in ('Completed','Arrived')





GO

0 个答案:

没有答案