1

我正在处理一个对表单进行审计的查询。有几页值得审核的问题。填写表格后,答案将按以下方式存储在两个表中:

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子句,原因是如果一个问题没有被问或回答,就没有记录,甚至没有 a NULL,所以如果我不使用它,人可以完成所有其他事情但是如果不是那个特定的项目,那么它们将被排除在最终结果集中。

如果我需要澄清,请告诉我。

** QUERY 实际执行计划 XML ** 查询 exec 实际 xml

谢谢

4

2 回答 2

4

smsmir.obsv是一个视图,即UNION-sa 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
于 2013-08-28T20:22:52.917 回答
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
于 2013-08-29T12:36:22.393 回答