Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
PIVOT running very slow
Message
From
03/11/2006 13:01:51
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
Other
Title:
PIVOT running very slow
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01166964
Message ID:
01166964
Views:
97
Hi gang. Would you test this also and confirm or say where I'm going wrong. Issue is related with this topic on MS forums:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=888543&SiteID=1&mode=1

Got no reply there and trying my chance here.
--Run in SSMS against AdventureWorks sample database
SELECT TerritoryId,
[2003-01],[2003-02],[2003-03],[2003-04],[2003-05],[2003-06],
[2003-07],[2003-08],[2003-09],[2003-10],[2003-11],[2003-12],
[2004-01],[2004-02],[2004-03],[2004-04],[2004-05],[2004-06],
[2004-07]
from ( select TerritoryId,
   CONVERT(CHAR(7), h.OrderDate, 120) AS theDate, d.LineTotal
   FROM Sales.SalesOrderHeader h JOIN Sales.SalesOrderDetail d ON h.SalesOrderID = d.SalesOrderID
) p
pivot (
sum(LineTotal) for theDate in ([2003-01],[2003-02],[2003-03],[2003-04],[2003-05],[2003-06],
[2003-07],[2003-08],[2003-09],[2003-10],[2003-11],[2003-12],
[2004-01],[2004-02],[2004-03],[2004-04],[2004-05],[2004-06],[2004-07])
) pvt
order by TerritoryId
Inner select runs in 390 milliseconds. However full SQL with pivot takes 5187 milliseconds. Repeated runs might change it a few milliseconds but the general timing stands.

Now if I instead simply ask for the inner SQL from VFP and pivot myself then tmings are:

For same period range used in sample (less months):
Got data-elapsed:457
XTabbed-total elapsed:729

For all 37 months:
Got data-elapsed:455
XTabbed-total elapsed:741

Question is where do I go wrong in PIVOT syntax?

PS: Creating a CLR procedure to generate month column names insetad of hard coding, and limiting the period of inner select didn't help but even if it did, is not the main question here.
I used my own xtabbing routine not VFP _xtab. If you need it I can provide. TIA
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Next
Reply
Map
View

Click here to load this message in the networking platform