Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Regarding filtering on tags (which you mentioned in the comments on <a href="https://stackoverflow.com/questions/7832806/optimising-mysql-query-on-joined-tables-with-group-by-and-order-by-without-using/7833628#7833628">Johan's answer</a>), if the obvious</p> <pre><code>SELECT p.*, GROUP_CONCAT(pt.name) AS tags FROM products p JOIN product_tags_for_products pt4p2 ON (pt4p2.product_id = p.id) JOIN product_tags pt2 ON (pt2.id = pt4p2.product_tag_id) LEFT JOIN product_tags_for_products pt4p ON (pt4p.product_id = p.id) LEFT JOIN product_tags pt ON (pt.id = pt4p.product_tag_id) WHERE pt2.name IN ('some', 'tags', 'here') GROUP BY p.id ORDER BY p.created LIMIT 30 </code></pre> <p>doesn't run fast enough, you could always try this:</p> <pre><code>CREATE TEMPORARY TABLE products30 SELECT p.* FROM products p JOIN product_tags_for_products pt4p ON (pt4p.product_id = p.id) JOIN product_tags pt ON (pt.id = pt4p.product_tag_id) WHERE pt.name IN ('some', 'tags', 'here') GROUP BY p.id ORDER BY p.created LIMIT 30 SELECT p.*, GROUP_CONCAT(pt.name) AS tags FROM products30 p LEFT JOIN product_tags_for_products pt4p ON (pt4p.product_id = p.id) LEFT JOIN product_tags pt ON (pt.id = pt4p.product_tag_id) GROUP BY p.id ORDER BY p.created </code></pre> <p>(I used a temp table because you said "no subqueries"; I don't know if they're any easier to use in an Active Record framework, but at least it's another way to do it.)</p> <hr> <p>Ps. One really off-the-wall idea about your original problem: would it make any difference if you changed the <code>GROUP BY p.id</code> clause to <code>GROUP BY p.created, p.id</code>? Probably not, but I'd at least try it.</p>
    singulars
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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