2011-07-28 78 views
2

我發現了一個很奇怪的mysql的行爲:當我運行一個特定的查詢兩次,這個查詢的解釋是不同的,第二次:2完全相同的mysql查詢給出了2個不同的'explain'輸出:爲什麼?

query = SELECT `twstats_twwordstrend`.`id`, `twstats_twwordstrend`.`created`, `twstats_twwordstrend`.`freq`, `twstats_twwordstrend`.`word_id` FROM `twstats_twwordstrend` INNER JOIN `twstats_twwords` ON (`twstats_twwordstrend`.`word_id` = `twstats_twwords`.`id`) WHERE (`twstats_twwords`.`name` = '@ladygaga' AND `twstats_twwordstrend`.`created` > '2011-01-28 01:30:19'); 

1st query execution and then run explain : 

mysql> EXPLAIN SELECT `twstats_twwordstrend`.`id`, `twstats_twwordstrend`.`created`, `twstats_twwordstrend`.`freq`, `twstats_twwordstrend`.`word_id` FROM `twstats_twwordstrend` INNER JOIN `twstats_twwords` ON (`twstats_twwordstrend`.`word_id` = `twstats_twwords`.`id`) WHERE (`twstats_twwords`.`name` = '@ladygaga' AND `twstats_twwordstrend`.`created` > '2011-01-28 01:30:19'); 
+----+-------------+----------------------+--------+-------------------------------+---------+---------+-------------------------------------------+---------+-------------+ 
| id | select_type | table    | type | possible_keys     | key  | key_len | ref          | rows | Extra  | 
+----+-------------+----------------------+--------+-------------------------------+---------+---------+-------------------------------------------+---------+-------------+ 
| 1 | SIMPLE  | twstats_twwordstrend | ALL | twstats_twwordstrend_4b95d890 | NULL | NULL | NULL          | 4877401 | Using where | 
| 1 | SIMPLE  | twstats_twwords  | eq_ref | PRIMARY      | PRIMARY | 4  | statweestics.twstats_twwordstrend.word_id |  1 | Using where | 
+----+-------------+----------------------+--------+-------------------------------+---------+---------+-------------------------------------------+---------+-------------+ 
2 rows in set (0.00 sec) 

2nd query execution and then run explain : 

mysql> EXPLAIN SELECT `twstats_twwordstrend`.`id`, `twstats_twwordstrend`.`created`, `twstats_twwordstrend`.`freq`, `twstats_twwordstrend`.`word_id` FROM `twstats_twwordstrend` INNER JOIN `twstats_twwords` ON (`twstats_twwordstrend`.`word_id` = `twstats_twwords`.`id`) WHERE (`twstats_twwords`.`name` = '@ladygaga' AND `twstats_twwordstrend`.`created` > '2011-01-28 01:30:19'); 
+----+-------------+----------------------+------+-------------------------------+-------------------------------+---------+---------------------------------+--------+-------------+ 
| id | select_type | table    | type | possible_keys     | key       | key_len | ref        | rows | Extra  | 
+----+-------------+----------------------+------+-------------------------------+-------------------------------+---------+---------------------------------+--------+-------------+ 
| 1 | SIMPLE  | twstats_twwords  | ALL | PRIMARY      | NULL       | NULL | NULL       | 222994 | Using where | 
| 1 | SIMPLE  | twstats_twwordstrend | ref | twstats_twwordstrend_4b95d890 | twstats_twwordstrend_4b95d890 | 4  | statweestics.twstats_twwords.id |  15 | Using where | 
+----+-------------+----------------------+------+-------------------------------+-------------------------------+---------+---------------------------------+--------+-------------+ 
2 rows in set (0.00 sec) 

mysql> describe twstats_twwords; 
+---------+--------------+------+-----+---------+----------------+ 
| Field | Type   | Null | Key | Default | Extra   | 
+---------+--------------+------+-----+---------+----------------+ 
| id  | int(11)  | NO | PRI | NULL | auto_increment | 
| created | datetime  | NO |  | NULL |    | 
| name | varchar(140) | NO |  | NULL |    | 
+---------+--------------+------+-----+---------+----------------+ 
3 rows in set (0.00 sec) 

mysql> describe twstats_twwordstrend; 
+---------+----------+------+-----+---------+----------------+ 
| Field | Type  | Null | Key | Default | Extra   | 
+---------+----------+------+-----+---------+----------------+ 
| id  | int(11) | NO | PRI | NULL | auto_increment | 
| created | datetime | NO |  | NULL |    | 
| freq | double | NO |  | NULL |    | 
| word_id | int(11) | NO | MUL | NULL |    | 
+---------+----------+------+-----+---------+----------------+ 
4 rows in set (0.00 sec) 

這可怎麼可能?

+0

郵政結果CREATE TABLE twstats_twwords',請 – Timur

+0

CREATE TABLE'twstats_twwords'( 'id' INT(11)NOT NULL AUTO_INCREMENT, 'created'日期時間NOT NULL, 'name' VARCHAR(140)NOT NULL, PRIMARY KEY('id'), UNIQUE KEY'name'('name')<----這個鍵剛添加在我的帖子後 )ENGINE = InnoDB AUTO_INCREMENT = 225038 DEFAULT CHARSET = utf8 – Eric

回答

4

查看rows列。引擎能夠收集更多的統計數據 - 所以下次它會嘗試使用更好的計劃。

快樂編碼。 SHOW的`

相關問題