Note that there are some explanatory texts on larger screens.

plurals
  1. POImprove performance of mysql query
    primarykey
    data
    text
    <p>I have a mysql sql select that's taking way to long to return data.</p> <p><strong>Tables</strong></p> <pre><code>╔════════════════╗ ╔════════════════╗ ║ ITEM ║ ║ Workspace ║ ╠════════════════║ ╠════════════════║ ║ id ║ ║ id ║ ║ guid ║ ║ guid ║ ║ workspace_id ║ ║ company_id ║ ║ deleted ║ ║ deleted ║ ╚════════════════╝ ╚════════════════╝ Indexes: id, guid Indexes: id, guid, workspace_id company_id ╔════════════════╗ ╔════════════════════╗ ║ COMPANY ║ ║ item_category_xref ║ ╠════════════════║ ╠════════════════════║ ║ id ║ ║ item_id ║ ║ deleted ║ ║ category_id ║ ╚════════════════╝ ╚════════════════════╝ Indexes: id Indexes: item_id, category_id ╔════════════════╗ ╔═════════════════════╗ ║ item_image ║ ║ tracking_action ║ ╠════════════════║ ╠═════════════════════║ ║ item_id ║ ║ id ║ ║ sequence ║ ║ guid ║ ╚════════════════╝ ║ action ║ Indexes: ║ context ║ (item_id, sequence) ║ deleted ║ ╚═════════════════════╝ </code></pre> <p><strong>SQL</strong></p> <pre><code> SELECT itm.id "item.id", ws.id "workspace.id", co.id "company.id", ((SELECT count(*) FROM item_category_xref icx WHERE icx.item_id = itm.id AND icx.featured = 1) &gt; 0) "featured", (SELECT COUNT(*) FROM tracking_action ta1 WHERE ta1.context = 'ITEM' AND ta1.context_guid = itm.guid AND ta1.action = 'VIEW') ta_view_count , (SELECT COUNT(*) FROM tracking_action ta2 WHERE ta2.context = 'ITEM' AND ta2.context_guid = itm.guid AND ta2.action = 'SEARCH_RESULT') ta_search_count FROM item itm JOIN workspace ws ON itm.workspace_id = ws.id AND ws.deleted != 1 JOIN company co ON ws.company_id = co.id AND co.deleted != 1 JOIN item_category_xref icx ON itm.id = icx.item_id AND icx.category_id = 1 LEFT JOIN item_image ii ON itm.id = ii.item_id AND ii.sequence = 1 WHERE itm.deleted != 1 HAVING featured &gt; 0; </code></pre> <p><strong>EXPLAIN</strong> <img src="https://i.stack.imgur.com/xihfT.png" alt="SQL EXPLAIN"></p> <p>This query is a result of my efforts to reduce and improve. I've gone from the original query that took 180 seconds down to this that now takes about 20 seconds but its still not enough.</p> <p>Can anyone offer performance improvements for this query?</p> <p>We're searching through a few million rows of data so every little bit will help.</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.
 

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