简体   繁体   English

子查询更快地加入

[英]Faster Join on Sub-query

I'm attempting to do a single query for a report that I need and I'm not sure how to get past a speed issue. 我正在尝试对需要的报告进行单个查询,但不确定如何解决速度问题。

Expected Outcome 预期结果
I need to have a single row per patient that lists all of their diagnosis codes in a the same column. 我需要为每位患者提供一行,并在同一列中列出所有诊断代码。 My code does work and gets the job done but it increases my runs which must be done 30 times under different criteria and will make a 5 minute process about 30. 我的代码可以正常工作并完成工作,但是它增加了我的运行速度,在不同条件下必须运行30次,并且需要5分钟才能完成大约30分钟的运行。

Attempted Resolution 尝试解决
I am using the following code to left outer join to. 我正在使用以下代码离开外部联接。

left outer join (Select distinct add2.VisitID, 
substring((Select ', '+add1.Diagnosis  AS [text()]  
From AbsDrgDiagnoses add1 Where add1.VisitID = add2.VisitID 
ORDER BY add1.VisitID,DiagnosisSeqID For XML PATH ('')), 2, 1000) DiagText
From [Livendb].[dbo].[AbsDrgDiagnoses] add2) add3 on diag.VisitID = add3.VisitID  

Outcome 结果
This works but my 9 second query over a month of data with only a filter one 1 of 30 codes raises to 1m 12s. 这行得通,但是我在一个月的数据中仅用一个过滤器(30个代码中的1个)进行了9秒钟的查询,结果提高到了1m 12s。 If I run the query by itself it takes 3m 49s seconds to compile so its an improvement in my main table but I would like to slim this down if possible. 如果我自己运行查询,则编译需要3m 49s秒,因此对我的主表来说是一个改进,但我希望尽可能减少这种情况。

Other Attempted Resolutions 其他尝试的解决方案
I attempted to create a view from the query and use that but received the same run time. 我试图从查询中创建一个视图并使用它,但是得到了相同的运行时间。

I also attached SourceID which is always the same value but my 8 tables use this in their index but it actually slightly increased my time. 我还附加了SourceID,它始终是相同的值,但是我的8个表在其索引中使用了此值,但实际上实际上增加了我的时间。

Conclusion 结论
The table that I need to merge contains around 30 million rows which is most likely the issue and there is no way around the increased time, but I'm hoping someone may have a trick that could help me decrease this time. 我需要合并的表包含大约3000万行,这很可能是问题所在,并且无法解决增加时间的问题,但是我希望有人可以通过一些技巧来减少这次的时间。

This is your subquery: 这是您的子查询:

(Select distinct add2.VisitID,
       substring((Select ', '+add1.Diagnosis  AS [text()]  
                  From AbsDrgDiagnoses add1
                  Where add1.VisitID = add2.VisitID 
                  order by add1.VisitID,DiagnosisSeqID
                  For XML PATH ('')
                 ), 2, 1000) DiagText
  From [Livendb].[dbo].[AbsDrgDiagnoses] add2
) add3
on diag.VisitID = add3.VisitID 

Let me assume that when you remove it, the query is fast. 让我假设删除它时,查询速度很快。

I think you would be better off with outer apply : 我认为您最好使用outer apply

outer apply
    (select stuff((Select ', ' + add1.Diagnosis as [text()]  
                   From AbsDrgDiagnoses add
                   Where diag.VisitID = add.VisitID 
                   order by DiagnosisSeqID
                   For XML PATH ('')
                  ), 1, 2, '') DiagText
    ) add3

I can't imagine that the second level of subqueries actually helps performance. 我无法想象第二级子查询实际上可以提高性能。

And, speaking of performance, you can use an index on AbsDrgDiagnoses(VisitID, DiagnosisSeqID, Diagnosis) . 而且,谈到性能,您可以在AbsDrgDiagnoses(VisitID, DiagnosisSeqID, Diagnosis)上使用索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM