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;
關於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;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-2122648/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- index range scan,index fast full scan,index skip scan發生的條件IndexAST
- Index Range Scan (214)Index
- INDEX UNIQUE SCAN,INDEX FULL SCAN和INDEX FAST FULL SCANIndexAST
- Index Range Scan成本 Histogram 和 10053IndexHistogram
- INDEX FULL SCAN和INDEX FAST FULL SCAN區別IndexAST
- index full scan 和 index FAST full scan 區別IndexAST
- INDEX FULL SCAN和INDEX FAST FULL SCAN的區別IndexAST
- Index Range Scan成本與10053Index
- [總結]關於index range scans & INDEX (FAST FULL SCAN)IndexAST
- 高效的SQL(index range scan優化排序)SQLIndex優化排序
- Index Unique Scan (213)Index
- INDEX RANGE SCAN DESCENDING的邏輯讀問題Index
- rowid,index,INDEX FULL SCAN,INDEX FAST FULL SCAN|IndexAST
- index full scan 和 index fast full scan (IFS,FFS)的不同IndexAST
- Index Full Scan vs Index Fast Full ScanIndexAST
- Index Full Scan 與 Index Fast Full ScanIndexAST
- Index的掃描方式:index full scan/index fast full scanIndexAST
- 高效的SQL(Index unique scan最優化)SQLIndex優化
- pk 、unique index 和 index 區別Index
- Index Full Scan 與 Index Fast Full Scan (Final)IndexAST
- INDEX SKIP SCANIndex
- mysql loose index scan的實現MySqlIndex
- Index Full Scan和Index Fast Full Scan行為差異分析(上)IndexAST
- Index Full Scan和Index Fast Full Scan行為差異分析(下)IndexAST
- Clustered Index Scan and Clustered Index SeekIndex
- 索引唯一性掃描(INDEX UNIQUE SCAN)索引Index
- [20230908]Oracle Index Range Scan with LIKE Condition on Wildcard '_'.txtOracleIndex
- index fast full scan 和 nullIndexASTNull
- 理解index skip scanIndex
- PostgreSQL DBA(119) - pgAdmin(LIMIT:Index Scan vs Bitmap Index Scan)SQLMITIndex
- 簡單談談MySQL的loose index scanMySqlIndex
- [轉貼]Skip Scan IndexIndex
- 關於INDEX SKIP SCANIndex
- Oracle vs PostgreSQL Develop(31) - Index Only ScanOracleSQLdevIndex
- oracle hint_skip scan_index_ssOracleIndex
- MYSQL 中的GROUP BY 的方式 (1)(loose index scan鬆散掃描 tight index scan緊湊掃描)MySqlIndex
- Fast full index scan 淺析ASTIndex
- 索引優化index skip scan索引優化Index