我正在寻求如何优化此查询的想法。虽然我已经评估了执行计划,但它并没有提供任何遗漏索引的想法,所以我很好奇是否编写更好的查询(采用不同策略)会导致更快/更轻的查询。
SELECT [Place], COUNT([Place])
FROM (
SELECT scoresid, REPLACE(REPLACE(EventPlace1,'T', ''),'*','') [Place]
FROM [MS.Prod]..mso_scores UNION
SELECT scoresid, REPLACE(REPLACE(EventPlace2,'T', ''),'*','')
FROM [MSO.Prod]..mso_scores UNION
SELECT scoresid, REPLACE(REPLACE(EventPlace3,'T', ''),'*','')
FROM [MSO.Prod]..mso_scores UNION
SELECT scoresid, REPLACE(REPLACE(EventPlace4,'T', ''),'*','')
FROM [MSO.Prod]..mso_scores UNION
SELECT scoresid, REPLACE(REPLACE(EventPlace5,'T', ''),'*','')
FROM [MSO.Prod]..mso_scores UNION
SELECT scoresid, REPLACE(REPLACE(EventPlace6,'T', ''),'*','')
FROM [MSO.Prod]..mso_scores UNION
SELECT scoresid, REPLACE(REPLACE(AAPlace,'T', ''),'*','')
FROM [MSO.Prod]..mso_scores
) data1
JOIN [MSO.Prod]..mso_scores scores ON scores.scoresid = data1.scoresid
AND scores.usagnum = '274246'
AND scores.TeamResult='N'
WHERE data1.Place IN ('1', '2', '3')
GROUP BY Place
简单解释一下:有6个事件场地字段。这些字段中的数据看起来像"1"、"2"、"1T"、"3"、"5T",其中"T"表示并列。我只关心数字1、2、3,因此从场地中解析出"T"或"*",然后对查询进行分组计数。
他们有多少个第一名,多少个第二名,以此类推。
usagnum
和TeamResult
列上添加索引,并将EventPlace1
、EventPlace2
、...、AAPlace
列作为包含在索引中。请同时指定您的服务器版本和表的DDL
。 - DevartSET STATISTICS IO ON;
。我还注意到你没有聚集索引。是这样吗? - Devart