Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Pivot table query performance?

Author  Topic 

aiken
Aged Yak Warrior

525 Posts

Posted - 2002-11-22 : 17:06:36
I found the pivot table article very interesting, and decided to implement it on a page that gives web pageview statistics. The page had previously iterated through each hour for each server and issued 24*[number of servers] queries to the database.

Much to my surprise, performance is far worse with the new pivot table query.

It's a very simple schema:

CREATE TABLE servers (i int identity, name varchar(15))

CREATE TABLE log_running (i int, pagename varchar(50), i_servers int, datetime smalldatetime)


...and here's the pivot table query (which usually lives in an SP):

SELECT servers.name,
SUM(CASE WHEN DATEPART(hour,datetime) = 0 THEN 1 ELSE 0 END) AS '12am',
SUM(CASE WHEN DATEPART(hour,datetime) = 1 THEN 1 ELSE 0 END) AS '1am',
SUM(CASE WHEN DATEPART(hour,datetime) = 2 THEN 1 ELSE 0 END) AS '2am',
SUM(CASE WHEN DATEPART(hour,datetime) = 3 THEN 1 ELSE 0 END) AS '3am',
SUM(CASE WHEN DATEPART(hour,datetime) = 4 THEN 1 ELSE 0 END) AS '4am',
SUM(CASE WHEN DATEPART(hour,datetime) = 5 THEN 1 ELSE 0 END) AS '5am',
SUM(CASE WHEN DATEPART(hour,datetime) = 6 THEN 1 ELSE 0 END) AS '6am',
SUM(CASE WHEN DATEPART(hour,datetime) = 7 THEN 1 ELSE 0 END) AS '7am',
SUM(CASE WHEN DATEPART(hour,datetime) = 8 THEN 1 ELSE 0 END) AS '8am',
SUM(CASE WHEN DATEPART(hour,datetime) = 9 THEN 1 ELSE 0 END) AS '9am',
SUM(CASE WHEN DATEPART(hour,datetime) = 10 THEN 1 ELSE 0 END) AS '10am',
SUM(CASE WHEN DATEPART(hour,datetime) = 11 THEN 1 ELSE 0 END) AS '11am',
SUM(CASE WHEN DATEPART(hour,datetime) = 12 THEN 1 ELSE 0 END) AS '12pm',
SUM(CASE WHEN DATEPART(hour,datetime) = 13 THEN 1 ELSE 0 END) AS '1pm',
SUM(CASE WHEN DATEPART(hour,datetime) = 14 THEN 1 ELSE 0 END) AS '2pm',
SUM(CASE WHEN DATEPART(hour,datetime) = 15 THEN 1 ELSE 0 END) AS '3pm',
SUM(CASE WHEN DATEPART(hour,datetime) = 16 THEN 1 ELSE 0 END) AS '4pm',
SUM(CASE WHEN DATEPART(hour,datetime) = 17 THEN 1 ELSE 0 END) AS '5pm',
SUM(CASE WHEN DATEPART(hour,datetime) = 18 THEN 1 ELSE 0 END) AS '6pm',
SUM(CASE WHEN DATEPART(hour,datetime) = 19 THEN 1 ELSE 0 END) AS '7pm',
SUM(CASE WHEN DATEPART(hour,datetime) = 20 THEN 1 ELSE 0 END) AS '8pm',
SUM(CASE WHEN DATEPART(hour,datetime) = 21 THEN 1 ELSE 0 END) AS '9pm',
SUM(CASE WHEN DATEPART(hour,datetime) = 22 THEN 1 ELSE 0 END) AS '10pm',
SUM(CASE WHEN DATEPART(hour,datetime) = 23 THEN 1 ELSE 0 END) AS '11pm'

FROM log_running
JOIN servers on servers.i=log_running.iServers
WHERE datetime>=dateadd(hour,-24,getdate())
GROUP by servers.name
ORDER by servers.name


...as you can see, it gives a running 24-hour window onto pageviews per server per hour. The query works fine, but *damn* is it slow.

The main query cost seems to be coming from a clustered index scan (the clustered index being on the 'datetime' column of log_running).

Is there any reasonable way to speed this up? Am I missing something?

Thanks
-b

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2002-11-22 : 17:14:21
Look at my post at the bottom of the article discussion:

http://sqlteam.com/forums/topic.asp?TOPIC_ID=8961


It may speed things up a little, at least. Not sure though. Didn't try. Makes it easier to read/maintain anyway !

Also, adding a redundant indexed "Hour" field to the raw data would do the trick of speeding up the queries quite a bit, I would imagine. It could be a nice small 1 byte field well worth adding in.

- Jeff

Edited by - jsmith8858 on 11/22/2002 17:17:01
Go to Top of Page
   

- Advertisement -