mysql索引的創(chuàng)建,新增,刪除,查看
索引定義
MySQL官方對索引的定義是:索引(Index)是幫助MySQL高效獲取數(shù)據(jù)的數(shù)據(jù)結(jié)構(gòu)
。索引最形象的比喻就是圖書的目錄。注意只有在大量數(shù)據(jù)中查詢時索引才顯得有意義
。
在MySQL中索引是在存儲引擎層實現(xiàn)的``,
而不是在服務(wù)器層實現(xiàn)的,
所以不同存儲引擎具有不同的索引類型和實現(xiàn)```。
索引的優(yōu)缺點:
優(yōu)點
1、合理的索引提高了查詢的效率,但是insert update delete等性能會變差
2)創(chuàng)建索引可以大幅提高系統(tǒng)性能,幫助用戶提高查詢的速度;
3)通過索引的唯一性,可以保證數(shù)據(jù)庫表中的每一行數(shù)據(jù)的唯一性;
4)可以加速表與表之間的鏈接;
5)降低查詢中分組和排序的時間
缺點
索引的存儲需要占用磁盤空間;
當數(shù)據(jù)的量非常巨大時,索引的創(chuàng)建和維護所耗費的時間也是相當大的;
當每次執(zhí)行CRU操作時,索引也需要動態(tài)維護,降低了數(shù)據(jù)的維護速度
存儲引擎介紹
**存儲引擎就是存儲數(shù)據(jù)、建立索引、更新/查詢數(shù)據(jù)等技術(shù)的實現(xiàn)方式。存儲引擎是基于表的,而不是基于庫的,所以存儲引擎也可被稱為表類型**
索引分類:
按數(shù)據(jù)結(jié)構(gòu)分類:B+tree索引、Hash索引、Full-text索引。
按物理存儲分類(innodb存儲引擎):聚集索引、非聚集索引(也叫二級索引、輔助索引)。
按字段特性分類:主鍵索引(PRIMARY KEY)、唯一索引(UNIQUE)、普通索引(INDEX)、全文索引(FULLTEXT)。
-
主鍵索引(PRIMARY KEY)
建立在主鍵上的索引被稱為主鍵索引
,一張數(shù)據(jù)表只能有一個主鍵索引,索引列值不允許有空值,通常在創(chuàng)建表時一起創(chuàng)建。 -
唯一索引(UNIQUE)
建立在UNIQUE字段上的索引被稱為唯一索引,一張表可以有多個唯一索引,索引列值允許為空,列值中出現(xiàn)多個空值不會發(fā)生重復(fù)沖突。
CREATE UNIQUE INDEX indexName ON tableName(tableColumns(length))
CREATE UNIQUE INDEX manager_id ON dept(manager_id)
- 普通索引(INDEX) 建立在普通字段上的索引被稱為普通索引。
create index 索引名 on 表名(字段名)
單個索引
create index idx_user_id on user(id)
聯(lián)合索引
create index idx_user_id _name_age on user(id,name,age)
idx_user_id:索引簡寫+表+索引字段
- 全文索引(FULLTEXT) MyISAM
存儲引擎支持Full-text索引,用于查找文本中的關(guān)鍵詞,而不是直接
比較是否相等。Full-text索引一般使用倒排索引實現(xiàn),它記錄著關(guān)鍵詞到其所在文檔的映射。
InnoDB 存儲引擎在 MySQL 5.6.4 版本中也開始支持Full-text索引
按字段個數(shù)分類:單列索引、聯(lián)合索引(也叫復(fù)合索引、組合索引)
MySQL索引按字段個數(shù)分類可分為:單列索引、聯(lián)合索引(也叫復(fù)合索引、組合索引)。
- 單列索引 :建立在單個列上的索引被稱為單列索引
查詢索引:
語法: show index from 表
主要參數(shù):
刪除索引
DROP INDEX <索引名> ON <表名>
drop index department_name on dept;
新建表中添加索引
① 普通索引
create table t_dept(
no int not null primary key,
name varchar(20) null,
sex varchar(2) null,
info varchar(20) null,
index index_no(no)
)
1
2
3
4
5
6
7
8
② 唯一索引
create table t_dept(
no int not null primary key,
name varchar(20) null,
sex varchar(2) null,
info varchar(20) null,
unique index index_no(no)
)
1
2
3
4
5
6
7
8
③ 全文索引
create table t_dept(
no int not null primary key,
name varchar(20) null,
sex varchar(2) null,
info varchar(20) null,
fulltext index index_no(no)
1
2
3
4
5
6
7
④ 多列索引
create table t_dept(
no int not null primary key,
name varchar(20) null,
sex varchar(2) null,
info varchar(20) null,
key index_no_name(no,name)
)
已建表中添加索引:
① 普通索引
語法:create index 索引字段名 on 表(索引字段名)
create index string on books(string);
② 唯一索引
語法:create unique index 索引字段名 on 表(索引字段名)
create unique index index_name
on t_dept(name);
③ 全文索引
create fulltext index index_name
on t_dept(name);
④ 多列索引
create index index_name_no
on t_dept(name,no)
以修改表的方式添加索引
① 普通索引
alter table t_dept
add index index_name(name);
② 唯一索引
alter table t_dept
add unique index index_name(name);
③ 全文索引
alter table t_dept
add fulltext index_name(name);文章來源:http://www.zghlxwxcb.cn/news/detail-426230.html
④ 多列索引
alter table t_dept
add index index_name_no(name,no);文章來源地址http://www.zghlxwxcb.cn/news/detail-426230.html
到了這里,關(guān)于Mysql高級知識-------索引的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!