[英]How do I optimize the following query?
Im using the following query to create a report in SSRS, but its taking about 10min to givmme the result.我使用以下查询在 SSRS 中创建报告,但需要大约 10 分钟才能得到结果。
I was trying to add index to the view but seems like i dont have the permission to do so.我试图向视图添加索引,但似乎我没有这样做的权限。
Is there any other way to optimize the query?还有其他方法可以优化查询吗?
(FYI, this query is joining table and view together, not sure if this causing the slowness) (仅供参考,此查询将表和视图连接在一起,不确定这是否会导致速度缓慢)
/*Im creating the temp table here, because i think it would help run faster but it does not*/
SELECT
QM.*
INTO #QM
FROM ODS.dbo.QNXT_MEMBER QM
DROP TABLE IF EXISTS #CVG
SELECT
CVG.*
INTO #CVG
FROM JIVA_DWH.dbo.mbr_cvg CVG
DROP TABLE IF EXISTS #1;
SELECT G.ext_cvg_id MemberSourceId,
A.MBR_IDN,
I.ENC_IDN,
I.INTRACN_IDN,
A.ACTIVITY,
A.ACTIVITY_TYPE,
A.UPDATED_DATE,
A.ACTIVITY_STATUS,
A.SCHEDULED_DATE,
I.INTERACTION_DATE,
I.INTERACTION_OUTCOME,
I.INTERACTION_STATUS,
I.MODIFIED_USER,
M.STATUS_CHANGE_DATE,
M.EPISODE_STATUS,
MP.ALTERNATE_ID,
[ROW_NUM] = ROW_NUMBER() OVER (PARTITION BY A.ENC_IDN ORDER BY I.INTERACTION_DATE DESC)
INTO #1
FROM JIVA_DWH.dbo.kv_V_MODEL_MBR_ENC_ACTIVITY A /*this is a view*/
JOIN JIVA_DWH.dbo.kv_V_MODEL_EPISODES M /*this is a view*/
ON M.ENC_IDN = A.ENC_IDN
JOIN JIVA_DWH.dbo.kv_V_MODEL_INTERACTIONS I /*this is a view*/
ON I.ENC_IDN = M.ENC_IDN
JOIN #CVG G /*this is a table*/
ON G.mbr_idn = A.MBR_IDN
LEFT JOIN #QM MP /*this is a table*/
ON G.ext_cvg_id = MP.MEMBER_SOURCE_ID COLLATE DATABASE_DEFAULT
WHERE A.ACTIVITY IN ( 'Verbal consent to be received', 'Incoming Call', 'Initial outreach Call', 'Contact Member' )
AND M.EPISODE_TYPE_CD = 'ECM'
AND I.INTERACTION_DATE
BETWEEN @StartDate AND @EndDate
AND CONVERT(DATE, [M].[EPISODE_START_DATE_UTC] + GETDATE() - GETUTCDATE())
BETWEEN @StartDate AND @EndDate; /*I declear this variable on the top*/
I also tried create a temp table and store "JIVA_DWH.dbo.kv_V_MODEL_MBR_ENC_ACTIVITY" but it took 6min to load So im high sus its because the view itself.我还尝试创建一个临时表并存储“JIVA_DWH.dbo.kv_V_MODEL_MBR_ENC_ACTIVITY”,但加载需要 6 分钟所以我很高兴,因为视图本身。
What should i do to optimize the query?我应该怎么做才能优化查询? Much appreciate!!!!非常感谢!!!!
The views you are using were probably designed for a specific reason.您使用的视图可能是出于特定原因而设计的。 There may be more there than you need.那里可能比你需要的更多。 You might try reading the definitions of your 3 views and using only what you need:您可以尝试阅读 3 个视图的定义并仅使用您需要的内容:
DECLARE @StartDate date
DECLARE @EndDate date
SET @StartDate = getdate()
SET @EndDate = DATEADD(year, 1, getdate())
DROP TABLE IF EXISTS #1;
WITH
my_kv_V_MODEL_MBR_ENC_ACTIVITY AS (
<simplified code from kv_V_MODEL_MBR_ENC_ACTIVITY view>
WHERE <tbl>.ACTIVITY IN ( 'Verbal consent to be received', 'Incoming Call', 'Initial outreach Call', 'Contact Member' )
),
my_kv_V_MODEL_EPISODES AS (
<simplified code from kv_V_MODEL_EPISODES view>
WHERE <tbl>.EPISODE_TYPE_CD = 'ECM'
AND CONVERT(DATE, [M].[EPISODE_START_DATE_UTC] + GETDATE() - GETUTCDATE())
BETWEEN @StartDate AND @EndDate
),
my_kv_V_MODEL_INTERACTIONS AS (
<simplified code from kv_V_MODEL_INTERACTIONS view>
WHERE <tbl>.INTERACTION_DATE
BETWEEN @StartDate AND @EndDate
)
SELECT G.ext_cvg_id MemberSourceId,
A.MBR_IDN,
I.ENC_IDN,
I.INTRACN_IDN,
A.ACTIVITY,
A.ACTIVITY_TYPE,
A.UPDATED_DATE,
A.ACTIVITY_STATUS,
A.SCHEDULED_DATE,
I.INTERACTION_DATE,
I.INTERACTION_OUTCOME,
I.INTERACTION_STATUS,
I.MODIFIED_USER,
M.STATUS_CHANGE_DATE,
M.EPISODE_STATUS,
MP.ALTERNATE_ID,
[ROW_NUM] = ROW_NUMBER() OVER (PARTITION BY A.ENC_IDN ORDER BY I.INTERACTION_DATE DESC)
INTO #1
FROM my_kv_V_MODEL_MBR_ENC_ACTIVITY A
JOIN my_kv_V_MODEL_EPISODES M ON M.ENC_IDN = A.ENC_IDN
JOIN my_kv_V_MODEL_INTERACTIONS I ON I.ENC_IDN = M.ENC_IDN
JOIN JIVA_DWH.dbo.mbr_cvg G ON G.mbr_idn = A.MBR_IDN
LEFT JOIN ODS.dbo.QNXT_MEMBER MP ON G.ext_cvg_id = MP.MEMBER_SOURCE_ID COLLATE DATABASE_DEFAULT
Also, upon inspecting the 3 views, you may discover that they share some commonality.此外,在检查这 3 个视图时,您可能会发现它们有一些共同点。 Perhaps you are asking the database server to unnecessarily perform the same steps several times.也许您要求数据库服务器多次不必要地执行相同的步骤。 It may be better to avoid using the views and write your query using only the source tables.最好避免使用视图并仅使用源表编写查询。
One thing it would be good for potential answerers to know is how many rows are in each table or output by each view.潜在回答者最好知道每个表中有多少行或每个视图有多少行 output。
When trying to optimize the query I would recommend to use a tool in SQL Server Management Studio.在尝试优化查询时,我建议使用 SQL Server Management Studio 中的工具。
When running the query on the actual database activate the option "Include Actual Execution Plan".在实际数据库上运行查询时,激活选项“包括实际执行计划”。
This gives you 2 benefits:这给你 2 个好处:
Please note, that I recommended a procedure rather than the outcome of such a procedure, because the results also depend on the amount of data in the tables and other factors which are difficult to post in a forum.请注意,我推荐的是一个过程而不是这样一个过程的结果,因为结果还取决于表格中的数据量和其他难以在论坛中发布的因素。 I hope it helps.我希望它有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.