国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

MySQL的索引是什么

發(fā)布時(shí)間:2021-09-14 18:07 來(lái)源:億速云 閱讀:0 作者:柒染 欄目: Mysql 歡迎投稿:712375056

本篇文章為大家展示了的索引是什么,內容簡(jiǎn)明扼要并且容易理解,絕對能使你眼前一亮,通過(guò)這篇文章的詳細介紹希望你能有所收獲。

和其它數據一樣,MySQL索引對表中指定列進(jìn)行排序后另外保存,用于快速查找具有特定值的行。如果沒(méi)有索引,必須從第一行開(kāi)始,讀取整個(gè)表查找,表越大,成本就越高。如果表中有相關(guān)列的索引,就可以快速確定要在數據文件中間查找的位置,而不必查看所有數據,比按順序讀取每一行快得多。

MySQL的PRIMARY KEY索引、UNIQUE索引、普通索引、FULLTEXT索引都使用B-trees存儲,Spatial索引使用R-trees存儲。

MySQL 5.7中的創(chuàng )建索引語(yǔ)法:

CREATE [UNIQUE | FULLTEXT | SPATIAL] INDEX index_name
[index_type]
ON tbl_name (key_part,…)
[index_option]
[algorithm_option | lock_option] …

key_part:
col_name [(length)] [ASC | DESC]

index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER parser_name
| COMMENT ‘string’

index_type:
USING {BTREE | HASH}

algorithm_option:
ALGORITHM [=] {DEFAULT | INPLACE | COPY}

lock_option:
LOCK [=] {DEFAULT | NONE | SHARED | EXCLUSIVE}

也可以使用alter table語(yǔ)句來(lái)創(chuàng )建索引。

MySQL支持前綴索引,即對索引字段的前N個(gè)字符創(chuàng )建索引。

訪(fǎng)問(wèn)原文提升閱讀體驗: https://www.modb.pro/db/22805?cyn 

root@database-one 21:56:  [gftest]> select * from emp;+--------+------+---------+------------+--------+| ename  | age  | sal     | hiredate   | deptno |+--------+------+---------+------------+--------+| 郭軍   |   27 | 8400.00 | 2019-12-08 |     10 || 劉杰   |   30 | 9100.00 | 2018-04-09 |     10 || 王艷   |   24 | 6000.00 | 2020-01-05 |     20 || 馬麗   |   26 | 7200.00 | 2018-07-06 |     30 || 肖偉   |   29 | 8700.00 | 2017-05-28 |     30 |+--------+------+---------+------------+--------+5 rows in set (0.02 sec)root@database-one 21:57:  [gftest]> show index from emp \GEmpty set (0.01 sec)root@database-one 21:57:  [gftest]> create index idx_emp_ename on emp(ename(2));Query OK, 0 rows affected (0.09 sec)Records: 0  Duplicates: 0  Warnings: 0root@database-one 21:57:  [gftest]> show index from emp \G*************************** 1. row ***************************Table: empNon_unique: 1Key_name: idx_emp_enameSeq_in_index: 1Column_name: enameCollation: ACardinality: 5Sub_part: 2Packed: NULLNull: YESIndex_type: BTREEComment:Index_comment:1 row in set (0.00 sec)root@database-one 21:57:  [gftest]> explain select * from emp where ename like '王%';+----+-------------+-------+------------+-------+---------------+---------------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type  | possible_keys | key           | key_len | ref  | rows | filtered | Extra       |+----+-------------+-------+------------+-------+---------------+---------------+---------+------+------+----------+-------------+|  1 | SIMPLE      | emp   | NULL       | range | idx_emp_ename | idx_emp_ename | 9       | NULL |    1 |   100.00 | Using where |+----+-------------+-------+------------+-------+---------------+---------------+---------+------+------+----------+-------------+1 row in set, 1 warning (0.07 sec)root@database-one 21:58:  [gftest]> explain select * from emp where sal>6000;+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+|  1 | SIMPLE      | emp   | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    5 |    33.33 | Using where |+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+1 row in set, 1 warning (0.03 sec)

可以看到,創(chuàng )建的索引是BTREE類(lèi)型,并且在按照ename查詢(xún)時(shí)被使用。

索引使用總有一些原則:

  • 對經(jīng)常在where、連接條件中出現的列考慮建索引。

  • 對選擇性比較好的列必要時(shí)建索引。比如用戶(hù)表中,身份證的列具有不同值,選擇性很好,索引被使用時(shí)特別高效;姓名列,選擇性較好,索引被使用時(shí)也比較高效;性別列,只含有男和女,選擇性很差,對此列建索引就沒(méi)有多大用處。

  • 不要過(guò)度創(chuàng )建索引。索引不是越多越好,每個(gè)索引都要占用磁盤(pán)空間,并會(huì )降低DML操作的性能。另外MySQL在生成執行計劃時(shí),過(guò)多的索引也會(huì )加重優(yōu)化器的工作,甚至可能干擾優(yōu)化器選擇不到最好的索引。

    免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

    最新版天堂资源中文官网| 中文成人无字幕乱码精品区| 日韩在线成年视频人网站观看| 日韩久久无码免费毛片软件| 永久免费AV网站| 日韩AV色综合网站|