SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to optimize this query


How to optimize this query

Author
Message
mabud_ncc
mabud_ncc
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 71
I have a query for a report which is basically aggregating some data from different tables as following:
/*******************************************************/

WITH CAT AS

(SELECT DISTINCT
c.cat_id,
c.name

FROM c INNER JOIN pv ON c.cat_id=pv.cat_id
WHERE
pv.step=1 and
c.status=0
)


SELECT
c.name,
(select count(1)
from p inner join p2p on p.prod_id=p2p.prod_id
inner join pv on p2p.path_id = pv.path_id
where
pv.cat_id=c.cat_id and
p.status=0 and
p2p.status=0
) AS 'Total products',

(select count(1)
from p inner join o on p.product_id=o.product_id
inner join p2p on p.prod_id=p2p.prod_id
inner join pv on p2p.path_id = pv.path_id
where
pv.cat_id=c.cat_id and
p.status=0 and
o.status=0 and
p2p.status=0
) AS 'Total Reviews',


(select count(1)
from p inner join p2p on p.prod_id=p2p.prod_id
inner join pv on p2p.path_id = pv.path_id
where
pv.cat_id=c.cat_id and
p.status=0 and
p2p.status=0 and
(select count(1) from O where prod_id = p.prod_id and status=0)>1) AS 'Prod. With 1 Review'

From
CAT c
order by c.name

/*************************************/
as you can see in the above query I am using some common tables again and again for different aggragation purpose like
p, p2p,pv are common tables used in all subqueries. As the no of record in each of these tables is very high so the query is running very slow.

Can anyone please suggest me how can i optimize this query?

Thanks
Dave Ballantyne
Dave Ballantyne
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7010 Visits: 8370
Please post query plan and DDL.
See the links in my signature if you are unsure of how to do this.



Clear Sky SQL
My Blog
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40700 Visits: 20000
Some sample data showing the relationships between the tables would be great. There are a number of ways of structuring this type of query which has different aggregates hanging off key values, which one is best for you will depend on the data. Here's one way which is at least easy to understand, if not the fastest:
;WITH CTE_ProductReviews AS (
SELECT p2p.path_id, p2p.prod_id, o.ReviewCount
FROM p2p
INNER JOIN (products) p ON p.prod_id = p2p.prod_id AND p.[status] = 0
LEFT JOIN (SELECT product_id, COUNT(*) AS ReviewCount
FROM Reviews
WHERE [status] = 0
GROUP BY product_id) o ON o.product_id = p.product_id
WHERE p2p.[status] = 0)


SELECT c.name,
d1.ProductCount AS 'Total products',
d1.ReviewCount AS 'Total Reviews',
d2.ProductCount AS 'Prod. With 1 Review'

FROM c

INNER JOIN pv ON pv.cat_id = c.cat_id AND pv.step = 1

INNER JOIN (SELECT path_id,
COUNT(*) AS ProductCount,
SUM(ReviewCount) AS ReviewCount
FROM CTE_ProductReviews
GROUP BY path_id) d1 ON d1.path_id = pv.path_id

INNER JOIN (SELECT path_id,
COUNT(*) AS ProductCount
FROM CTE_ProductReviews
WHERE ReviewCount = 1
GROUP BY path_id) d2 ON d2.path_id = pv.path_id

WHERE c.[status] = 0

ORDER BY c.name



Cheers

ChrisM

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
mabud_ncc
mabud_ncc
Forum Newbie
Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)Forum Newbie (5 reputation)

Group: General Forum Members
Points: 5 Visits: 71
table p [prod_id (pk) , name, status]
table o [oid (pk), prod_id(fk), status]
table p2p [p2pid (pk),path_id(fk), prod_id(fk), status]
table pv [path_id(fk), cat_id (fk), step]
table c [cat_id (pk), status, type]

data
table p
-> p1, name1, 0
-> p2, name2, 0
-> p3, name3, 0
-> p4, name4,0

table o
-> o1,p1,0
-> o2,p1,0
-> o3,p2,0
-> o4,p3,1
-> o5,p3,0

table p2p
-> 1, path1,p1,0
-> 2, path1,p2,0
-> 3, path2,p3
-> 4, path3,p4

table pv
-> path1, c1, 1
-> path2, c2, 1
-> path3, c3, 1

table CAT
-> c1,0,2
-> c2,0,1
-> c3,0,4
-> c4,0,2

Result of this query
------------------------
cat totProducts total rev. prod with one rev
c1 2 3 2
c2 1 1 1
c3 1 0 0
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)SSC-Forever (40K reputation)

Group: General Forum Members
Points: 40700 Visits: 20000
mabud_ncc (12/4/2009)
table p [prod_id (pk) , name, status]
table o [oid (pk), prod_id(fk), status]
table p2p [p2pid (pk),path_id(fk), prod_id(fk), status]
table pv [path_id(fk), cat_id (fk), step]
table c [cat_id (pk), status, type] 1 0 0


Please read the article in the link of my sig.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
GilaMonster
GilaMonster
SSC Guru
SSC Guru (220K reputation)SSC Guru (220K reputation)SSC Guru (220K reputation)SSC Guru (220K reputation)SSC Guru (220K reputation)SSC Guru (220K reputation)SSC Guru (220K reputation)SSC Guru (220K reputation)

Group: General Forum Members
Points: 220073 Visits: 46279
Please post table definitions, index definitions and execution plan, as per http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search