Click here to Skip to main content
15,881,794 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a type in my procedure which takes 1sec to execute .. i have been told to optimize this query .. how can i do this..



Here are sp

------------------------
SQL
ELSE IF(@in_type = 5) --Used in Catalogcl class 

BEGIN

    SELECT d.in_catalog_id, a.in_product_id, a.vc_product_name, a.vc_product_image_large,

    (CASE a.bt_product_image_exist WHEN 1 THEN a.vc_product_image_small WHEN 0 THEN '/product_small_images/comingsoon1.gif' END) AS vc_product_image_small,

  dbo.catalog_fnc_get_sale_price(5, d.in_catalog_id, 0, a.in_product_id, @in_website_id) AS mn_sale_price,

    d.vc_catalog_name + ' ' + a.vc_product_name AS vc_product_full_name

    FROM dbo.product a

    INNER JOIN dbo.catalog_product b ON a.in_product_id = b.in_product_id

    INNER JOIN dbo.website_catalog c ON b.in_catalog_id = c.in_catalog_id

    INNER JOIN dbo.catalog d ON c.in_catalog_id = d.in_catalog_id

    WHERE a.bt_active = @bt_active AND b.bt_active = @bt_active AND c.bt_active = @bt_active AND d.bt_active = @bt_active

    AND c.in_website_id = @in_website_id AND d.in_catalog_sub_type_id = @in_catalog_sub_type_id

    AND (@bt_best_sellers = 0 OR b.bt_best_sellers = @bt_best_sellers)

    AND dbo.catalog_fnc_get_sale_price(5, d.in_catalog_id, 0, a.in_product_id, @in_website_id) > 0

    ORDER BY a.vc_product_name

END



Please suggest
Posted
Updated 24-Oct-13 10:22am
v2
Comments
ZurdoDev 24-Oct-13 16:37pm    
What part is slow?
PIEBALDconsult 24-Oct-13 17:15pm    
Looks good to me, and one second is awesome. Don't mess with it.

Hi,
You are using function "catalog_fnc_get_sale_price" in your query , if you can expand the function , within the query it can surely in increse speed of query. AS for each row, function is being called in your case. you can verify it by using execution plan
 
Share this answer
 
As Amol_B suggested make sure that the function you are calling in both the the select and where clause is both valid and optomised - it is your biggest obvious penalty.

Next turn on actual execution plan in SSMMS (assumes sql server) and look for missing indexes in the result.

If there is nothing there then go back and ask your boss why he thinks it needs optomising.
 
Share this answer
 
Find out if the function call is the culprit by only calling it once, using a subquery like this:
SQL
SELECT  in_catalog_id
       ,in_product_id
       ,vc_product_name
       ,vc_product_image_large
       ,vc_product_image_small
       ,mn_sale_price
       ,vc_product_full_name
FROM
    (
    SELECT  d.in_catalog_id
           ,a.in_product_id
           ,a.vc_product_name
           ,a.vc_product_image_large
           ,(CASE a.bt_product_image_exist WHEN 1 THEN a.vc_product_image_small WHEN 0 THEN '/product_small_images/comingsoon1.gif' END) AS vc_product_image_small
           ,dbo.catalog_fnc_get_sale_price(5, d.in_catalog_id, 0, a.in_product_id, @in_website_id) AS mn_sale_price
           ,d.vc_catalog_name + ' ' + a.vc_product_name AS vc_product_full_name
    FROM dbo.product a
    INNER JOIN dbo.catalog_product b ON a.in_product_id = b.in_product_id
    INNER JOIN dbo.website_catalog c ON b.in_catalog_id = c.in_catalog_id
    INNER JOIN dbo.catalog d ON c.in_catalog_id = d.in_catalog_id
    WHERE   a.bt_active = @bt_active
    AND     b.bt_active = @bt_active
    AND     c.bt_active = @bt_active
    AND     d.bt_active = @bt_active
    AND     c.in_website_id = @in_website_id
    AND     d.in_catalog_sub_type_id = @in_catalog_sub_type_id
    AND     (@bt_best_sellers = 0 OR b.bt_best_sellers = @bt_best_sellers)
    )
WHERE   mn_sale_price > 0
ORDER BY vc_product_name
 
Share this answer
 
If query optimization would be that simple, just a reformulation of a statement string, it would be a fully automated process, it would be a funtion of any sql engine, thus would not be necessary.
One has to consider several things to optimize a query, like query plans, indexes, table sizes ad so on... But first of all, data semantics. None of these is available to us to help you.
Sorry, but you are on your own...
 
Share this answer
 
Comments
k@ran 24-Oct-13 18:48pm    
can you suggest me any way by which i can do it..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900