SQL查询调优

时间:2014-03-17 17:43:18

标签: sql-server performance

我刚刚开始学习查询调优,我正在寻找一些有关我的问题的帮助。如果保持原样,此查询将运行几个小时,但我被告知,一个小的更改将使其在一瞬间运行。我已经尝试过几件事,但似乎无法让它做我正在寻找的东西。任何帮助将不胜感激

SELECT     dt_ARRIVAL, s_PATIENT_FULL_NAME, n_ENCOUNTER_ID, s_VISIT_IDENT, dbo.fn_CheckIfRV(n_ENCOUNTER_ID, dt_ARRIVAL) AS RV, dt_DEPARTURE, 
                  s_OUTCOME_LOCATION, s_DispoLoc, s_LAST_ACUITY, s_COMPLAINT_FOR_SORT, s_DIAGNOSIS_FOR_SORT, s_GENDER, s_AGE, 
                  CASE WHEN Len(dbo.vw_RPT_ADHOC_DATA_DUMP.s_InitLabOrderList) > 4 THEN 1 ELSE 0 END AS LABv21, 
                  CASE WHEN Len(dbo.vw_RPT_ADHOC_DATA_DUMP.s_InitRadOrderList) > 4 THEN 1 ELSE 0 END AS RADv21
FROM         dbo.vw_RPT_ADHOC_DATA_DUMP
WHERE     (dt_ARRIVAL BETWEEN CONVERT(DATETIME, '2012-08-13 00:00:00', 102) AND     CONVERT(DATETIME, '2013-02-13 23:00:00', 102))

2 个答案:

答案 0 :(得分:2)

SELECT     dt_ARRIVAL, s_PATIENT_FULL_NAME, n_ENCOUNTER_ID, s_VISIT_IDENT, dbo.fn_CheckIfRV(n_ENCOUNTER_ID, dt_ARRIVAL) AS RV, dt_DEPARTURE, 
                  s_OUTCOME_LOCATION, s_DispoLoc, s_LAST_ACUITY, s_COMPLAINT_FOR_SORT, s_DIAGNOSIS_FOR_SORT, s_GENDER, s_AGE, 
                  CASE WHEN Len(dbo.vw_RPT_ADHOC_DATA_DUMP.s_InitLabOrderList) > 4 THEN 1 ELSE 0 END AS LABv21, 
                  CASE WHEN Len(dbo.vw_RPT_ADHOC_DATA_DUMP.s_InitRadOrderList) > 4 THEN 1 ELSE 0 END AS RADv21
FROM         dbo.vw_RPT_ADHOC_DATA_DUMP, 
       (select CONVERT(DATETIME, '2012-08-13 00:00:00', 102)  as date_from, 
               CONVERT(DATETIME, '2013-02-13 23:00:00', 102) as date_to) dates
WHERE     (dt_ARRIVAL BETWEEN dates.date_from AND dates.date_to)

尝试仅执行一次CONVERT,而不是每行

答案 1 :(得分:0)

根据 dbo.vw_RPT_ADHOC_DATA_DUMP 中的“ vw ”判断,查询似乎是针对视图而不是表格。查看视图的内部连接并尝试优化它。即使视图已经过优化,您也可能会发现对于此特定查询,您可以进行一些简化。

无论如何,我们需要查看视图的代码以改进我们的答案,如果它真的是一个视图。