MySQL Forums
Forum List  »  Optimizer & Parser

Re: calculating total products in each category
Posted by: Rick James
Date: December 29, 2009 09:54PM

SELECT  parent.ctgid, COUNT(1) total
    FROM  category AS node,
          category AS parent,
          product
    WHERE  node.lft BETWEEN parent.lft AND  parent.rgt
      AND  node.ctgid = product.category
      AND  product.active = 'Y'
    GROUP BY  parent.ctgid
    ORDER BY  node.lft

This will be a mess to optimize:
node.lft BETWEEN parent.lft AND parent.rgt
because it effectively needs to do a lot of work comparing fields in the two tables. Yet you have no index to help it; see if adding
INDEX(lft)
helps.

Better yet, use this instead:
INDEX(lft, rgt, ctgid)
That might make it "Using index".

Having to filter on product.active = 'Y' is a nuisance. Is it reasonable to remove them from that table? (Cannot predict how much this might help.)

Remove
ORDER BY node.lft
It is fetched (except for the WHERE), so has no meaning.

Options: ReplyQuote


Subject
Views
Written By
Posted
4521
December 16, 2009 04:26AM
Re: calculating total products in each category
1969
December 29, 2009 09:54PM


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.