我不是一个SQL专家,只是知道我的方式。
我需要压缩一些数据,以便为SQL报告目的生成它。
此处我的查询我不认为是最佳的,因为它很慢,我确信有10种更好的方法可以做到这一点:
此处的核心列是InspectionID,它是Inspections表的主键
有什么建议吗?
SELECT
INSP.HouseSiteNo,
(SELECT TOP 1 WORKFLOWSTATES.Name FROM INSPECTIONWORKFLOWITEM WFITEM
INNER JOIN WORKFLOWSTATES ON WFITEM.WorkflowStateID = WORKFLOWSTATES.WorkflowStateID
WHERE WFITEM.InspectionID = INSP.InspectionID AND WORKFLOWSTATES.StateNumber = 1) as StateName
FROM INSPECTIONS INSP
ORDER BY HouseSiteNo
表格结构如下:
检查:
HouseSiteNo HouseType ProjectID InspectionID
1 A 543C6381-8704-4129-B484-05B02927F690 E061F76F-8998-450B-B08C-DA2817B310A4
2 A 543C6381-8704-4129-B484-05B02927F690 E0D1F3DB-51D4-465A-A0B4-ACFFF181DC56
INSPECTIONWORKFLOWITEM
InspectionID WorkflowStateID DateStarted StateCompleted ID
FAA4E366-1D57-4699-982C-001FF351D717 AB2B7DA0-A90A-46FB-8F4B-8E662191E380 2012-09-09 00:00:00.000 0 D77332CB-D026-409A-814D-5632C109850A
FAA4E366-1D57-4699-982C-001FF351D717 E2AC895A-0986-4A03-B104-A727D41405E3 2012-09-09 00:00:00.000 0 F118FD02-20C4-4B45-8539-6F8BDC6B3868
FAA4E366-1D57-4699-982C-001FF351D717 F0469593-1B6B-4F98-92AE-DF50E76B9ED8 2012-09-09 00:00:00.000 0 8FBEE61B-D7B3-4D13-9FC8-730EB0FE7C95
FAA4E366-1D57-4699-982C-001FF351D717 5929D0CA-E8B0-42EE-9CF4-E3FBC9973AA6 2012-09-09 00:00:00.000 0 02646FE5-B338-4D6F-B5F5-7984B0EF7BF5
FAA4E366-1D57-4699-982C-001FF351D717 ECDDB790-88A4-4654-889D-BCA577C35CA8 2012-09-09 00:00:00.000 0 0B9C26FA-8D9D-4552-A3FE-84D4D21F5791
FAA4E366-1D57-4699-982C-001FF351D717 099D66A4-16F2-4E72-A42A-06B4D6DC0102 2012-09-09 00:00:00.000 0 3B90F113-5194-4ED2-B920-8DA004C9EA5C
FAA4E366-1D57-4699-982C-001FF351D717 66132F53-0A0E-431A-AD84-ADE77656CDF0 2012-09-09 00:00:00.000 0 05223484-8BB3-43A8-A9A8-C8FBC6270186
9AC52A0A-BC2D-4BBD-9585-0028C07ACC27 F0469593-1B6B-4F98-92AE-DF50E76B9ED8 2012-09-09 00:00:00.000 0 A1471AF4-B733-488C-86B4-FB29970E536E
9AC52A0A-BC2D-4BBD-9585-0028C07ACC27 ECDDB790-88A4-4654-889D-BCA577C35CA8 2012-09-09 00:00:00.000 0 30F3A1F9-6E51-4504-A9FD-B32FD2B35B39
9AC52A0A-BC2D-4BBD-9585-0028C07ACC27 5929D0CA-E8B0-42EE-9CF4-E3FBC9973AA6 2012-09-09 00:00:00.000 0 760D4907-ED6C-4D84-AED9-775FBBAE5123
9AC52A0A-BC2D-4BBD-9585-0028C07ACC27 AB2B7DA0-A90A-46FB-8F4B-8E662191E380 2012-09-09 00:00:00.000 0 49D632C7-31D0-4818-AC36-564F157D1959
9AC52A0A-BC2D-4BBD-9585-0028C07ACC27 66132F53-0A0E-431A-AD84-ADE77656CDF0 2012-09-09 00:00:00.000 0 C6771081-198D-475B-BA09-14BB696567DA
9AC52A0A-BC2D-4BBD-9585-0028C07ACC27 099D66A4-16F2-4E72-A42A-06B4D6DC0102 2012-09-09 00:00:00.000 0 F265B140-25C5-46CA-B060-2A27CD9F9D00
9AC52A0A-BC2D-4BBD-9585-0028C07ACC27 E2AC895A-0986-4A03-B104-A727D41405E3 2012-09-09 00:00:00.000 0 E56AEC15-4BA1-4089-B1A7-3BF054568929
WORKFLOWSTATES
WorkflowStateID Name StateNumber
099D66A4-16F2-4E72-A42A-06B4D6DC0102 State 3 3
AB2B7DA0-A90A-46FB-8F4B-8E662191E380 State 6 6
E2AC895A-0986-4A03-B104-A727D41405E3 State 1 1
66132F53-0A0E-431A-AD84-ADE77656CDF0 State 2 2
ECDDB790-88A4-4654-889D-BCA577C35CA8 State 5 5
F0469593-1B6B-4F98-92AE-DF50E76B9ED8 State 7 7
5929D0CA-E8B0-42EE-9CF4-E3FBC9973AA6 State 4 4
答案 0 :(得分:1)
如果您使用的是SQL Server 2005或更高版本,则可以使用cross/outer apply:
SELECT INSP.HouseSiteNo, StateName.Name
FROM INSPECTIONS INSP
CROSS APPLY
(
SELECT TOP 1 WORKFLOWSTATES.Name FROM INSPECTIONWORKFLOWITEM WFITEM
INNER JOIN WORKFLOWSTATES ON WFITEM.WorkflowStateID = WORKFLOWSTATES.WorkflowStateID
WHERE WFITEM.InspectionID = INSP.InspectionID AND WORKFLOWSTATES.StateNumber = 1
) StateName
ORDER BY HouseSiteNo
当我在一个批处理中运行这两个查询时,实际执行计划显示相对于批处理的查询成本是68%而我的是32% - 因此它获得了巨大的性能提升。性能提升是由于缺乏ORDER BY
声明造成的。目前它们都是相对于批次的50%。
答案 1 :(得分:0)
不确定这是否是您正在寻找的。 p>
如果您的查询需要很长时间并且有效,请尝试在分析器中运行它,看看是否有可以添加的索引以提高性能。
SELECT i.HouseSiteNo, w.Name
FROM Inspections i
JOIN InspectionWorkflowItem wfi on i.InspectionID = wfi.InspectionID
JOIN WorkflowStatus w on w.WorkflowStateID = wfi.WorkflowStateID
WHERE w.StateNumber = 1
ORDER BY i.HouseSiteNo
答案 2 :(得分:0)
这是一个潜在的问题。没有独特排序的前1名是非确定性的 即使是具有群集PK的表也不能保证在没有排序的情况下返回相同的前1个。
SELECT TOP 1 WORKFLOWSTATES.Name
FROM INSPECTIONWORKFLOWITEM WFITEM
INNER JOIN WORKFLOWSTATES
ON WFITEM.WorkflowStateID = WORKFLOWSTATES.WorkflowStateID
WHERE WFITEM.InspectionID = INSP.InspectionID
AND WORKFLOWSTATES.StateNumber = 1
SELECT INSP.HouseSiteNo, min(WORKFLOWSTATES.Name) -- at least min is deterministic
FROM FROM INSPECTIONS INSP
JOIN INSPECTIONWORKFLOWITEM WFITEM
ON WFITEM.InspectionID = INSP.InspectionID
JOIN WORKFLOWSTATES
ON WORKFLOWSTATES.WorkflowStateID = WFITEM.WorkflowStateID
AND WORKFLOWSTATES.StateNumber = 1
GROUP BY INSP.HouseSiteNo
ORDER BY INSP.HouseSiteNo