2011-07-28 4 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

ポスト結果表twstats_twwords'をCREATE、 – Timur

+0

は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 `の