Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to optimize this query Expand / Collapse
Author
Message
Posted Friday, December 4, 2009 2:34 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 17, 2013 8:36 AM
Points: 5, Visits: 51
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
Post #828700
Posted Friday, December 4, 2009 2:39 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 7:03 AM
Points: 1,949, Visits: 8,303
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
Kent user group
Post #828702
Posted Friday, December 4, 2009 3:29 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:51 AM
Points: 7,127, Visits: 13,503
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
Post #828728
Posted Friday, December 4, 2009 5:13 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Monday, June 17, 2013 8:36 AM
Points: 5, Visits: 51
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
Post #828764
Posted Friday, December 4, 2009 5:32 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:51 AM
Points: 7,127, Visits: 13,503
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
Post #828773
Posted Friday, December 4, 2009 6:34 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 9:57 AM
Points: 42,464, Visits: 35,527
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 2008, MVP
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

Post #828831
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse