选择查询执行时间太长(SQL Server 2008)

时间:2013-07-15 11:05:36

标签: sql sql-server-2008 group-by max

我上周尝试运行此查询,但在7小时后它仍在执行,我不得不在当天结束时取消。它只返回了800多行,最终结果应该是20,000行。任何关于提高性能的建议都非常受欢迎。

非常感谢。

Select 
    a.entryID, g.GroupName, 
   MAX(CASE WHEN e.attributedefinitionid = 15 THEN e.AttributeValue END) AS EnglishWording, 
   MAX(CASE WHEN e.attributedefinitionid = 41 THEN e.AttributeValue END) AS GermanWording, 
   MAX(CASE WHEN e.attributedefinitionid = 64 THEN e.AttributeValue END) AS RussianWording,
   MAX(CASE WHEN e.attributedefinitionid = 91 THEN e.AttributeValue END) AS FrenchWording, 
   MAX(CASE WHEN e.attributedefinitionid = 143 THEN e.AttributeValue END) AS ItalianWording, 
   MAX(CASE WHEN e.attributedefinitionid = 160 THEN e.AttributeValue END) AS SpanishWording,
   MAX(CASE WHEN e.attributedefinitionid = 165 THEN e.AttributeValue END) AS ChineseWording,
   MAX(CASE WHEN e.attributedefinitionid = 170 THEN e.AttributeValue END) AS JapaneseWording,
   MAX(CASE WHEN h.attributedefinitionid = 17 
              THEN convert(varchar(10), h.AttributeValue, 120) 
       END) as DateA, 
   MAX(CASE WHEN h.attributedefinitionid = 557 
              THEN convert(varchar(10), h.AttributeValue, 120) 
       END) as DateO,
   MAX(CASE WHEN h.attributedefinitionid = 558 
              THEN convert(varchar(10), h.AttributeValue, 120) 
       END) as DateC,
   MAX(CASE WHEN j.attributedefinitionid = 29 THEN j.AttributeValue END) AS Warning,
   MAX(CASE WHEN l.attributedefinitionid = 23 THEN l.AttributeValue END) AS Highlight,
   MAX(CASE WHEN n.attributedefinitionid = 572 THEN n.AttributeValue END) AS Investigations,
   MAX(CASE WHEN p.attributedefinitionid = 30 THEN p.AttributeValue END) AS Comments,
   MAX(CASE WHEN s.attributedefinitionid = 39 THEN s.AttributeValue END) AS USD, 
   MAX(CASE WHEN s.attributedefinitionid = 40 THEN s.AttributeValue END) AS EUR, 
   MAX(CASE WHEN s.attributedefinitionid = 92 THEN s.AttributeValue END) AS GBP, 
   MAX(CASE WHEN s.attributedefinitionid = 450 THEN s.AttributeValue END) AS RBN, 
   MAX(CASE WHEN s.attributedefinitionid = 451 THEN s.AttributeValue END) AS JPY, 
   MAX(CASE WHEN s.attributedefinitionid = 552 THEN s.AttributeValue END) AS HK$,
   MAX(CASE WHEN u.attributeDefinitionID = 142 THEN u.attributeValue END) AS [Status]
From 
   Entry as a
inner join 
   entrycategory as b on b.entryid = a.entryid
inner join 
   category as c on c.categoryid = b.categoryid 
inner join 
   [Group] as g on g.groupID = c.groupID
left outer join 
   EntryAttribute AS d on d.entryID = A.entryid
left outer join 
   attributestring AS e on e.attributeid = d.attributeid
left outer join 
   EntryAttribute as f on f.EntryID = a.EntryID
left outer join 
   AttributeDateTime as h on h.AttributeID = f.AttributeID
left outer join 
   EntryAttribute as i on i.EntryID = a.EntryID
left outer join 
   AttributeString as j on j.AttributeID = i.AttributeID
left outer join 
   EntryAttribute as k on k.EntryID = a.EntryID
left outer join 
   AttributeString as l on l.AttributeID = k.AttributeID
