Mysql 为什么条件上的内部联接不使用键
我正在创建一个表作为Mysql 为什么条件上的内部联接不使用键,mysql,Mysql,我正在创建一个表作为 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(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\u id
键,但当我在条件中使用code\u 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 |
+----+-------------+-------+-------+-------------------------------------+---------+---------+-------+------+-------+
我也尝试了(唯一、复合主、复合)键,但它不起作用。
有人能解释为什么会这样吗?因为您的表包含的记录非常少,所以优化者认为不值得使用索引。表格扫描也可以 此外,您还选择了所有字段(
SELECT*
),如果它使用索引执行JOIN
,则仍然需要行扫描才能获得完整内容
在以下情况下,查询更有可能使用索引:
- 您只选择了
字段date\u id
temp\u测试中超过4行
SELECT*
),如果它使用索引执行JOIN
,则仍然需要行扫描才能获得完整内容
在以下情况下,查询更有可能使用索引:
- 您只选择了
字段date\u id
temp\u测试中超过4行
child\u depth
索引。您是对的,它使用的是child\u depth键,但我的问题是为什么不使用复合主键(child\u code\u id,child\u depth)嗨,我的实际表记录将超过10亿条,而加入10亿条记录时,主键(date\u id,day)不起作用,它选择所有表range@user2728070抱歉,我可能误解了:对于一个有10亿条记录的表,您是否真的得到了相同的执行计划?这里是我的表的解释语句************************************3。行************************id:3选择类型:派生表:t类型:引用可能的关键字:主关键字、子关键字、子关键字、子关键字:子关键字:子关键字:8引用:const行:19156额外:使用索引;使用临时设备;使用文件排序*********************************************4。行**********************复合主键(子代码id,子代码深度),我在条件(即)on(代码id=子代码id)和子代码深度中使用子代码id=0@user2728070上面写着“钥匙:儿童深度”。此查询确实使用了child\u depth
索引。您是对的,它使用的是child\u depth键,但我的问题是为什么它不使用复合主键(child\u code\u id,child\u depth)