Note that there are some explanatory texts on larger screens.

plurals
  1. POAny way to speed up this query?
    primarykey
    data
    text
    <p>This query is really slow. Takes between 9 and 10 seconds...</p> <pre><code>SELECT DISTINCT a.* FROM addresses a LEFT JOIN contacts c ON c.id = a.contact_id LEFT JOIN organizations o ON o.id = a.organization_id ORDER BY c.last_name, c.first_name, o.name LIMIT 0, 24 </code></pre> <p>If I comment out the <code>ORDER BY</code> clause the query runs much faster -- about 5 milliseconds. But I need the <code>ORDER BY</code> to support paging of the search results. And the users need the addresses to be sorted by contact and organization.</p> <hr> <p><strong>Table structure</strong></p> <pre><code>addresses --------- id int NOT NULL contact_id int # could be NULL organization_id int # could be NULL contacts -------- id int NOT NULL first_name varchar(255) last_name varchar(255) organizations ------------- id int NOT NULL name varchar(255) </code></pre> <p>They're all InnoDB tables.</p> <p>I have these indexes on the contacts table:</p> <pre><code> KEY `idx_contacts_first_name` (`first_name`), KEY `idx_contacts_last_name` (`last_name`), KEY `idx_contacts_first_name_last_name` (`first_name`,`last_name`) </code></pre> <p>And on the organizations table:</p> <pre><code> KEY `idx_organization_name` (`name`) </code></pre> <hr> <p><strong>Amount of data</strong></p> <pre><code>Addresses: 22,271 Contacts: 17,906 Organizations: 8,246 </code></pre> <hr> <p><strong>DESCRIBE output</strong></p> <pre><code>mysql&gt; DESCRIBE -&gt; SELECT DISTINCT a.* -&gt; FROM addresses a -&gt; LEFT JOIN contacts c -&gt; ON c.id = a.contact_id -&gt; LEFT JOIN organizations o -&gt; ON o.id = a.organization_id -&gt; ORDER BY c.last_name, c.first_name, o.name -&gt; LIMIT 0, 24; +----+-------------+-------+--------+---------------+---------+---------+--------------------------------------------+-------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+--------+---------------+---------+---------+--------------------------------------------+-------+---------------------------------+ | 1 | SIMPLE | a | ALL | NULL | NULL | NULL | NULL | 22387 | Using temporary; Using filesort | | 1 | SIMPLE | c | eq_ref | PRIMARY | PRIMARY | 4 | contactdb_v2_development.a.contact_id | 1 | Distinct | | 1 | SIMPLE | o | eq_ref | PRIMARY | PRIMARY | 4 | contactdb_v2_development.a.organization_id | 1 | Distinct | +----+-------------+-------+--------+---------------+---------+---------+--------------------------------------------+-------+---------------------------------+ 3 rows in set (0.00 sec) </code></pre>
    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