mysql索引簡介

索引從本質上來說也是一種表,這種表存儲被列爲索引的列項值和指向真正完整記錄的指針。索引對用戶透明,只被數據庫引擎用來加速檢索真實記錄。有索引的表,insert和update操作會耗費更多時間而select則會變快,因爲insert和update操作同時也要insert和update索引值,但這些操作對用戶也透明。索引通常運用在where、join、order by語句中[1]。

在mysql中,index和key是同義詞,兩者可以互換[2]。一張表最多能有16個索引,每個索引最多由15個列組成。mysql中有4種類型索引。索引依據表類型(也既是存儲引擎)有不同存儲方式[3]。

  • primary key 可以指定一列或多列組合成索引,用來唯一確定一條記錄,每個指定爲primary key的項必須唯一,且不能爲空。一張表至多有一個primary key。設置了 primary key 的表自動爲 primary key
    建立索引。
  • unique key 與 primary key 類似,可以指定一列或多列組合成索引,用來唯一確定一條記錄。每個指定爲 unique key 的項必須唯一,允許有NULL值。一張表可以有多個 unique key。
  • fulltext類型索引只能用於MyISAM表,也只能用於char,varchar,text列項。
  • 普通 index,沒有唯一性限制,也沒有非空限制。
存儲引擎 可能的索引類型
MyISAM btree
InnoDB btree
Memory/Heap hash,btree
NDB btree,hash

索引可以在創建表的時候創建:

create table table_name (create_column_definition [, ...] );

其中 create_column_definition 可以替換成:

  • column_name column_definetion
  • [constraint [symbol]] primary key (column_name, ...) [index_type]
  • [constraint [symbol]] unique [index|key] (column_name, ...) [index_type]
  • {index|key} [index_name] (column_name, ...) [index_type]
  • {fulltext} [index | key] (column_name, ...) [index_type]
    其中 column_definetion 可以替換成:
  • data_type [not null | null] [default default_value]
  • [auto_increment] [unique [key] | [primary] key]
  • [comment 'string'] [reference_definition]

例如:

create table test(
`id` int unsigned not null auto_increment,
`data0` varchar(20),
`data1` varchar(20),
primary key (`id`),
);
create table test(
id int unsigned not null auto_increment primary key,
`data0` varchar(20),
`data1` varchar(20)
);

表創建之後也可以添加索引:
1)使用alter命令:

alter table table_name
[alter_specification [, alter_specification] ... ];

其中 alter_sepcification 可以替換成任意一種:

  • add [constraint [symbol]] primary key (index_cloumn_name, ... ) [index_type]
  • add [constraint [symbol]] unique [index|key] [index_name] (index_cloumn_name, ... ) [index_type]
  • add {index | key} [index_name] (index_cloumn_name, ... ) [index_type]
  • add [fulltext] [index|key] [index_name] (index_cloumn_name, ... ) [index_type]

其中 index_cloumn_name 可以替換成:column_name [(length) [asc|desc]]
其中 index_type 可以替換成:using {btree|hash}
例如:

alter table test add unique key `index_data0` (`data0` (10));

2)使用create命令:

create [unique|fulltext|spatial] index index_name
on table_name (index_cloumn_name, ... ) [index_type];

其中 index_cloumn_name 可以替換成:column_name [(length) [asc|desc]]
其中 index_type 可以替換成:using {btree|hash}
需要注意的幾點:

  • create命令不能用於創建primary key
  • 能夠爲char,varchar,binary,varbinary設置索引前綴長度,這意味着可以只索引這些字段的前面某部分。
  • blob和text若爲索引項類型,必須指定索引前綴長度[5]。

例如:

create index `index_data1` on test (`data1` (10));

刪除索引:

alter table table_name drop primary key;
alter table table_name drop {index | key} index_name;

當創建多列索引之後,查詢全部索引或索引的前n列(與定義索引順序一致),能夠使用該索引[6]。例如:

create table test(
id int not null auto_increment,
last_name char(30) not null,
first_name char(30) not null,
primary key (id),
index name (last_name, first_name)
);

以下這些查詢能用到索引 name:

select * from test where last='xyb';
select * from test where last='xyb' and first_name='love';
select * from test where last='xyb' and (first_name='love' or first_name='Charlotte');
select * from test where last='xyb' and first_name >= 'l' and first_name <= 'n';

以下這些chauncey不能用到索引 name:

select * from test where first_name='Charlotte';
select * from test where last='xyb' or first_name='Charlotte';

綜合講解索引可以參見鏈接[7]

參考鏈接:
[1]http://www.tutorialspoint.com/mysql/mysql-indexes.htm
[2]http://stackoverflow.com/questions/3844899/difference-between-key-primary-key-unique-key-and-index-in-mysql
[3]https://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html
[4]https://dev.mysql.com/doc/refman/5.0/en/alter-table.html
[5]https://dev.mysql.com/doc/refman/5.0/en/create-table.html
[6]https://dev.mysql.com/doc/refman/5.0/en/multiple-column-indexes.html
[7]http://blog.csdn.net/tianmohust/article/details/7930482

發佈了38 篇原創文章 · 獲贊 27 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章