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
 General SQL Server Forums
 New to SQL Server Programming
 Query performance trouble

Author  Topic 

HappyMelon
Starting Member

6 Posts

Posted - 2013-06-27 : 16:19:12
Hi!

I have a query that is performing poorly and that I would like to rewrite in a more efficient way. What I want is to calculate some statistics. Below is some example code illustrating the problem:

CREATE TABLE #Temp
( ID INT,
ProductID int,
Quantity int
)
INSERT INTO #Temp
VALUES
( 1, 1, 23),
( 2, 1, 33),
( 3, 1, 6),
( 4, 1, 7),
( 5, 2, 15),
( 6, 2, 6),
( 7, 2, 13),
( 8, 3, 21),
( 9, 3, 16),
( 10, 4, 3),
( 11, 4, 11),
( 12, 4, 9),
( 13, 4, 14)

SELECT ProductID,
COUNT(*)
FROM #Temp
WHERE Quantity < 10
GROUP BY ProductID

SELECT ProductID,
COUNT(*)
FROM #Temp
WHERE Quantity < 15
GROUP BY ProductID

SELECT ProductID,
COUNT(*)
FROM #Temp
WHERE Quantity < 20
GROUP BY ProductID

SELECT ProductID,
COUNT(*)
FROM #Temp
WHERE Quantity < 25
GROUP BY ProductID

DROP TABLE #Temp

It must be possible to rewrite this in a more efficient way?

Appreciating any input on this

/HappyMelon

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-06-27 : 16:36:07
You can rewrite the multiple selects in one query - that will speed it up. If you still need performance improvement, add an index on Quantity
SELECT
ProductId,
SUM(CASE WHEN quantity < 10 THEN 1 ELSE 0 END) AS LessThan10,
SUM(CASE WHEN quantity < 15 THEN 1 ELSE 0 END) AS LessThan15,
SUM(CASE WHEN quantity < 20 THEN 1 ELSE 0 END) AS LessThan20,
SUM(CASE WHEN quantity < 25 THEN 1 ELSE 0 END) AS LessThan25
FROM
#temp
WHERE
quantity < 25
GROUP BY
ProductId;
Editing: On second thoughts, I don't think the index on quantity will improve performance significantly, unless most of your quantities were greater than or equal to 25.
Go to Top of Page

HappyMelon
Starting Member

6 Posts

Posted - 2013-06-27 : 16:50:08
That is what I was looking for!
Thank you James for the help :)

/HappyMelon
Go to Top of Page

nevzab
Starting Member

34 Posts

Posted - 2013-06-27 : 18:05:55
James,

Can you explain what you mean by adding an index in this context please? I am new to SQL and indexing is a concept I understand in terms of speeding up queries but I wouldn't know how to implement an index.

Cheers,

Nev

Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-06-28 : 06:33:54
quote:
Originally posted by nevzab

James,

Can you explain what you mean by adding an index in this context please? I am new to SQL and indexing is a concept I understand in terms of speeding up queries but I wouldn't know how to implement an index.

Cheers,

Nev





With the assumption that you know what an index is and how it works, James suggestion in this context was to create an index (if not already) on the quantity column only if the ratio of number of records < 25 compared to >=25 is some how 10:90 or 20:80 or 30:90 would make the query faster, but if the ratio is between <25 and >=25 is 51:49 or 60:40 or ... then instead optimizing it will inverse affect.

You can find several good articles on google explaining how/when to create indexes.

Cheers
MIK
Go to Top of Page
   

- Advertisement -