2011-03-22 160 views
2

我有兩個表:爲什麼不使用我的索引?


mysql> desc myps3t_gameusertrophyinfo; 

+-----------+--------------+------+-----+---------+----------------+ 
| Field  | Type   | Null | Key | Default | Extra   | 
+-----------+--------------+------+-----+---------+----------------+ 
| id  | int(11)  | NO | PRI | NULL | auto_increment | 
| user_id | int(11)  | NO | MUL | NULL |    | 
| trophy_id | int(11)  | NO | MUL | NULL |    | 
| date  | datetime  | NO | MUL | NULL |    | 
| date_read | varchar(100) | NO |  | NULL |    | 
+-----------+--------------+------+-----+---------+----------------+ 
5 rows in set (0.19 sec) 


mysql> show index from myps3t_gameusertrophyinfo; 

+---------------------------+------------+------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 
| Table      | Non_unique | Key_name       | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
+---------------------------+------------+------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 
| myps3t_gameusertrophyinfo |   0 | PRIMARY       |   1 | id   | A   |  4004589 |  NULL | NULL |  | BTREE  |   | 
| myps3t_gameusertrophyinfo |   0 | user_id       |   1 | user_id  | A   |  7686 |  NULL | NULL |  | BTREE  |   | 
| myps3t_gameusertrophyinfo |   0 | user_id       |   2 | trophy_id | A   |  4004589 |  NULL | NULL |  | BTREE  |   | 
| myps3t_gameusertrophyinfo |   1 | myps3t_gameusertrophyinfo_403f60f |   1 | user_id  | A   |  7686 |  NULL | NULL |  | BTREE  |   | 
| myps3t_gameusertrophyinfo |   1 | myps3t_gameusertrophyinfo_61a683d8 |   1 | trophy_id | A   |  22624 |  NULL | NULL |  | BTREE  |   | 
| myps3t_gameusertrophyinfo |   1 | idx_gameusertrophyinfo_date  |   1 | date  | A   |  4004589 |  NULL | NULL |  | BTREE  |   | 
+---------------------------+------------+------------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 
7 rows in set (0.06 sec) 

其他表:



mysql> desc myps3t_gametrophyinfo ; 
+-----------+--------------+------+-----+---------+----------------+ 
| Field  | Type   | Null | Key | Default | Extra   | 
+-----------+--------------+------+-----+---------+----------------+ 
| id  | int(11)  | NO | PRI | NULL | auto_increment | 
| game_id | int(11)  | NO | MUL | NULL |    | 
| name  | varchar(500) | NO |  | NULL |    | 
| desc  | varchar(500) | NO |  | NULL |    | 
| type  | varchar(20) | NO |  | NULL |    | 
| pic_url | varchar(200) | NO |  | NULL |    | 
| desc_pt | varchar(500) | NO |  | NULL |    | 
| name_pt | varchar(500) | NO |  | NULL |    | 
| hidden_id | int(11)  | NO |  | NULL |    | 
| total_id | int(11)  | NO |  | NULL |    | 
| trophy_id | int(11)  | NO |  | NULL |    | 
| addon_id | int(11)  | YES |  | NULL |    | 
| points | double  | NO |  | 0  |    | 
| sony_id | int(11)  | YES |  | NULL |    | 
+-----------+--------------+------+-----+---------+----------------+ 
14 rows in set (0.00 sec) 

mysql> show index from myps3t_gametrophyinfo; 
+-----------------------+------------+-------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 
| Table     | Non_unique | Key_name      | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | 
+-----------------------+------------+-------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 
| myps3t_gametrophyinfo |   0 | PRIMARY      |   1 | id   | A   |  25976 |  NULL | NULL |  | BTREE  |   | 
| myps3t_gametrophyinfo |   1 | myps3t_gametrophyinfo_game_id |   1 | game_id  | A   |   764 |  NULL | NULL |  | BTREE  |   | 
+-----------------------+------------+-------------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+ 

當我做到這一點查詢:



mysql> explain select * from myps3t_gameusertrophyinfo a, myps3t_gametrophyinfo b where a.trophy_id = b.id and b.addon_id = 58;          +----+-------------+-------+--------+--------------------------------------------------------------+---------+---------+-----------------------------+---------+-------------+ 
| id | select_type | table | type | possible_keys            | key  | key_len | ref       | rows | Extra  | 
+----+-------------+-------+--------+--------------------------------------------------------------+---------+---------+-----------------------------+---------+-------------+ 
| 1 | SIMPLE  | a  | ALL | myps3t_gameusertrophyinfo_61a683d8,myps3t_gameusertrophyinfo | NULL | NULL | NULL      | 4004592 |    | 
| 1 | SIMPLE  | b  | eq_ref | PRIMARY              | PRIMARY | 4  | fabriciols_ps3t.a.trophy_id |  1 | Using where | 
+----+-------------+-------+--------+--------------------------------------------------------------+---------+---------+-----------------------------+---------+-------------+ 
2 rows in set (0.00 sec) 

爲什麼MySQL的不使用我的鑰匙? 此查詢需要30多秒,第一次表有4milion記錄...

- 編輯 -

爲quasnoi



mysql> SELECT COUNT(*), COUNT(DISTINCT addon_id), SUM(addon_id = 58) FROM myps3t_gametrophyinfo; 
+----------+--------------------------+--------------------+ 
| COUNT(*) | COUNT(DISTINCT addon_id) | SUM(addon_id = 58) | 
+----------+--------------------------+--------------------+ 
| 25976 |      160 |     6 | 
+----------+--------------------------+--------------------+ 
1 row in set (0.00 sec) 
+0

這是什麼查詢返回:'SELECT COUNT(*),COUNT(DISTINCT addon_id),SUM(addon_id = 58)FROM myps3t_gametrophyinfo'? – Quassnoi 2011-03-22 17:28:19

+0

請注意,您正在使用CROSS JOIN,嘗試使用其他一些連接並測試執行速度。 – 2011-03-22 17:31:58

回答

2

MySQL選擇a作爲一個領先的桌子和b爲一個驅動的桌子。它在b上使用PRIMARY KEY作爲連接。

創建索引myps3t_gametrophyinfo (addon_id),這樣b將更有可能被選爲主表。

+0

完美!這項工作就像一個魅力!但是,有什麼地方可以閱讀來理解這個mysql的選擇嗎? – fabriciols 2011-03-22 17:59:04

+0

@fabriciols:http://dev.mysql.com/doc/refman/5.5/en/mysql-indexes.html – Quassnoi 2011-03-22 18:21:41

0

您可以嘗試

select * from 
myps3t_gametrophyinfo b 
STRAIGHT_JOIN myps3t_gameusertrophyinfo a ON (a.trophy_id = b.id) 
WHERE b.addon_id = 58; 
0

我可能會重寫查詢,試圖獲得一個更健全的執行路徑。我覺得像下面是更容易得到你想要的性能,是你在做一個人的讀者有什麼更清晰

SELECT * FROM myps3t_gametrophyinfo a LEFT JOIN myps3t_gameusertrophyinfo b ON a.id = b.trophy_id WHERE a.addon_id=58;