mysql索引型別:
1、普通索引
最基本的索引,它沒有任何限制,用於加速查詢。
建立方法:
a. 建表的時候一起建立
create table mytable ( name varchar(32) , index index_mytable_name (name) );
b. 建表後,直接建立索引
create index index_mytable_name on mytable(name);
c. 修改表結構
alter table mytable add index index_mytable_name (name);
注:如果是字串字段,還可以指定索引的長度,在列命令後面加上索引長度就可以了(例如:name(11))
2、唯一索引
索引列的值必須唯一,但允許有空值。如果是組合索引,則列值的組合必須唯一。
建立方法:
a. 建表的時候一起建立
create table mytable ( `name` varchar(32) , unique index_unique_mytable_name (`name`) );
b. 建表後,直接建立索引
create unique index index_mytable_name on mytable(name);
c. 修改表結構
alter table mytable add unique index index_mytable_name (name);
注:如果是字串字段,還可以指定索引的長度,在列命令後面加上索引長度就可以了(例如:name(11))
3、主鍵索引
是一種特殊的唯一索引,乙個表只能有乙個主鍵,不允許有空值。一般是在建表的時候同時建立主鍵索引。
建立方法:
a. 建表的時候一起建立
create table mytable ( `id` int(11) not null auto_increment , `name` varchar(32) , primary key (`id`) );
b. 修改表結構
alter table test.t1 add constraint t1_pk primary key (id);
注:如果是字串字段,還可以指定索引的長度,在列命令後面加上索引長度就可以了(例如:name(11))
4、組合索引
指多個欄位上建立的索引,只有在查詢條件中使用了建立索引時的第乙個字段,索引才會被使用。使用組合索引時遵循最左字首集合。
建立方法:
a. 建表的時候一起建立
create table mytable ( `id` int(11) , `name` varchar(32) , index index_mytable_id_name (`id`,`name`) );
b. 建表後,直接建立索引
create index index_mytable_id_name on mytable(id,name);
c. 修改表結構
alter table mytable add index index_mytable_id_name (id,name);
5、全文索引
主要用來查詢文字中的關鍵字,而不是直接與索引中的值相比較。
fulltext索引跟其它索引大不相同,它更像是乙個搜尋引擎,而不是簡單的where語句的引數匹配。
fulltext索引配合match against操作使用,而不是一般的where語句加like。
它可以在create table,alter table ,create index使用,不過目前只有char、varchar,text 列上可以建立全文索引。
建立方法:
a. 建表的時候一起建立
create table `article` ( `id` int(11) not null auto_increment , `title` char(250) not null , `contents` text null , `create_at` int(10) null default null , primary key (`id`), fulltext (contents) );
b. 建表後,直接建立索引
create fulltext index index_article_contents on article(contents);
c. 修改表結構
alter table article add fulltext index index_article_contents (contents);
總結雖然索引可以增加查詢資料,但對於更新、建立或者刪除的時候,需要去維護索引,導致效能會受影響,因此,索引也不能建立太多。
mysql索引型別介紹 mysql索引型別介紹
索引型別介紹 主鍵索引 primary key 要求關鍵字不能重複,也不能為null,同時增加主鍵約束 主鍵索引定義時,不能命名 唯一索引 unique index 要求關鍵字不能重複,同時增加唯一約束 普通索引 index 對關鍵字沒有要求 全文索引 fulltext key 關鍵字的 不是所有欄...
mysql索引型別介紹 mysql索引型別介紹
b 樹是多路平衡查詢樹,相對於平衡二叉樹,對父結點的直接子結點個數,不再僅限於2,可以指定m 自定義 這樣可以在樹的深度不大量增加的前提下,儲存更多的結點。b 樹是通常在檔案系統中使用。特點 a 樹的每個結點最多有m 自定義 子結點 b 若根結點不是葉子結點,則至少有兩個子結點 c 除根結點外的所有...
MySQL索引 索引型別
在mysql有兩種索引型別 hash b 樹 hash索引原理比較簡單就是利用了乙個hash表 說b 樹之前先要了解b 樹的資料結構。不廢話,先上圖。對b 樹做一些解釋,參考上圖。b 樹的資料都在葉子節點上 非葉子節點上的這些都是範圍。舉例 最上面的根節點上的資料是5,28,65代表的是它的三個子樹...