4

複数のテーブルから JOIN を実行して、ファセット検索を実行しています。JOIN を回避し、クエリを 2 つの異なるクエリに分割すると、パフォーマンスが大幅に向上することに気付いたので、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

users テーブルへの 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   
4

1 に答える 1

2

クエリの下で実行するテーブルにインデックスを作成します。

Table    ColumnName
------   ----------
tags     user_id, skillid (Both column in one index)

これを試して:

SELECT s.*, u.a, u.b, u.c, u.d, u.e, u.f, c.location_country, 
       st.location_state, ct.location_city 
FROM tags t 
LEFT JOIN search s ON t.user_id = s.user_id 
LEFT JOIN users u ON t.user_id = u.user_id 
LEFT JOIN countries c ON s.countryId = c.countryId 
LEFT JOIN states st ON s.stateId = st.stateId 
LEFT JOIN cities ci ON s.cityId= ct.cityId 
WHERE t.skillId =52772 
ORDER BY s.number DESC 
LIMIT 0,200

編集

適切なインデックスを使用してこの2つのクエリを試してみて、以下のクエリが機能するかどうかをお知らせください。

SELECT s.*, u.a, u.b, u.c, u.d, u.e, u.f, c.location_country, 
       st.location_state, ct.location_city 
FROM (SELECT user_id FROM tags WHERE t.skillId = 52772) AS t 
LEFT JOIN search s ON t.user_id = s.user_id 
LEFT JOIN users u ON t.user_id = u.user_id 
LEFT JOIN countries c ON s.countryId = c.countryId 
LEFT JOIN states st ON s.stateId = st.stateId 
LEFT JOIN cities ci ON s.cityId= ct.cityId 
ORDER BY s.number DESC 
LIMIT 0,200; 

また

SELECT s.*, u.a, u.b, u.c, u.d, u.e, u.f, c.location_country, 
       st.location_state, ct.location_city 
FROM (SELECT t.user_id, s.* FROM tags t 
      LEFT JOIN search s ON t.user_id = s.user_id  
      WHERE t.skillId = 52772 
      ORDER BY s.number DESC 
      LIMIT 0,200) AS t 
LEFT JOIN users u ON t.user_id = u.user_id 
LEFT JOIN countries c ON s.countryId = c.countryId 
LEFT JOIN states st ON s.stateId = st.stateId 
LEFT JOIN cities ci ON s.cityId= ct.cityId 
ORDER BY s.number DESC 
LIMIT 0,200; 
于 2012-12-25T15:59:42.790 に答える