Sql server 由于ORDER BY子句和内部联接,SQL查询的性能不佳

Sql server 由于ORDER BY子句和内部联接,SQL查询的性能不佳,sql-server,Sql Server,在WHERE子句中,我有一个查询将两个表连接了8次,每个表都有很多条件。查询还包括数字列上的ORDER BY子句。返回需要12秒,这太长了,我需要加快速度。令人惊讶的是,我发现如果删除orderby子句,需要2秒钟。为什么order by会产生如此巨大的差异,以及如何对其进行优化?我正在使用SQLServer2008R2。非常感谢 select top 1 cq.answer AS RM_comment ,su.user_name

WHERE
子句中,我有一个查询将两个表连接了8次,每个表都有很多条件。查询还包括数字列上的ORDER BY子句。返回需要12秒,这太长了,我需要加快速度。令人惊讶的是,我发现如果删除
orderby
子句,需要2秒钟。为什么order by会产生如此巨大的差异,以及如何对其进行优化?我正在使用SQLServer2008R2。非常感谢

select top 1   
 cq.answer                              AS RM_comment
,su.user_name                           AS RM_name
,cq.execution_date                      AS RM_date

,cq1.answer                             AS UHB_comment
,su1.user_name                          AS UHB_name
,cq1.execution_date                     AS UHB_date

,cq2.answer                             AS HB_comment
,su2.user_name                          AS HB_name
,cq2.execution_date                     AS HB_date

,cq3.answer                             AS UHCR_comment
,su3.user_name                          AS UHCR_name
,cq3.execution_date                     AS UHCR_date

,cq4.answer                             AS CRM_comment
,su4.user_name                          AS CRM_name
,cq4.execution_date                     AS CRM_date

,cq5.answer                             AS HCR_comment
,cq5Bis.answer                          AS HCR_Comment2
,su5.user_name                          AS HCR_name
,cq5.execution_date                     AS HCR_date

,cq6.answer                             AS CRO_comment
,cq6Bis.answer                          AS CRO_comment2
,su6.user_name                          AS CRO_name
,cq6.execution_date                     AS CRO_date

,cq7.answer                             AS GM_comment
,cq7Bis.answer                          AS GM_comment2
,su7.user_name                          AS GM_name
,cq7.execution_date                     AS GM_date

from collect_question cq

left join collect_question cq1 on (cq.item_id=cq1.item_id and cq1.collect_question=@UHB_CQ 
                                    and cq1.step_name=@UHB_StepName  
                                    )

left join collect_question cq2 on (cq.item_id=cq2.item_id and cq2.collect_question=@HB_CQ 
                                    and cq2.step_name=@HB_StepName  
                                     )                                   

left join collect_question cq3 on (cq.item_id=cq3.item_id and cq3.collect_question=@UHCR_CQ 
                                    and cq3.step_name in (@UHCR_StepName,@UHCR_StepName1)  
                                     )  

left join collect_question cq4 on (cq.item_id=cq4.item_id and cq4.collect_question=@CRM_CQ 
                                    and cq4.step_name=@CRM_StepName  
                                     )  

left join collect_question cq5 on (cq.item_id=cq5.item_id and cq5.collect_question=@HCR_CQ 
                                    and cq5.step_name=@HCR_StepName)
left join collect_question cq5Bis on (cq.item_id=cq5Bis.item_id and cq5Bis.collect_question=@HCR_CQ2 
                                    and cq5Bis.step_name=@HCR_StepName)

left join collect_question cq6 on (cq.item_id=cq6.item_id and cq6.collect_question=@CRO_CQ 
                                    and cq6.step_name=@CRO_StepName)
left join collect_question cq6Bis on (cq.item_id=cq6Bis.item_id and cq6Bis.collect_question=@CRO_CQ2 
                                    and cq6Bis.step_name=@CRO_StepName)

left join collect_question cq7 on (cq.item_id=cq7.item_id and cq7.collect_question=@GM_CQ 
                                    and cq7.step_name=@GM_StepName)
left join collect_question cq7Bis on (cq.item_id=cq7Bis.item_id and cq7Bis.collect_question=@GM_CQ2 
                                    and cq7Bis.step_name=@GM_StepName)                                       


left join software_user su on cq.executed_by=su.user_id
left join software_user su1 on cq1.executed_by=su1.user_id
left join software_user su2 on cq2.executed_by=su2.user_id
left join software_user su3 on cq3.executed_by=su3.user_id
left join software_user su4 on cq4.executed_by=su4.user_id
left join software_user su5 on cq5.executed_by=su5.user_id
left join software_user su6 on cq6.executed_by=su6.user_id
left join software_user su7 on cq7.executed_by=su7.user_id

 where cq.collect_question=@RM_CQ
 and cq.step_name=@RM_StepName 

 and cq.item_id=172325

 Order By     cq.sequence_id desc
             ,cq1.sequence_id desc
             ,cq2.sequence_id desc
             ,cq3.sequence_id desc
             ,cq4.sequence_id desc
             ,cq5.sequence_id desc
             ,cq6.sequence_id desc
             ,cq7.sequence_id desc

您只需选择前1个,因此假设cq.sequence\u id是唯一的,则无需进一步订购:

  SELECT TOP 1 
  ...
  ORDER BY cq.sequence_id DESC

我真的很想知道,如果在CTE SELECT语句下面运行,性能会如何

select top 1 *
from (
select top 1   
 cq.answer                              AS RM_comment
,su.user_name                           AS RM_name
,cq.execution_date                      AS RM_date

,cq1.answer                             AS UHB_comment
,su1.user_name                          AS UHB_name
,cq1.execution_date                     AS UHB_date

,cq2.answer                             AS HB_comment
,su2.user_name                          AS HB_name
,cq2.execution_date                     AS HB_date

,cq3.answer                             AS UHCR_comment
,su3.user_name                          AS UHCR_name
,cq3.execution_date                     AS UHCR_date

,cq4.answer                             AS CRM_comment
,su4.user_name                          AS CRM_name
,cq4.execution_date                     AS CRM_date

,cq5.answer                             AS HCR_comment
,cq5Bis.answer                          AS HCR_Comment2
,su5.user_name                          AS HCR_name
,cq5.execution_date                     AS HCR_date

,cq6.answer                             AS CRO_comment
,cq6Bis.answer                          AS CRO_comment2
,su6.user_name                          AS CRO_name
,cq6.execution_date                     AS CRO_date

,cq7.answer                             AS GM_comment
,cq7Bis.answer                          AS GM_comment2
,su7.user_name                          AS GM_name
,cq7.execution_date                     AS GM_date


,cq.sequence_id sequence_id0
,cq1.sequence_id sequence_id1
,cq2.sequence_id sequence_id2
,cq3.sequence_id sequence_id3
,cq4.sequence_id sequence_id4
,cq5.sequence_id sequence_id5
,cq6.sequence_id sequence_id6
,cq7.sequence_id sequence_id7
from collect_question cq

left join collect_question cq1 on (cq.item_id=cq1.item_id and cq1.collect_question=@UHB_CQ 
                                    and cq1.step_name=@UHB_StepName  
                                    )

left join collect_question cq2 on (cq.item_id=cq2.item_id and cq2.collect_question=@HB_CQ 
                                    and cq2.step_name=@HB_StepName  
                                     )                                   

left join collect_question cq3 on (cq.item_id=cq3.item_id and cq3.collect_question=@UHCR_CQ 
                                    and cq3.step_name in (@UHCR_StepName,@UHCR_StepName1)  
                                     )  

left join collect_question cq4 on (cq.item_id=cq4.item_id and cq4.collect_question=@CRM_CQ 
                                    and cq4.step_name=@CRM_StepName  
                                     )  

left join collect_question cq5 on (cq.item_id=cq5.item_id and cq5.collect_question=@HCR_CQ 
                                    and cq5.step_name=@HCR_StepName)
left join collect_question cq5Bis on (cq.item_id=cq5Bis.item_id and cq5Bis.collect_question=@HCR_CQ2 
                                    and cq5Bis.step_name=@HCR_StepName)

left join collect_question cq6 on (cq.item_id=cq6.item_id and cq6.collect_question=@CRO_CQ 
                                    and cq6.step_name=@CRO_StepName)
left join collect_question cq6Bis on (cq.item_id=cq6Bis.item_id and cq6Bis.collect_question=@CRO_CQ2 
                                    and cq6Bis.step_name=@CRO_StepName)

left join collect_question cq7 on (cq.item_id=cq7.item_id and cq7.collect_question=@GM_CQ 
                                    and cq7.step_name=@GM_StepName)
left join collect_question cq7Bis on (cq.item_id=cq7Bis.item_id and cq7Bis.collect_question=@GM_CQ2 
                                    and cq7Bis.step_name=@GM_StepName)                                       


left join software_user su on cq.executed_by=su.user_id
left join software_user su1 on cq1.executed_by=su1.user_id
left join software_user su2 on cq2.executed_by=su2.user_id
left join software_user su3 on cq3.executed_by=su3.user_id
left join software_user su4 on cq4.executed_by=su4.user_id
left join software_user su5 on cq5.executed_by=su5.user_id
left join software_user su6 on cq6.executed_by=su6.user_id
left join software_user su7 on cq7.executed_by=su7.user_id

 where cq.collect_question=@RM_CQ
 and cq.step_name=@RM_StepName 

 and cq.item_id=172325
 ) t
 Order By
sequence_id0 desc
,sequence_id1 desc
,sequence_id2 desc
,sequence_id3 desc
,sequence_id4 desc
,sequence_id5 desc
,sequence_id6 desc
,sequence_id7 desc

你能发布DDL吗?索引是显而易见的答案。您的查询看起来很糟糕。我想重新设计表就是解决方案。我需要这两个参数才能得到一条记录。最新的一个。这是一个怎样的“CTE选择”?还有,这是一个怎样的答案?它很快,但是顺序是什么(因为内部选择只返回一条记录)?首先应用排序。然后返回排序列表的第一行