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.

Thanks


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;
LVL 2
meteorelecAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ArgentiCommented:
hi,

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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
0
PortletPaulfreelancerCommented:
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
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
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
ORDER BY
        o.date_purchased DESC /* is this really needed? it isn't displayed */
limit 6;

Open in new window

0
PortletPaulfreelancerCommented:
if the order by isn't needed several tables/joins could be avoided I think
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.