2011-07-28 58 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的`

相关问题