繁体   English   中英

使用左联接和排序时如何避免文件排序

[英]How to avoid filesort when using left join and order by

我有3个表:accounts,contacts和accounts_contacts(一个映射表)。 我在每个表中都有1百万条记录。 该查询正在使用文件排序,并且需要一分钟以上的时间来运行:

explain SELECT contacts.salutation salutation, contacts.first_name first_name, contacts.last_name last_name, contacts.title title, jt0_accounts.id account_id, jt0_accounts.name account_name

FROM contacts

LEFT JOIN accounts_contacts jt1_accounts_contacts ON (contacts.id = jt1_accounts_contacts.contact_id AND jt1_accounts_contacts.deleted = 0)

LEFT JOIN accounts jt0_accounts ON (jt0_accounts.id = jt1_accounts_contacts.account_id AND jt0_accounts.deleted = 0)

ORDER BY jt0_accounts.name DESC;

这是解释输出:

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1    SIMPLE contacts    ALL NULL    NULL    NULL    NULL    195634  Using temporary; Using filesort
1    SIMPLE jt1_accounts_contacts   ref idx_contid_del_accid    idx_contid_del_accid    113 sugar7.contacts.id,const    1   
1   SIMPLE  jt0_accounts    eq_ref  PRIMARY,idx_accounts_id_del,idx_accounts_date_entered,idx_accnt_assigned_del    PRIMARY 108 sugar7.jt1_accounts_contacts.account_id 1   

如您所见,联系人表在联系人表上使用文件排序。

我尝试通过在“ ORDER BY”之前添加“ WHERE jt0_accounts.name <>”来摆脱文件排序,因此它变为:

explain SELECT contacts.salutation salutation, contacts.first_name first_name, contacts.last_name last_name, contacts.title title, jt0_accounts.id account_id, jt0_accounts.name account_name

FROM contacts

LEFT JOIN accounts_contacts jt1_accounts_contacts ON (contacts.id = jt1_accounts_contacts.contact_id AND jt1_accounts_contacts.deleted = 0)

LEFT JOIN accounts jt0_accounts ON (jt0_accounts.id = jt1_accounts_contacts.account_id AND jt0_accounts.deleted = 0)

WHERE jt0_accounts.name <> ''
ORDER BY jt0_accounts.name DESC;

它确实摆脱了联系人表上的文件排序,但现在在映射表上使用了文件排序:

id  select_type table   type    possible_keys   key key_len ref rows    Extra
1   SIMPLE  jt1_accounts_contacts   ALL idx_account_contact,idx_contid_del_accid    NULL    NULL    NULL    34994   Using where; Using temporary; Using filesort
1   SIMPLE  jt0_accounts    eq_ref  PRIMARY,idx_accounts_id_del,idx_accounts_date_entered,idx_accnt_name_del,idx_accnt_assigned_del PRIMARY 108 sugar7.jt1_accounts_contacts.account_id 1   Using where
1   SIMPLE  contacts    eq_ref  PRIMARY,idx_contacts_id_del,idx_contacts_date_entered   PRIMARY 108 sugar7.jt1_accounts_contacts.contact_id 1   Using where

idx_account_contact索引由account_id和contacts_id组成。 我尝试将它们添加到WHERE子句中,但似乎没有什么区别。

任何建议,将不胜感激。 谢谢。

您可能无法对您的特定查询做很多事情。 但是,如果将查询更改为使用inner join ,则可能会有机会:

SELECT c.salutation, c.first_name, c.last_name, c.title,
       a.id as account_id, a.name as account_name
FROM accounts a JOIN
     accounts_contacts ac
     ON a.id = ac.account_id AND a.deleted = 0
     contacts c JOIN
     ON c.id = ac.contact_id AND ac.deleted = 0     
ORDER BY a.name DESC;

然后,尝试以下索引: accounts(name, deleted, id)accounts_contacts(account_id, contact_id)concats(contact_id, deleted)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM