2013-07-15 150 views
0

上週我試圖運行這個查詢,但在7小時後它仍在執行,我不得不在一天結束時取消。它只返回了800多行,最終結果應該是20000行。任何提高性能的建議都非常值得讚賞。選擇執行時間太長的查詢(SQL Server 2008)

非常感謝。

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 

回答

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組內產生humungous笛卡爾乘積。

+0

aaah是的,它現在執行不到一分鐘!非常感謝。我有很多NULL返回。我可以排除這些,只是返回NULL空白? – Jasmine

+0

@Jasmine。 。 。要返回空白而不是'NULL',請爲所有'case'語句添加一個'else''子句。 –

+0

我想添加一個新的字段到這個查詢,我希望字段返回是,而不是實際的值,當發現匹配。所以如果存在,我希望它只是返回Yes。但是,如果我將它作爲一個未集合的字段添加,那麼我需要添加到Group by子句中,而我不想那樣做。 (從EntryAttribute中選擇*作爲內部連接AttributeString作爲a.AttributeID = e.AttributeID,其中e.entryID = x.entryID和a.AttributeDefinitionID = 44)從Entry中選擇不同的x.entryID,a.attributeValue。 – Jasmine