1

私はテーブルを作成しています

create table temp_test2 (
date_id int(11) NOT NULL DEFAULT '0',
 `date` date NOT NULL,
  `day` int(11) NOT NULL,
PRIMARY KEY (date_id)
);

create table temp_test1 (
date_id int(11) NOT NULL DEFAULT '0',
 `date` date NOT NULL,
  `day` int(11) NOT NULL,
PRIMARY KEY (date_id)
);


explain select * from temp_test as t  inner join temp_test2 as t2 on (t2.date_id =t.date_id) limit 3;

+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
| id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | Extra                                              |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+
|  1 | SIMPLE      | t     | ALL  | date_id       | NULL | NULL    | NULL |    4 | NULL                                               |
|  1 | SIMPLE      | t2    | ALL  | date_id       | NULL | NULL    | NULL |    4 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------+---------------+------+---------+------+------+----------------------------------------------------+

code_id両方のテーブルでキーが使用されていない理由ですがcode_id=something、条件付きで使用するとキーを使用しているため、

explain select * from temp_test as t  inner join temp_test2 as t2 on (t2.date_id =t.date_id and t.date_id =1) limit 3;

+----+-------------+-------+-------+-------------------------------------+---------+---------+-------+------+-------+
| id | select_type | table | type  | possible_keys                       | key     | key_len | ref   | rows | Extra |
+----+-------------+-------+-------+-------------------------------------+---------+---------+-------+------+-------+
|  1 | SIMPLE      | t     | const | PRIMARY,date_id,date_id_2,date_id_3 | PRIMARY | 4       | const |    1 | NULL  |
|  1 | SIMPLE      | t2    | ref   | date_id,date_id_2,date_id_3         | date_id | 4       | const |    1 | NULL  |
+----+-------------+-------+-------+-------------------------------------+---------+---------+-------+------+-------+

(unique,composite primary,composite) キーも試しましたが、機能しません。なぜこれがそうなのか、誰でも説明できますか?

4

1 に答える 1

1

テーブルに含まれるレコードの数が非常に少ないため、オプティマイザーはインデックスを使用する価値がないと判断します。テーブル スキャンも同様に有効です。

また、すべてのフィールド ( ) を選択しSELECT *ました。インデックスを使用しJOINて行スキャンを実行した場合でも、完全な内容を取得するには行スキャンが必要になります。

次の場合、クエリはインデックスを使用する可能性が高くなります。

  • date_idフィールドのみを選択しました
  • 4行以上ありましたtemp_test
于 2013-08-29T13:04:38.027 に答える