日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

oracle 5种索引方式,MySQL使用索引的几种方式

發布時間:2024/9/19 数据库 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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使用索引的几种方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。