oracle 5种索引方式,MySQL使用索引的几种方式
MySQL使用索引的幾種方式
之前的blog有介紹令人迷糊的extra中信息,本文我們重點來看看當執行計劃使用索引的時候
extra中幾種顯示的場景
版本:Mysql 5.6.14
測試表結構如下:
CREATE TABLE `snapshot` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`snap_id` int(11) DEFAULT NULL,
`name` varchar(500) DEFAULT NULL,
`value` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `snap_id_ix` (`snap_id`)
) ENGINE=InnoDB? DEFAULT CHARSET=utf8
在snap_id上創建了索引,然后插入一些測試數據。
1,snap_id為等于的操作
mysql> explain extended select * from snapshot where snap_id=10;
+----+-------------+----------+------+---------------+------------+---------+-------+------+----------+-------+
| id | select_type | table??? | type | possible_keys | key??????? | key_len | ref?? | rows | filtered | Extra |
+----+-------------+----------+------+---------------+------------+---------+-------+------+----------+-------+
|? 1 | SIMPLE????? | snapshot | ref? | snap_id_ix??? | snap_id_ix | 5?????? | const |??? 1 |?? 100.00 | NULL? |
+----+-------------+----------+------+---------------+------------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
這個雖然使用了索引,但是extra中沒有任何信息,顯示為空
2,使用索引條件,先掃描索引,然后再通過索引掃描表
mysql>? explain extended select *? from snapshot where snap_id<100;
+----+-------------+----------+-------+---------------+------------+---------+------+------+----------+-----------------------+
| id | select_type | table??? | type? | possible_keys | key??????? | key_len | ref? | rows | filtered | Extra???????????????? |
+----+-------------+----------+-------+---------------+------------+---------+------+------+----------+-----------------------+
|? 1 | SIMPLE????? | snapshot | range | snap_id_ix??? | snap_id_ix | 5?????? | NULL |??? 1 |?? 100.00 | Using index condition |
+----+-------------+----------+-------+---------------+------------+---------+------+------+----------+-----------------------+
1 row in set, 1 warning (0.00 sec)
通過索引條件,然后掃描表數據,extra是Using index condition
3,使用cover index,只查索引中的列,直接從索引中返回數據就可以了
mysql>? explain extended select snap_id from snapshot;
+----+-------------+----------+-------+---------------+------------+---------+------+------+----------+-------------+
| id | select_type | table??? | type? | possible_keys | key??????? | key_len | ref? | rows | filtered | Extra?????? |
+----+-------------+----------+-------+---------------+------------+---------+------+------+----------+-------------+
|? 1 | SIMPLE????? | snapshot | index | NULL????????? | snap_id_ix | 5?????? | NULL | 6415 |?? 100.00 | Using index |
+----+-------------+----------+-------+---------------+------------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
mysql>? explain extended select snap_id from snapshot where snap_id=10;
+----+-------------+----------+------+---------------+------------+---------+-------+------+----------+-------------+
| id | select_type | table??? | type | possible_keys | key??????? | key_len | ref?? | rows | filtered | Extra?????? |
+----+-------------+----------+------+---------------+------------+---------+-------+------+----------+-------------+
|? 1 | SIMPLE????? | snapshot | ref? | snap_id_ix??? | snap_id_ix | 5?????? | const |??? 1 |?? 100.00 | Using index |
+----+-------------+----------+------+---------------+------------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.01 sec)
mysql>? explain extended select snap_id from snapshot where snap_id>10;
+----+-------------+----------+-------+---------------+------------+---------+------+------+----------+--------------------------+
| id | select_type | table??? | type? | possible_keys | key??????? | key_len | ref? | rows | filtered | Extra??????????????????? |
+----+-------------+----------+-------+---------------+------------+---------+------+------+----------+--------------------------+
|? 1 | SIMPLE????? | snapshot | range | snap_id_ix??? | snap_id_ix | 5?????? | NULL | 6367 |?? 100.00 | Using where; Using index |
+----+-------------+----------+-------+---------------+------------+---------+------+------+----------+--------------------------+
1 row in set, 1 warning (0.00 sec)
在extra出現的是Using index提示,表明只使用索引即可,不需要對表進行掃描操作.
4,?? 接下來我們看連接的操作,假設為snapshot作為連接的內表
mysql> explain
-> select *
-> from t1 join snapshot
-> on (t1.a=snapshot.snap_id)
-> ;
+----+-------------+----------+------+---------------+------------+---------+----------+------+-------+
| id | select_type | table??? | type | possible_keys | key??????? | key_len | ref????? | rows | Extra |
+----+-------------+----------+------+---------------+------------+---------+----------+------+-------+
|? 1 | SIMPLE????? | t1?????? | ALL? | PRIMARY?????? | NULL?????? | NULL??? | NULL???? |??? 3 | NULL? |
|? 1 | SIMPLE????? | snapshot | ref? | snap_id_ix??? | snap_id_ix | 5?????? | db1.t1.a |? 200 | NULL? |
+----+-------------+----------+------+---------------+------------+---------+----------+------+-------+
2 rows in set (0.00 sec)
通過執行計劃可以看出對snapshot也是索引來訪問的,外表傳過來的值,在內表很類似于snap_id=XX,所以和第1中的執行
計劃很類似.除ref之外,一個是來自于常量(const),而本例中是來自表t1的索引db.t1.a.
總結
以上是生活随笔為你收集整理的oracle 5种索引方式,MySQL使用索引的几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c#连接oracle11,C#连接远程o
- 下一篇: linux cmake编译源码,linu