Click here to monitor SSC
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
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1818 Visits: 8370
Cross post, please keep to one thread

http://www.sqlservercentral.com/Forums/Topic828700-360-1.aspx



Clear Sky SQL
My Blog
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