我有一个查询连接2个表8次,每个表有WHERE
子句中的很多条件。该查询还包括数字列上的ORDER BY
子句。返回需要12秒,这太长了,我需要加快速度。令人惊讶的是,我发现如果删除ORDER BY
子句则需要2秒钟。为什么订单会产生如此巨大的差异以及如何优化它?我正在使用SQL Server 2008 R2。非常感谢。
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
答案 0 :(得分:0)
您只是选择了TOP 1,因此假设cq.sequence_id是唯一的,则无需进一步订购:
SELECT TOP 1
...
ORDER BY cq.sequence_id DESC
答案 1 :(得分:0)
我真的很想知道如果你在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