Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's what I would try:</p> <p>Take each derived query and run an <code>EXPLAIN</code> against each separately. As the comments suggest, check for any lines that are missing indexes and add when needed. Post your <code>EXPLAIN</code> results for any help. So</p> <pre><code>EXPLAIN SELECT uids.id id, uids_uids.parent_id parent_id, uids.created date, .... EXPLAIN SELECT uids_uids.parent_id parent_id, MAX(uids.created) maxdate .... </code></pre> <p>If adding indexes doesn't help or help much, then put each subquery into a temp table first and apply indexes to it:</p> <pre><code>CREATE TABLE temp_uid SELECT uids.id id, uids_uids.parent_id parent_id, uids.created date, uids.type type FROM uids JOIN uids_uids ON uids_uids.uid_id = uids.id JOIN aros_uids ON uids.id = aros_uids.uid_id JOIN uids_uids ParentUids ON uids_uids.parent_id = ParentUids.uid_id WHERE uids.type IN ('Document','Photo','Release','PreRelease', 'ArtworkResource','Event') AND (uids.start_date IS NULL OR uids.start_date &lt;= NOW()) AND (uids.end_date IS NULL OR uids.end_date &lt;= NOW()) AND aros_uids.aro_id IN (3,2,86,1448); CREATE TABLE temp_t2 SELECT uids_uids.parent_id parent_id, MAX(uids.created) maxdate FROM uids JOIN uids_uids ON uids_uids.uid_id = uids.id GROUP BY uids_uids.parent_id, uids.type; </code></pre> <p>And <code>JOIN</code> on those tables:</p> <pre><code>SELECT DISTINCT Uid.id, Uid.type FROM temp_uid AS Uid JOIN temp_t2 AS T2 ON Uid.parent_id = T2.parent_id AND Uid.date = T2.maxdate ORDER BY Uid.date DESC LIMIT 100; </code></pre> <p>As I mentioned, you'll probably have to add indexes, and probably to these columns in the temp tables:</p> <pre><code>ALTER TABLE temp_uid ADD INDEX parentDateIdx (parent_id, Uid.date); ALTER TABLE temp_t2 ADD INDEX parentMaxDateIdx (parent_id, maxdate); </code></pre> <p>If you need to refresh the temp tables, just truncate them and perform an <code>INSERT INTO temp_uid...SELECT</code> and <code>INSERT INTO temp_t2...SELECT</code> on them, instead of a <code>CREATE...SELECT</code>. A stored procedure works great for this.</p> <p>btw, performing a <code>CREATE TABLE temp_t2...SELECT</code>, the way I did for each temp table, may not create an optimal table structure, so it might be better to modify the create afterward or do it yourself from scratch.</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.
 

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