2012-12-07 116 views
2

我有一個查詢MySQL查詢優化[加入]

SELECT 
i.*, 
gu.* 
vs.* 
FROM 
common.global_users gu 
LEFT JOIN common.global_users_perms gup ON (gu.global_user_id=gup.global_user_id) 
LEFT JOIN p.individuals i ON (gup.parent_id = i.member_id) 
LEFT JOIN p.vs ON (i.member_id=vs.member_id AND vs.status IN (1,4)) 
WHERE gup.region = 'us' 
AND gu.user_type=2 
AND ((gu.email='[email protected]') OR (i.email='[email protected]') OR (gu.username='[email protected]')) 
ORDER BY i.status, vs.member_id; 

和下一步的計劃

+----+-------------+-------+--------+------------------------------------------+------------------+---------+-----------------------------+--------+----------+---------------------------------+ 
| id | select_type | table | type | possible_keys       | key    | key_len | ref       | rows | filtered | Extra       | 
+----+-------------+-------+--------+------------------------------------------+------------------+---------+-----------------------------+--------+----------+---------------------------------+ 
| 1 | SIMPLE  | gu | ref | PRIMARY,username,idx_user_type,idx_email | idx_user_type | 1  | const      | 524243 | 100.00 | Using temporary; Using filesort | 
| 1 | SIMPLE  | gup | ref | global_user_id_2       | global_user_id_2 | 4  | common.gu.global_user_id |  1 | 100.00 | Using where      | 
| 1 | SIMPLE  | i  | eq_ref | PRIMARY         | PRIMARY   | 4  | common.gup.parent_id  |  1 | 100.00 | Using where      | 
| 1 | SIMPLE  | vs | ref | member_id,status       | member_id  | 4  | p.i.member_id    |  1 | 100.00 |         | 
+----+-------------+-------+--------+------------------------------------------+------------------+---------+-----------------------------+--------+----------+---------------------------------+ 

是否有可能使它更快嗎?現在大約需要12秒

P.S.在gu表中只有兩個唯一的type值。並且該表格中的所有搜尋數超過1M。

回答

0

您的計劃結果可能表明問題'使用臨時;使用filesort意味着正在磁盤上進行排序,可能是因爲它的大小適合內存。如果可能的話,您可能需要增加內存限制。

這也可能是與您的排序對列。如果刪除ORDER BY,查詢是否更快執行?如果是這種情況,你可能想看看ORDER BY Optimization

+0

是的,沒有ORDER子句查詢工作更快的兩次,但在我的情況下,從兩個不同的表(並沒有從第一個表中)的順序子句中的字段。 '在某些情況下,MySQL無法使用索引來解決ORDER BY 您正在連接多個表,並且ORDER BY中的列並非全部來自用於檢索行的第一個非常數表。 – user1016265