left outer join 
   EntryAttribute as m on m.EntryID = a.EntryID
left outer join 
   AttributeString as n on n.AttributeID = m.AttributeID
left outer join 
   EntryAttribute as o on o.EntryID = a.EntryID
left outer join 
   AttributeString as p on p.AttributeID = o.AttributeID
inner join 
   EntryAttribute AS r on r.entryID = A.entryid
inner join 
   attributestring AS s on s.attributeid = r.attributeid
left outer join 
   EntryAttribute as t on t.EntryID = a.EntryID
left outer join 
   AttributeString as u on u.AttributeID = t.AttributeID
Group by 
   a.entryID, g.GroupName

1 个答案:

答案 0 :(得分:4)

据我所知,您只需要加入EntryAttributeAttributeString表。聚合处理其余部分。

Select a.entryID,g.GroupName, 
MAX(CASE WHEN e.attributedefinitionid = 15 THEN e.AttributeValue END) AS EnglishWording, 
MAX(CASE WHEN e.attributedefinitionid = 41 THEN e.AttributeValue END) AS GermanWording, 
MAX(CASE WHEN e.attributedefinitionid = 64 THEN e.AttributeValue END) AS RussianWording,
MAX(CASE WHEN e.attributedefinitionid = 91 THEN e.AttributeValue END) AS FrenchWording, 
MAX(CASE WHEN e.attributedefinitionid = 143 THEN e.AttributeValue END) AS ItalianWording, 
MAX(CASE WHEN e.attributedefinitionid = 160 THEN e.AttributeValue END) AS SpanishWording,
MAX(CASE WHEN e.attributedefinitionid = 165 THEN e.AttributeValue END) AS ChineseWording,
MAX(CASE WHEN e.attributedefinitionid = 170 THEN e.AttributeValue END) AS JapaneseWording,

MAX(CASE WHEN e.attributedefinitionid = 17 THEN convert(varchar(10),e.AttributeValue,120) END) as DateA, 
MAX(CASE WHEN e.attributedefinitionid = 557 THEN convert(varchar(10),e.AttributeValue,120) END) as DateO,
MAX(CASE WHEN e.attributedefinitionid = 558 THEN convert(varchar(10),e.AttributeValue,120) END) as DateC,

MAX(CASE WHEN e.attributedefinitionid = 29 THEN e.AttributeValue END) AS Warning,
MAX(CASE WHEN e.attributedefinitionid = 23 THEN e.AttributeValue END) AS Highlight,
MAX(CASE WHEN e.attributedefinitionid = 572 THEN e.AttributeValue END) AS Investigations,
MAX(CASE WHEN e.attributedefinitionid = 30 THEN e.AttributeValue END) AS Comments,

MAX(CASE WHEN e.attributedefinitionid = 39 THEN e.AttributeValue END) AS USD, 
MAX(CASE WHEN e.attributedefinitionid = 40 THEN e.AttributeValue END) AS EUR, 
MAX(CASE WHEN e.attributedefinitionid = 92 THEN e.AttributeValue END) AS GBP, 
MAX(CASE WHEN e.attributedefinitionid = 450 THEN e.AttributeValue END) AS RBN, 
MAX(CASE WHEN e.attributedefinitionid = 451 THEN e.AttributeValue END) AS JPY, 
MAX(CASE WHEN e.attributedefinitionid = 552 THEN e.AttributeValue END) AS HK$,
MAX(CASE WHEN e.attributeDefinitionID = 142 THEN e.attributeValue END) AS [Status]

From Entry as a
inner join entrycategory as b on b.entryid = a.entryid
inner join category as c on c.categoryid= b.categoryid 
inner join [Group] as g on g.groupID = c.groupID
left outer join EntryAttribute AS d on d.entryID = A.entryid
left outer join attributestring AS e on e.attributeid = d.attributeid
Group by a.entryID,g.GroupName;

通过多次加入,您将在每个entryIdGroupName组中生成大量的笛卡尔积。