THE SQL Server Blog Spot on the Web

Welcome to - The SQL Server blog spot on the web Sign in | |
in Search

Alexander Kuznetsov

PIVOting dense data may speed up your queires

Of course, PIVOting uses up some CPU. However, if the data is dense (all the cells in the pivoted result set are not NULL), then the size of the pivoted result set may be significantly less. As a result,  the overall time to retrieve a pivoted result set and transmit it over the network may be less. Here are my benchmarks.

Let us create a helper table with 1M numbers:

INSERT INTO dbo.Numbers
SET @i=0;
WHILE @i<21 
FROM dbo.Numbers;
SET @i @i 1;

Let us create and populate an typical EAV table:

CREATE TABLE dbo.ObjectAttributes(ObjectID INT NOT NULL, AttributeID INT NOT NULL, IntValue INT NOT NULL,
CONSTRAINT PK_ObjectAttributes PRIMARY KEY(ObjectIDAttributeID));
TRUNCATE TABLE dbo.ObjectAttributes;
INSERT INTO dbo.ObjectAttributes(ObjectIDAttributeIDIntValue)
SELECT n1.nn2.nn1.n
FROM dbo.Numbers AS n1
dbo.Numbers AS n2
WHERE n1.n <100001 
AND n2.n<11;

Let us select all the data from it as is a couple of times:

FROM dbo.ObjectAttributes;

FROM dbo.ObjectAttributes;

 SQL Server Execution Times:
   CPU time = 157 ms,  elapsed time = 8369 ms.
 SQL Server Execution Times:
   CPU time = 266 ms,  elapsed time = 5935 ms.

Let us select pivoted data:

SELECT ObjectID[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]
FROM dbo.ObjectAttributes
MAX(IntValueFOR AttributeID IN([1],[2],[3],[4],[5],[6],[7],[8],[9],[10])) AS pvt
ORDER BY pvt.ObjectID;

Although the pivoted query used more CPU, the overall time was substantially less:

SQL Server Execution Times:
   CPU time = 1219 ms,  elapsed time = 1794 ms.

I was inspired by the following Aaron's post:

What is so bad about EAV, anyway?







Published Friday, November 20, 2009 1:53 PM by Alexander Kuznetsov

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS


No Comments

Leave a Comment


About Alexander Kuznetsov

Alex Kuznetsov has been working with object oriented languages, mostly C# and C++, as well as with databases for more than a decade. He has worked with Sybase, SQL Server, Oracle and DB2. He regularly blogs on, mostly about database unit testing, defensive programming, and query optimization. Alex has written a book entitled "Defensive Database Programming with Transact-SQL" and several articles on and Currently he works at DRW Trading Group in Chicago, where he leads a team of developers, practicing agile development, defensive programming, TDD, and database unit testing.

This Blog


Powered by Community Server (Commercial Edition), by Telligent Systems
  Privacy Statement