2完全相同的mysql查询给出2个不同的'解释'输出:为什么?

时间:2011-07-28 23:53:27

标签: mysql explain

我发现了一个非常奇怪的mysql行为:当我运行两次特定查询时,第二次对此查询的解释是不同的:

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)

这怎么可能???

1 个答案:

答案 0 :(得分:4)

查看rows列。引擎能够收集更多统计信息 - 所以下次它会尝试使用更好的计划。

快乐的编码。