oracle index unique scan/index range scan和mysql range/const/ref/eq_ref的区别

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 关于oracle index unique scan/index range scan和mysql range/const/ref/eq_ref type的区别    关于ORACLE index unique scan和index range scan区别在于是否...
关于oracle index unique scan/index range scan和mysql range/const/ref/eq_ref type的区别


   关于ORACLE index unique scan和index range scan区别在于是否索引是唯一的,如果=操作谓词有唯一索引则使用unique scan否则则使用range scan
但是这种定律视乎在MYSQL中不在成立

如下执行
kkkm2 id为主键


mysql> explain extended select * from kkkm2 where id=2;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | filtered | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | kkkm2 | const | PRIMARY,key_t | PRIMARY | 4       | const |    1 |   100.00 | NULL  |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)


我们发现他使用了type const这个代表是查询一条记录并且进行了转换为了常量
mysql> show warnings;
+-------+------+-------------------------------------------------------------------------------------+
| Level | Code | Message                                                                             |
+-------+------+-------------------------------------------------------------------------------------+
| Note  | 1003 | /* select#1 */ select '2' AS `id`,'gaopeng2' AS `name2` from `test`.`kkkm2` where 1 |
+-------+------+-------------------------------------------------------------------------------------+
确实如此


但是如果我们进行UPDATE


mysql> explain update kkkm2 set name2='gaopeng1000' where id=1;
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref   | rows | Extra       |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
|  1 | SIMPLE      | kkkm2 | range | PRIMARY,key_t | PRIMARY | 4       | const |    1 | Using where |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
1 row in set (0.03 sec)


这里问题来了,为什么我明明是主键为什么执行计划是type是range呢?ORACLE在这里肯定是INDEX UNIQUE SCAN,
但是MYSQL这里使用range。
给人的感觉eq_ref视乎是更合适的type,唯一扫描嘛,但是看看文档解释如下:
eq_ref can be used for indexed columns that are compared using the = operator. The comparison
value can be a constant or an expression that uses columns from tables that are read before this
table. In the following examples, MySQL can use an eq_ref join to process ref_table:

SELECT * FROM ref_table,other_table
WHERE ref_table.key_column=other_table.column;


SELECT * FROM ref_table,other_table
WHERE ref_table.key_column_part1=other_table.column
AND ref_table.key_column_part2=1;


这里 MySQL can use an eq_ref join to process ref_table明确说了eq_ref是用于join的,对于单表不适用
他用在被驱动表的连接字段有唯一索引的情况下。






而ref呢,实际上他也是用于连接用在被驱动表是非唯一索引的情况,并且适用于单表谓词非唯一的情况  如下:
   All rows with matching index values are read from this table for each combination of rows from the
previous tables. refis used if the join uses only a left most prefix of the key or if the key is not a
PRIMARY KEY or UNIQUE index (in other words, if the join cannot select a single row based on the
key value). If the key that is used matches only a few rows, this is a good join type.
   ref can be used for indexed columns that are compared using the =or <=>operator. In the
following examples, MySQL can use a ref join to process ref_table:


SELECT * FROM ref_tableWHERE key_column=expr;


SELECT * FROM ref_table,other_table
WHERE ref_table.key_column=other_table.column;


SELECT * FROM ref_table,other_table
WHERE ref_table.key_column_part1=other_table.column
AND ref_table.key_column_part2=1;


如果只考虑 = 操作:
   感觉mysql的ref 和 oracle的index range scan类似,不管单表或者jion都可以使用,
适用于索引是非唯一的情况。
   但是mysql的eq_ref 和oracle的index unique scan 并不同,因为eq_ref只会用在join的
情况下并且被驱动表是唯一的情况下,在单表谓词查询使用唯一索引的情况eq_ref并不会出现,
出现的是type const或者type range


如果> < 等范围操作,出现的一定是type range了,这个和ORACLE一样一旦唯一键出现了范围
条件出现的一定是INDEX RANGE SCAN。


range描述如下:
Only rows that are in a given range are retrieved, using an index to select the rows. The key column
in the output row indicates which index is used. The key_len contains the longest key part that was
used. The ref column is NULL for this type.
range scan be used when a key column is compared to a constant using any of the =, <>, >, >=, <,
<=, IS NULL, <=>, BETWEEN, or IN () operators:
SELECT * FROM tbl_name
WHERE key_column= 10; 


SELECT * FROM tbl_name
WHERE key_columnBETWEEN 10 and 20;

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
1月前
|
存储 SQL 运维
TIDB和MySQL的区别
TIDB和MySQL的区别
158 0
|
1月前
|
SQL Oracle 关系型数据库
避坑,Oracle中left join 与 (+) 的区别
避坑,Oracle中left join 与 (+) 的区别
|
1月前
|
存储 缓存 关系型数据库
Mysql的两种存储引擎以及区别
Mysql的两种存储引擎以及区别
14 1
|
2月前
|
SQL 人工智能 关系型数据库
mysql中in 和exists的区别
mysql中in 和exists的区别
|
3月前
|
Oracle 关系型数据库 MySQL
mysql数据库和Oracle的区别
mysql数据库和Oracle的区别
50 1
|
3月前
|
存储 数据库
MySQL-函数和存储过程区别
MySQL-函数和存储过程区别
46 0
|
7天前
|
存储 关系型数据库 MySQL
MySQL引擎对决:深入解析MyISAM和InnoDB的区别
MySQL引擎对决:深入解析MyISAM和InnoDB的区别
21 0
|
1月前
|
存储 缓存 关系型数据库
MySQL两种存储引擎及区别
MySQL两种存储引擎及区别
21 4
MySQL两种存储引擎及区别
|
10天前
|
存储 关系型数据库 MySQL
深入理解MySQL中varchar和text的区别
在MySQL中,varchar和text都是用于存储文本数据的数据类型。varchar是可变长度字符串,存储时按实际长度分配空间,适合存储较短的、长度可变的字符串,如用户名。text类型用于存储大量文本,始终占用足够空间,适合文章内容。varchar在存储和查询时可能更快,可被索引,而text需特殊搜索技术。在数据库设计时,应根据存储需求和性能平衡选择。
38 0
|
25天前
|
Oracle 关系型数据库 MySQL
Seata常见问题之oracle 数据库 报 just support mysql如何解决
Seata 是一个开源的分布式事务解决方案,旨在提供高效且简单的事务协调机制,以解决微服务架构下跨服务调用(分布式场景)的一致性问题。以下是Seata常见问题的一个合集
53 0

推荐镜像

更多