Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Rant:</strong> OMG Ponies' answer gives what you asked for - a rewrite with a join. But I would not be too excited about it, your performance killer is the inside join on email address which, I assume, is not particular selective and then your database needs to sift through all of those rows looking for the MAX of order date.</p> <p>This in reality for MySQL will mean doing a filesort (can you post EXPLAIN SELECT ....?).</p> <p>Now, if mysql had access to an index that would include <code>emailaddress</code>, <code>product</code> and <code>orderdate</code> it might, especially on MyISAM be much more efficient in determining MAX(orderdate) (and no, having an index on each of the columns is not the same as having a composite index on all of the columns). If I was trying to optimize that query, I would bet on that.</p> <p>Other than this rant here's my version of <code>not the latest from a category</code> (I don't expect it to be better, but it is different and you should test the performance; it just might be faster due to lack of subqueries)</p> <p><strong>My attempt</strong> (not tested)</p> <pre><code>SELECT DISTINCT notlatest.id, notlatest.emailaddress, notlatest.product, notlatest.orderdate FROM OrderHistoryTable AS notlatest LEFT JOIN OrderHistoryTable AS EMP latest ON notlatest.emailaddress = latest.emailaddress AND notlatest.orderdate &lt; latest.orderdate AND WHERE notlatest.product IN ('ProductA','ProductB','ProductC','ProductD') AND latest.product IN ('ProductA','ProductB','ProductC','ProductD') AND latest.id IS NOT NULL </code></pre> <p><strong>Comments:</strong><br> - If there is only one record in the category it will not be displayed<br> - Again indexes should speed the above very much</p> <p>Actually this is (might be) a good example of how normalizing data would <strong>improve</strong> performance - your product implies product category, but product category is not stored anywhere and the IN test will not be maintainable in the long run. </p> <p>Furthermore by creating a product category you would be able to <strong>index</strong> directly on it. </p> <p>If the Product was indexed on the category then the performance of joins on the category should be better then test on the Product indexed by value (and not category). (Actually then MyISAM's composite index on <code>emailaddress</code>, <code>category</code>, <code>orderdate</code> should already contain max, min and count per category and that should be cheap).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload