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

关于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;

时间: 2024-09-01 14:34:00

oracle index unique scan/index range scan和mysql range/const/ref/eq_ref的区别的相关文章

Sql Server中的表访问方式Table Scan, Index Scan, Index Seek

  Sql Server中的表访问方式Table Scan, Index Scan, Index Seek 0.参考文献 oracle表访问方式 Index Seek和Index Scan的区别以及适用情况 1.oracle中的表访问方式 在oracle中有表访问方式的说法,访问表中的数据主要通过三种方式进行访问: 全表扫描(full table scan),直接访问数据页,查找满足条件的数据 通过rowid扫描(table access by rowid),如果知道数据的rowid,那么直接通

跳跃式索引(Skip Scan Index)浅析

在Oracle9i中,有一个新的特性:跳跃式索引(Skip Scan Index).当表有一个复合索引,而在查询中有除了索引中第一列的其他列作为条件,并且优化器模式为CBO,这时候查询计划就有可能使用到SS.此外,还可以通过使用提示index_ss(CBO下)来强制使用SS. 举例: SQL> create table test1 (a number, b char(10), c varchar2(10)); Table created. SQL> create index test_idx1

Oracle技术:使用Index提示强制使用索引

虽然索引并不总会快于全表扫描,但是很多时候我们希望Oracle使用索引来执行某些SQL,这时候我们可以通过index hints来强制SQL使用index. Index Hints的格式如下: /*+ INDEX ( table [index [index]...] ) */我们简单看一下这个提示的用法(范例为Oracle10g数据库): SQL> create table t as select username,password from dba_users;Table created.SQ

ORACLE虚拟索引(Virtual Index)

ORACLE虚拟索引(Virtual Index)   虚拟索引概念   虚拟索引(Virtual Indexes)是一个定义在数据字典中的假索引(fake index),它没有相关的索引段.虚拟索引的目的是模拟索引的存在而不用真实的创建一个完整索引.这允许开发者创建虚拟索引来查看相关执行计划而不用等到真实创建完索引才能查看索引对执行计划的影响,并且不会增加存储空间的使用.如果我们观察到优化器生成了一个昂贵的执行计划并且SQL调整指导建议我们对某些的某列创建索引,但在生产数据库环境中创建索引与测

(bugfix in 5.6.21) DUPLICATES IN UNIQUE SECONDARY INDEX BECAUSE OF FIX OF BUG#68021

(bugfix in 5.6.21) DUPLICATES IN UNIQUE SECONDARY INDEX BECAUSE OF FIX OF BUG#68021 在MySQL5.6.21版本里,fix了这样一个有趣(坑爹)的bug,影响5.6.12之后及5.6.21之间的版本 InnoDB: With a transaction isolation level less than or equal to READ COMMITTED, gap locks were not taken wh

Oracle索引分裂(Index Block Split)

Oracle索引分裂(Index Block Split) 索引分裂:index  block split : 就是索引块的分裂,当一次DML 事务操作修改了索引块上的数据,但是旧有的索引块没有足够的空间去容纳新修改的数据,那么将分裂出一个新的索引块,旧有块的部分数据放到新开辟的索引块上去. 分裂的类型:根节点分裂,分支节点分裂,叶节点分裂(最频繁发生,对性能影响最直接) 按照数据迁移量的比例,将索引分裂分为两种类型:9-1分裂和5-5分裂. 9-1分裂:绝大部分数据还保留在旧有节点上,仅有非常

MySQL hash index VS Btree index

MySQL AHI(adaptive hash index):没有牺牲任何的事物特点和可靠性; 根据搜索的匹配模式,MySQL会利用 B-Tree index key 前半部分(利用btree index 所能找到的部分)长度任意建立hash index.hash index根据需求只对访问频率较高的page中的index建立hashindex 如果一个表的数据全部在内存里面,hash index可以加快查询速度:innodb本身有监控index 查询频率的机制:通过hash index提高查询

请问403 Forbidden nginx怎么解决,目录权限没问题,nginx有设置index.html和index.php

问题描述 请问403Forbiddennginx怎么解决,目录权限没问题,nginx有设置index.html和index.php

oracle函数和存数过程转成mysql

问题描述 oracle函数和存数过程转成mysql create or replace function public_f_get_bsc012 ( v_id in varchar2 ) return varchar2 as v_temp varchar2(200); begin select bsc012 into v_temp from sc05 where bsc010=v_id; return v_temp; exception when others then return ''; e