我正在处理一个对表单进行审计的查询。有几页需要审核的问题。填写表单后,答案将按以下方式存储在两个表中:
Table 1: smsmir.obsv OBS
EPISODE NO | FORM USAGE | QUEST | ANSWER | ...
123456789 | ADMISSION | QUESTION 1 | YES | ...
123456789 | ADMISSION | QUESTION 2 | 150 | ...
...
Table 2: smsdss.QOC_vst_summ QOC
EPISODE NO | HT IND | WT IND | ADV DIR | ...
123456789 | 1 | 1 | 0 | ...
...
表1:smsmir.obsv OBS
将信息存储在向量中,因此对于每个问题,都有另一行。表2:smsdss.QOC_vst_summ QOC
连续存储答案,因此每次访问只有一行。表3是相同的,每次访问id只有一行。
我的查询首先收集存储在表格中的VISIT IDS
,然后传递到下一组以回答一些问题。我从其他表中提取访问ID的原因是因为这是存储访问开始和结束日期的地方。该表看起来像这样:
Table 3: smsdss.BMH_PLM_PtAcct_V PAV
EPISODE NO | ADM DATE | ...
123456789 | 2013-08-01 | ...
...
我得到的所需输出是以下内容:
EPISODE NO | QUESTION 1 | QUESTION 2 | HT IND | WT IND | ADV DIR | ...
123456789 | 1 | 1 | 1 | 1 | 0 | ...
...
在上表中,1表示使用案例陈述回答问题,0表示未回答。查询已被重写,现在正在产生正确的结果,但是非常慢,要回到40条记录需要53分36秒。由于查询当前未完成,只返回7列,我必须将其扩展为总共65列。
我有子查询的原因是答案存储在一个向量中,每一行都是一个问题和答案,但由于我想在列中显示答案和问题,我做了一个子查询。有没有更好的方法来加快速度?
以下是查询:
-- THIS QUERY WILL PERFORM AN AUDIT OF THE ADMISSION ASSESSMENT AND
-- OTHER REQUIRED QUESTIONS BY NURSING INFORMATICS
-----------------------------------------------------------------------
-- VARIABLE DECLARATION AND INITIALIZATION. BY DECLARING A START AND
-- END DATE A USER CAN SIMPLY CHANGE THOSE PARAMETERS AND AUDIT ALL
-- INPATIENT ADMISSION ASSESSMENTS FOR THAT TIME PERIOD
DECLARE @SD DATETIME
DECLARE @ED DATETIME
SET @SD = '2013-08-01'
SET @ED = '2013-08-01'
-- QUERY 1
-- THIS QUERY CREATES A TABLE THAT WILL HOUSE ALL VISIT ID NUMBERS THAT
-- ARE GOING TO BE INCLUDED INSIDE OF THE ADMISSION ASSESSMENT AUDIT
-- TABLE DECLARATION ##################################################
DECLARE @T1 TABLE (
VISIT_ID VARCHAR(20))
-- ####################################################################
-- THESE ARE THE ITEMS THAT ARE GOING TO BE INSERTED INTO THE TABLE
INSERT INTO @T1
-- COLUMN SELECTION
SELECT A.PtNo_Num
-- DB(S) USED
FROM (SELECT DISTINCT PTNO_NUM
FROM smsdss.BMH_PLM_PtAcct_V
WHERE Adm_Date BETWEEN @SD AND @ED
AND Plm_Pt_Acct_Type = 'I') A
--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//
-----------------------------------------------------------------------
-- QUERY TWO. THIS QUERY WILL TAKE THE VISIT ID'S FROM QUERY 1 AND RUN
-- THEM THROUGH A SET OF RULES TO DECIDE WHEATHER OR NOT THE ADDMISISON
-- ASSESSMENT WAS PROPERLY DONE
-----------------------------------------------------------------------
-- COLUMN SELECTION
SELECT DISTINCT OBS.episode_no AS [VISIT ID]
-- CASE STATEMENT, IF PREFERRED LANGUAGE IS NOT 'NULL' THEN CONSIDER
-- THIS COMPLETE AND SCORE 1 ELSE CONSIDER INCOMPLETE AND SCORE 0
,
CASE
WHEN QOC.prim_lng IS NOT NULL THEN 1
ELSE 0
END AS [PREF LANG COMPLETE?],
QOC.ht_chtd_ind AS [HT IND],
QOC.wt_chtd_ind AS [WT IND],
QOC.adv_dir_ind AS [ADV DIRECTIVE]
-- A SEPERATE SELECT STATEMENT IS USED HERE BECAUSE RESULTS OF THE
-- ADMISSION CONSENT ARE STORED IN A VECTOR, SO IT IS NECESSARY TO
-- MAKE A SELECTION FROM THAT LIST, HERE A VALUE OF 1 = YES AND
-- 0 = NO
,
CASE
WHEN OBS.episode_no NOT IN (SELECT episode_no
FROM smsmir.obsv
WHERE form_usage = 'Admission') THEN 0
ELSE 1
END AS [ADMIT ASSESSMENT DONE],
CASE
WHEN OBS.episode_no NOT IN (SELECT episode_no
FROM smsmir.obsv
WHERE form_usage = 'Admission'
AND obsv_cd_ext_name = 'Admission consent signed:') THEN 0
ELSE 1
END AS [ADMIT CONSENT SIGNED?]
-- DB(S) USED ---------------------------------------------------------
FROM smsmir.obsv OBS
JOIN smsdss.QOC_vst_summ QOC
ON OBS.episode_no = QOC.episode_no
JOIN @T1 T1
ON OBS.episode_no = T1.VISIT_ID
-- FILTERS ------------------------------------------------------------
WHERE T1.VISIT_ID = OBS.episode_no
GROUP BY OBS.episode_no,
QOC.prim_lng,
QOC.ht_chtd_ind,
QOC.wt_chtd_ind,
QOC.adv_dir_ind,
OBS.obsv_cd_ext_name
--#####################################################################
-- END REPORT ...[]...[]...[]
您会注意到我使用的是NOT IN
条款,原因是如果没有提出或回答问题,就没有记录,甚至没有NULL
,所以如果我这样做的话不使用它,人可以完成所有其他事情,但如果不是那个特定项目,那么它们将从最终结果集中排除。
如果我需要澄清,请告诉我。
** QUERY实际执行计划XML ** query exec actual xml
谢谢
答案 0 :(得分:4)
smsmir.obsv
是UNION
- 一个155,569,000行表和一个15,375,000行表的视图。
执行计划显示这些表被扫描42次。
绝大多数是因为表变量的默认差基数估计意味着嵌套循环的选择不当。用#temp
表替换应解决该问题。
同样使用PIVOT
技术而不是单个子查询可以进一步减少这种情况。可以在添加缺失索引方面应用其他优化,但是您可以尝试这个并让我知道时间和执行计划吗?
DECLARE @SD DATETIME = '2013-08-01';
DECLARE @ED DATETIME = '2013-08-01';
CREATE TABLE #T1
(
VISIT_ID VARCHAR(20) UNIQUE CLUSTERED
)
INSERT INTO #T1
SELECT DISTINCT PTNO_NUM
FROM smsdss.BMH_PLM_PtAcct_V
WHERE Adm_Date BETWEEN @SD AND @ED
AND Plm_Pt_Acct_Type = 'I'
OPTION (RECOMPILE);
WITH OBS
AS (SELECT episode_no,
MAX(CASE
WHEN form_usage = 'Admission' THEN 1
END) AS [ADMIT ASSESSMENT DONE],
MAX(CASE
WHEN form_usage = 'Admission'
AND obsv_cd_ext_name = 'Admission consent signed:' THEN 1
END) AS [ADMIT CONSENT SIGNED?]
FROM smsmir.obsv
WHERE form_usage = 'Admission'
GROUP BY episode_no)
SELECT OBS.episode_no AS [VISIT ID],
CASE
WHEN QOC.prim_lng IS NOT NULL THEN 1
ELSE 0
END AS [PREF LANG COMPLETE?],
QOC.ht_chtd_ind AS [HT IND],
QOC.wt_chtd_ind AS [WT IND],
QOC.adv_dir_ind AS [ADV DIRECTIVE],
ISNULL(OBS.[ADMIT ASSESSMENT DONE], 0) AS [ADMIT ASSESSMENT DONE],
ISNULL(OBS.[ADMIT CONSENT SIGNED?], 0) AS [ADMIT CONSENT SIGNED?]
FROM smsdss.QOC_vst_summ QOC
JOIN #T1
ON #T1.VISIT_ID = QOC.episode_no
LEFT JOIN OBS
ON OBS.episode_no = QOC.episode_no
DROP TABLE #T1
答案 1 :(得分:0)
你不必再次点击表格,你已经掌握了数据。聚合它。
DECLARE @SD DATETIME
DECLARE @ED DATETIME
SET @SD = '2013-08-01'
SET @ED = '2013-08-01'
DECLARE @T1 TABLE (
VISIT_ID VARCHAR(20))
INSERT INTO @T1
SELECT A.PtNo_Num
FROM (SELECT DISTINCT PTNO_NUM
FROM smsdss.BMH_PLM_PtAcct_V
WHERE Adm_Date BETWEEN @SD AND @ED
AND Plm_Pt_Acct_Type = 'I') A
SELECT DISTINCT OBS.episode_no AS [VISIT ID],
CASE
WHEN QOC.prim_lng IS NOT NULL THEN 1
ELSE 0
END AS [PREF LANG COMPLETE?],
QOC.ht_chtd_ind AS [HT IND],
QOC.wt_chtd_ind AS [WT IND],
QOC.adv_dir_ind AS [ADV DIRECTIVE],
max(CASE
WHEN form_usage = 'Admission' THEN 1
ELSE 0
END) AS [ADMIT ASSESSMENT DONE],
max(CASE
WHEN form_usage = 'Admission' AND obsv_cd_ext_name = 'Admission consent signed:' THEN 1
ELSE 0
END ) AS [ADMIT CONSENT SIGNED?]
FROM smsmir.obsv OBS
JOIN smsdss.QOC_vst_summ QOC
ON OBS.episode_no = QOC.episode_no
JOIN @T1 T1
ON OBS.episode_no = T1.VISIT_ID
WHERE T1.VISIT_ID = OBS.episode_no
GROUP BY OBS.episode_no,
QOC.prim_lng,
QOC.ht_chtd_ind,
QOC.wt_chtd_ind,
QOC.adv_dir_ind,
OBS.obsv_cd_ext_name