Optimize this MSSQL Query

Hi ,

I have this MySQL Query,  it currently is not well optimized

I believe i know what needs to be done - but i would like to get an experts take on this.


select p.products_id, p.products_small, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price, pd.products_description

from orders_products opa, orders_products opb, orders o, products p

left join specials s on p.products_id = s.products_id, products_description pd, categories c, products_to_categories p2c  

where c.categories_status='1' and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and opa.products_id = '0' and opa.orders_id = opb.orders_id and opb.products_id != '0' and opb.products_id = p.products_id and opb.orders_id = o.orders_id and p.products_status = '1' and pd.products_id = p.products_id and pd.products_id = p2c.products_id group by p.products_id order by o.date_purchased desc limit 6;
Who is Participating?
ArgentiConnect With a Mentor Commented:

why do you join the same table with itself, based on the same field?
from orders_products opa, orders_products opb...
where opa.orders_id = opb.orders_id
It doesn't do anything but waste time to make the join and get the exact same result, which is the orders_products table (especially if it's a large table)

Advice: rewrite your SQL using inner/outer joins between tables, starting from the largest one, towards the smallest:

select [your_fields_and_expressions]
from orders_products
inner join orders on [condition]
inner join products on [...]
inner join products_description on [...]
inner join products_to_categories on [...]
inner join categories on [...]
left join specials on [...]

Open in new window

leaving just the constants into the where clause

where c.categories_status='1' 
  and opa.products_id = '0' 
  and opb.products_id != '0'  
  and p.products_status = '1' 

Open in new window

Terry WoodsIT GuruCommented:
Argent1, note that different rows are being joined beween opa and opb. See the filters:
opa.products_id = '0'
and opb.products_id != '0'

There may be good reason for this.
PortletPaulConnect With a Mentor Commented:
regarding performance can you provide the execution plan detail?

& is the self join on orders_products really needed?

The following probably won't affect the performance although some conditions have been moved that may have some impact.

In the original there is one use of ANSI join syntax (LEFT JOIN) with the balance being non-ANSI syntax. It is never a good idea to mix these syntax styles as that can lead to error, as well as being a maintenance nightmare. Strongly urge you use only ANSI join syntax.

think this is the equivalent using ANSI syntax
      , p.products_small
      , p.products_tax_class_id
      , if(s.status, s.specials_new_products_price, p.products_price) as products_price
      , pd.products_description
FROM orders_products opa
INNER JOIN orders_products opb
        ON opa.orders_id = opb.orders_id
        AND opb.products_id != '0'
INNER JOIN orders o
        ON opb.orders_id = o.orders_id
INNER JOIN products p
        ON opb.products_id = p.products_id
        AND p.products_status = '1'
LEFT JOIN specials s
        ON p.products_id = s.products_id
INNER JOIN products_description pd
        ON p.products_id = pd.products_id
INNER JOIN products_to_categories p2c
        ON pd.products_id = p2c.products_id
INNER JOIN categories c
        ON p2c.categories_id = c.categories_id

WHERE opa.products_id = '0'
        AND c.categories_status = '1'

GROUP BY p.products_id
        o.date_purchased DESC /* is this really needed? it isn't displayed */
limit 6;

Open in new window

if the order by isn't needed several tables/joins could be avoided I think
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.