2014-11-14 79 views
2

我想弄清楚哪種方式更有效率。我試圖使用Sql小提琴,但它似乎沒有識別PIVOT或UNION 我可以查看哪些工具或統計數據來確定哪些更有效。樞軸VS案例T-sql效率

CREATE TABLE T 
(
    PersonNum INT 
,WeekOf DATETIME 
,ActivityType1 INT 
,ActivityType2 INT 
,Hours INT 
) 

INSERT INTO T VALUES(1,'11/2/2014',5,0,40) 
INSERT INTO T VALUES(1,'11/2/2014',1,0,5) 
INSERT INTO T VALUES(1,'11/2/2014',1,0,8) 
INSERT INTO T VALUES(1,'11/2/2014',2,1,6) 
INSERT INTO T VALUES(1,'11/2/2014',2,2,2) 
INSERT INTO T VALUES(1,'11/2/2014',2,3,9) 
INSERT INTO T VALUES(1,'11/2/2014',2,4,7) 

INSERT INTO T VALUES(1,'11/9/2014',5,0,40) 
INSERT INTO T VALUES(1,'11/9/2014',1,0,2) 
INSERT INTO T VALUES(1,'11/9/2014',1,0,6) 
INSERT INTO T VALUES(1,'11/9/2014',2,1,7) 
INSERT INTO T VALUES(1,'11/9/2014',2,2,2) 
INSERT INTO T VALUES(1,'11/9/2014',2,3,3) 
INSERT INTO T VALUES(1,'11/9/2014',2,4,5) 

方法1

SELECT 
    PersonNum 
, WeekOf 
, SUM(CASE WHEN ActivityType1 = 5 THEN Hours ELSE 0 END) AS Beginning 
, SUM(CASE WHEN ActivityType1 = 1 THEN Hours ELSE 0 END) AS Plus 
, SUM(CASE WHEN ActivityType1 = 2 AND ActivityType2 <> 3 THEN Hours ELSE 0 END) AS Minus 
, SUM(CASE WHEN ActivityType1 = 2 AND ActivityType2 = 3 THEN Hours ELSE 0 END) AS MinusSpecial 
FROM T 
GROUP BY 
    PersonNum 
, WeekOf 

方法2

SELECT 
    PersonNum 
, WeekOf 
, [Beginning] 
, [Plus] 
, [Minus] 
, [Minus Special] 
FROM 
(
    SELECT 
    PersonNum 
    , WeekOf 
    , 'Beginning' AS ColumnType 
    , Hours 
    FROM T 
    WHERE 
    ActivityType1 = 5 
    UNION 
    SELECT 
    PersonNum 
    , WeekOf 
    , 'Plus' AS ColumnType 
    , Hours 
    FROM T 
    WHERE 
    ActivityType1 = 1 
    UNION 
    SELECT 
    PersonNum 
    , WeekOf 
    , 'Minus' AS ColumnType 
    , Hours 
    FROM T 
    WHERE 
     ActivityType1 = 2 
    AND ActivityType2 <> 3 
    UNION 
    SELECT 
    PersonNum 
    , WeekOf 
    , 'Minus Special' AS ColumnType 
    , Hours 
    FROM T 
    WHERE 
     ActivityType1 = 2 
    AND ActivityType2 = 3 
    ) Data 
PIVOT 
(SUM(Hours) 
FOR ColumnType IN ([Beginning]     
        ,[Plus] 
        ,[Minus] 
        ,[Minus Special]) 
) pvt 
+1

看看這篇文章。它涵蓋了執行此類查詢的不同方式並探討了性能。 http://www.sqlservercentral.com/articles/T-SQL/63681/ – 2014-11-14 19:29:17

回答

1

你可以做到這一點

set statistics time on 

--first query 

set statistics time off 

set statistics time on 

--second query 

set statistics time off 

你可以看一下在消息窗口中看到執行時間

+0

哇,這個數據的速度大約慢了3.5倍 – Mike 2014-11-14 19:09:01

1

方法1肯定會更快。這是因爲只有一條select語句。另一方面,方法2中有5條select語句。 一般規則是,查詢中包含的語句越多,執行的時間就越長。

5

我可以建議重寫你的PIVOT查詢,所以你不必打表5次。

select P.PersonNum, 
     P.WeekOf, 
     P.Beginning, 
     P.Plus, 
     P.Minus, 
     P.MinusSpecial 
from (
    select T.PersonNum, 
      T.WeekOf, 
      T.Hours, 
      case ActivityType1 
      when 5 then 'Beginning' 
      when 1 then 'Plus' 
      when 2 then case ActivityType2 
          when 3 then 'MinusSpecial' 
          else 'Minus' 
         end 
      end as ColumnType 
    from T 
    ) as T 
pivot (sum(T.Hours) for T.ColumnType in (Beginning, Plus, MinusSpecial, Minus)) as P 

因此,您可能會通過查詢和大致相同的性能獲得與您的組相同的查詢計劃。