我正在对我正在处理的应用程序中的一些查询进行分析,我遇到了一个查询,其中检索的行数比必要数量多,结果集在应用程序代码中被修剪.
将LEFT JOIN更改为INNER JOIN将结果集修剪为仅需要的结果集,并且可能还会更具性能(因为选择较少的行).实际上,LEFT JOIN的查询优于INNER JOIN’ED,需要一半的时间才能完成.
LEFT JOIN:(总共127行,查询时间为0.0011秒)
INNER JOIN :(共10行,查询时间为0.0024秒)
(我多次运行查询,那些是平均值).
在两者上运行EXPLAIN都没有解释性能差异的东西:
对于INNER JOIN:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE contacts index NULL name 302 NULL 235 Using where
1 SIMPLE lists eq_ref PRIMARY PRIMARY 4 contacts.list_id 1
1 SIMPLE lists_to_users eq_ref PRIMARY PRIMARY 8 lists.id,const 1
1 SIMPLE tags eq_ref PRIMARY PRIMARY 4 lists_to_users.tag_id 1
1 SIMPLE users eq_ref email_2 email_2 302 contacts.email 1 Using where
对于LEFT JOIN:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE contacts index NULL name 302 NULL 235 Using where
1 SIMPLE lists eq_ref PRIMARY PRIMARY 4 contacts.list_id 1
1 SIMPLE lists_to_users eq_ref PRIMARY PRIMARY 8 lists.id,const 1
1 SIMPLE tags eq_ref PRIMARY PRIMARY 4 lists_to_users.tag_id 1
1 SIMPLE users eq_ref email_2 email_2 302 contacts.email 1
而查询本身:
SELECT `contacts`.*, `lists`.`name` AS `group`, `lists`.`id` AS `group_id`, `lists`.`shared_yn`, `tags`.`name` AS `context`, `tags`.`id` AS `context_id`, `tags`.`color` AS `context_color`, `users`.`id` AS `user_id`, `users`.`avatar`
FROM `contacts`
LEFT JOIN `lists` ON lists.id=contacts.list_id
LEFT JOIN `lists_to_users` ON lists_to_users.list_id=lists.id AND lists_to_users.user_id='1' AND lists_to_users.creator='1'
LEFT JOIN `tags` ON tags.id=lists_to_users.tag_id
INNER JOIN `users` ON users.email=contacts.email
WHERE (contacts.user_id='1')
ORDER BY `contacts`.`name` ASC
(我所说的条款是’用户’表上的最后一个INNER JOIN)
查询在MySQL 5.1数据库上运行,如果它有所作为.
有没有人知道为什么LEFT JOIN的查询在这种情况下胜过INNER加入?
更新:由于Tomalak的建议,我使用的小表使INNER JOIN更复杂,我创建了一个测试数据库与一些模拟数据. ‘users’表是5000行,联系人表是〜500,000行.结果是一样的(也是时候没有改变,这是令人惊讶的,当你认为表现在更大的现在).
我也在联系人表上运行ANALYZE和OPTIMIZE.没有任何明显的区别.