我正在執行JOIN從多個表執行分面搜索。當避免JOIN並將查詢分成兩個不同的查詢時,我注意到一個很大的性能提升,所以我假設我的JOIN沒有被優化。什麼會導致連接比分爲兩個查詢慢?
結構:
-- tags
userId | tagId
1 3
1 4
2 3
2 9
-- search
userId | number | countryId | stateId ...
1 13 221 55
-- countries
countryId | countryName
221 Somewhere
-- users
userId | profileImageLink
1 | <photo link>
我試圖提取有標籤的所有用戶,按照search.number和其他表把元數據的順序。查詢:
SELECT
search.*, users.a, users.b, users.c, users.d, users.e, users.f, countries.location_country, states.location_state, cities.location_city
FROM search
RIGHT JOIN tags
ON search.user_id = tags.user_id
LEFT JOIN users
ON users.user_id=search.user_id
LEFT JOIN countries
ON countries.countryId=search.countryId
LEFT JOIN states
ON states.countryId=search.countryId AND states.stateId=search.stateId
LEFT JOIN cities
ON cities.countryId=search.countryId AND cities.stateId=search.stateId AND cities.cityId=search.cityId
WHERE
tags.skillId =52772
ORDER BY
search.number DESC LIMIT 0,200
我注意到刪除JOIN到用戶表(並做了之後)使查詢快得多。我如何優化它以在相同的查詢中工作?我試着改變FROM到標籤,而不是搜索,但沒有奏效...
這是EXPLAIN顯示:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE tags ref skill_user,skillId skill_user 4 const 184854 Using index; Using temporary; Using filesort
1 SIMPLE search eq_ref user_id user_id 4 tags.user_id 1
1 SIMPLE countries eq_ref PRIMARY PRIMARY 2 search.countryId 1
1 SIMPLE states eq_ref PRIMARY,state PRIMARY 3 search.stateId 1
1 SIMPLE cities eq_ref PRIMARY,city PRIMARY 3 search.cityId 1
1 SIMPLE users eq_ref user_id user_id 4 search.user_id 1
EXPLAIN without the LEFT JOIN users:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE tags ref skill_user,skillId skill_user 4 const 155870 Using index
1 SIMPLE search eq_ref user_id user_id 4 tags.user_id 1
1 SIMPLE countries eq_ref PRIMARY PRIMARY 2 search.countryId 1
1 SIMPLE states eq_ref PRIMARY,state PRIMARY 3 search.stateId 1
1 SIMPLE cities eq_ref PRIMARY,city PRIMARY 3 search.cityId 1
EXPLAIN查詢的建議回答:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE tags index NULL userid_skill 8 NULL 22689539 Using where; Using index; Using temporary; Using filesort
1 SIMPLE search eq_ref user_id user_id 4 tags.user_id 1
1 SIMPLE users eq_ref user_id user_id 4 search.user_id 1
1 SIMPLE countries eq_ref PRIMARY PRIMARY 2 search.countryId 1
1 SIMPLE states eq_ref PRIMARY,state PRIMARY 3 search.stateId 1
1 SIMPLE cities eq_ref PRIMARY,city PRIMARY 3 search.cityId 1
是你的列(在加入柱)正確索引?非物質的,我建議避免這麼多的聯接。我認爲當你的數據量有可能增長時,分解查詢是一個更好的主意。 –
索引你的連接列,也許where子句中的where列...不知道我建議的建議是 –
你可以發佈你的查詢的「解釋」結果嗎?只需在查詢前添加EXPLAIN關鍵字 – sdespont