How to optimize this query

  • 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

  • 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[/url]

  • 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

  • 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

    ------------------------

    cattotProductstotal rev. prod with one rev

    c1 2 32

    c2 1 11

    c3 1 00

  • 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 00

    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

  • 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

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply