Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're probably missing an index. With the following schema, this same query hit the index for every table:</p> <pre><code>CREATE TABLE `products` ( `id` int(11) NOT NULL AUTO_INCREMENT, `deleted` tinyint(1) unsigned NOT NULL DEFAULT '0', `status` varchar(255) NOT NULL DEFAULT 'listed', `images` varchar(255) DEFAULT NULL, `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `tags` ( `id` int(11) NOT NULL AUTO_INCREMENT, `tag` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE `products_tags` ( `product_id` int(11) NOT NULL, `tag_id` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`product_id`,`tag_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; </code></pre> <p>Here are the results of the EXPLAIN query:</p> <pre><code>EXPLAIN SELECT p.id AS id, GROUP_CONCAT(t.tag SEPARATOR '|') AS tags, UNIX_TIMESTAMP(p.created) AS created FROM products p INNER JOIN products_tags pt ON pt.product_id = p.id INNER JOIN tags t ON t.id = pt.tag_id WHERE p.deleted = 0 AND (p.images IS NOT NULL OR p.images &lt;&gt; '') AND p.status LIKE 'listed' GROUP BY p.id; +----+-------------+-------+--------+---------------+---------+---------+----------------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+--------+---------------+---------+---------+----------------+------+-------------+ | 1 | SIMPLE | p | index | PRIMARY | PRIMARY | 4 | NULL | 1 | Using where | | 1 | SIMPLE | pt | ref | PRIMARY | PRIMARY | 4 | test.p.id | 1 | Using index | | 1 | SIMPLE | t | eq_ref | PRIMARY | PRIMARY | 4 | test.pt.tag_id | 1 | | +----+-------------+-------+--------+---------------+---------+---------+----------------+------+-------------+ 3 rows in set (0.00 sec) </code></pre>
    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