跳到主要内容

普通索引

2024年06月12日
柏拉文
越努力,越幸运

一、认识


普通索引是最常见的索引类型,用于加速对表中数据的查询。

二、创建索引


2.1 create index

默认

create index [indexName] on [tableName] ([columnName1][asc|desc], [columnName2][asc|desc], ……);

使用 B-Tree 索引类型, 其实默认使用的就是 B-Tree ,可以省略。

create index [indexName] on [tableName] ([columnName1][asc|desc], [columnName2][asc|desc], ……) using btree;

2.2 alter table [tableName] add index [indexName] ([columnName1][asc|desc], [columnName2][asc|desc], ……);

默认

alter table [tableName] add index [indexName] ([columnName1][asc|desc], [columnName2][asc|desc], ……);

使用 B-Tree 索引类型, 其实默认使用的就是 B-Tree ,可以省略。

alter table [tableName] add index [indexName] ([columnName1][asc|desc], [columnName2][asc|desc], ……) using btree;

2.3 create table [tableName] (……, index [indexName] ([columnName1][asc|desc], [columnName2][asc|desc], ……) )

默认

create table [tableName] (
column1 dateType,
column2 dateType,
index [indexName] ([columnName1][asc|desc], [columnName2][asc|desc], ……)
)

使用 B-Tree 索引类型, 其实默认使用的就是 B-Tree ,可以省略。

create table [tableName] (
column1 dateType,
column2 dateType,
index [indexName] ([columnName1][asc|desc], [columnName2][asc|desc], ……) using btree
)

三、删除索引


3.1 drop index [indexName] on [tableName]

drop index [indexName] on [tableName];

3.2 alter table [tableName] drop index [indexName]

alter table [tableName] drop index [indexName];

四、查看索引


4.1 show index from [tableName]\G

show index from [tableName]\G;