MySQL Forums
Forum List  »  Newbie

Mysql sub query optimization
Posted by: JH NG
Date: July 02, 2014 03:33AM

****Dependent Subquery****

SELECT c.category_id,
c.parent_id,
cd.name,
(SELECT Count(p.product_id)
FROM oc_product_to_category p2c
LEFT JOIN oc_product p
ON ( p.product_id = p2c.product_id )
LEFT JOIN oc_product_to_store p2s
ON ( p.product_id = p2s.product_id )
WHERE p.status = '1'
AND p.date_available <= Now()
AND p2s.store_id = '0'
AND p2c.category_id = c.category_id) AS total

FROM oc_category c
LEFT JOIN oc_category_description cd
ON ( c.category_id = cd.category_id )
LEFT JOIN oc_category_to_store c2s
ON ( c.category_id = c2s.category_id )

WHERE cd.language_id = '1'
AND c2s.store_id = '0'
AND c.status = '1'
ORDER BY c.sort_order,
Lcase(cd.name)




****Derived Sub query****
SELECT c.category_id,
c.parent_id,
cd.name,
if(t.total IS NULL, 0, total) as total

FROM oc_category c
LEFT JOIN oc_category_description cd
ON ( c.category_id = cd.category_id )
LEFT JOIN oc_category_to_store c2s
ON ( c.category_id = c2s.category_id )

LEFT JOIN (
SELECT Count(p.product_id) as total, p2c.category_id
FROM oc_product_to_category p2c
LEFT JOIN oc_product p
ON ( p.product_id = p2c.product_id )
LEFT JOIN oc_product_to_store p2s
ON ( p.product_id = p2s.product_id )
WHERE p.status = '1'
AND p.date_available <= Now()
AND p2s.store_id = '0'
GROUP BY p2c.category_id) t
ON ( t.category_id = c.category_id )

WHERE cd.language_id = '1'
AND c2s.store_id = '0'
AND c.status = '1'
ORDER BY c.sort_order,
Lcase(cd.name)

Dear all Pros,please give some opinion on the sub query above that which one of them is better for an open cart system.

Options: ReplyQuote


Subject
Written By
Posted
Mysql sub query optimization
July 02, 2014 03:33AM
July 03, 2014 02:02AM
July 04, 2014 01:01AM
July 09, 2014 02:42AM
July 06, 2014 11:30PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.