我有3个表:帐户,联系人和accounts_contacts(映射表)。 我在每个表中都有100万条记录。此查询使用filesort并且运行时间超过一分钟:
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
如您所见,联系人表格正在联系人表格中使用filesort。
我已尝试通过在“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;
它确实摆脱了联系人表上的filesort,但它现在在映射表上使用了filesort:
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子句中,但它似乎没有任何区别。
任何建议都将不胜感激。 感谢。
答案 0 :(得分:0)
对于您的特殊查询,您可能无法做多少。但是,如果您将查询更改为使用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)
。