简体   繁体   English

为什么优化器使用“ ALL”类型而不是“ index”类型?

[英]Why optimizer use 'ALL' type instead of 'index' type?

I'm currently learning execution plan in MySql . 我目前正在学习MySql execution plan
And I use dump data from here. 我从这里使用转储数据。

https://dev.mysql.com/doc/employee/en/sakila-structure.html https://dev.mysql.com/doc/employee/zh/sakila-structure.html

I added new index to from_date field of dept_emp table, named ix_fromdate . 我向dept_emp表的from_date字段添加了新索引,名为ix_fromdate
And I typed like below 我输入如下

EXPLAIN
SELECT *
FROM dept_emp
WHERE from_date>='1985-01-01'  

Execution plain result like below 执行普通结果如下
在此处输入图片说明

I understand that rows value is closer amount of this table rows amount and optimizer didn't use range index. 我知道rows值是该表中行数的更接近值,并且优化器未使用range索引。
And.. I think, type should be index , but ALL . 我认为type应该是index ,但是ALL
Why they did table full scan ? 他们为什么要进行table full scan
There's index for ix_fromdate , why they didn't use it though? ix_fromdate的索引,为什么他们没有使用它?
I know index full scan is better than table full scan ... 我知道index full scantable full scan更好...
Do I know it wrong? 我知道错吗?

  • Even if you have INDEX(from_date) ? 即使您有INDEX(from_date)
  • But more than about 20% of the table satisfies WHERE from_date>='1985-01-01' ? 但是,超过20%的表满足WHERE from_date>='1985-01-01'吗?

Then the Optimizer decides that it is probably faster to simply scan through the data, filtering as it goes. 然后,优化器决定仅扫描数据并进行过滤就可能更快。 The alternative (using the index) could be costly because of bouncing between the index BTree and the data BTree. 替代方法(使用索引)可能会很昂贵,因为索引BTree和数据BTree之间会发生反弹。

Use a newer date; 使用较新的日期; the Optimizer may change its mind and use the index. 优化器可能会改变主意并使用索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM