您的位置:首页 > 数据库 > MySQL

mysql索引 index的类型和使用

2016-11-19 00:00 561 查看
摘要: 本节主要讲述index的类型和创建,至于索引的详细使用会在后续介绍。

提高SELECT操作性能最好的方法就是在查询的一列或者多列创建索引。索引就像表行的指针,允许快速确定那些行和WHERE语句条件匹配,并检索这一行的其他列值,在mysql中所有的数据类型都可以被索引。我们可以根据存储引擎定义每个表的最大索引数和最大索引长度,每一种索引引擎的每张表至少支持16个索引,索引总长度最少为256字节。对于MyISAM表总长度可以达到1000字节,而对于InnoDB表可以达到767字节。

mysql常见的索引包括 PRIMARY KEY,UNIQUE,INDEX和FULLTEXT四种索引。

PRIMARY KEY 索引:mysql在创建主键的时候自动会为主键创建索引,而不需要我们显式的声明索引。比如:

mysql> create table tPri(
-> id INTEGER PRIMARY KEY,
-> name VARCHAR(20)
-> );
Query OK, 0 rows affected (0.06 sec)

mysql> explain select * from tPri where id>=1;
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | tPri  | range | PRIMARY       | PRIMARY | 4       | NULL |    1 | Using where |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.01 sec)

上面的explain命令可以解析sql语句优化sql语句,后续会详细介绍。使用此命令可以选择出一行数据,这里我们只关注possible_keys 和key,这两个字段描述的是索引的类型,可以看出创建PRIMARY KEY的时候直接创建一条索引。

创建其他索引可以直接在创建表的时候创建索引,也可以使用create [unique|fulltext|spatial] index index_name[using index_type] on table_name(index_col_name,...);其中index_col_name可以指定长度col_name[(length)] [ASC|DESC];同时我们也可以使用alter table的语法增加索引。

UNIQUE:可选。表示索引为唯一性索引。
FULLTEXT;可选。表示索引为全文索引。
SPATIAL:可选。表示索引为空间索引。
INDEX和KEY:用于指定字段为索引,两者选择其中之一就可以了,作用是一样的。
索引名:可选。给创建的索引取一个新名称。
字段名1:指定索引对应的字段的名称,该字段必须是前面定义好的字段。
长度:可选。指索引的长度,必须是字符串类型才可以使用。
ASC:可选。表示升序排列。
DESC:可选。表示降序排列。

下面分别是创建索引 的三种情况。

1.在 建表的时候直接创建索引。

mysql> create table user(
-> id INTEGER,
-> name VARCHAR(20),
-> address VARCHAR(100),
-> phone varchar(11),
-> index idx_1(id),
-> unique index idx_2(name),
-> fulltext index idx_3(address)
-> );
Query OK, 0 rows affected (0.48 sec)

2.使用alter创建索引

mysql> create table user(
-> id INTEGER,
-> name VARCHAR(20),
-> address VARCHAR(100),
-> phone varchar(11)
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> alter table user add primary key(id);
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table user add un
3ff0
ique (name);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table user add index idx_1 ( phone);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table user add fulltext ( address);
Query OK, 0 rows affected, 1 warning (0.24 sec)
Records: 0  Duplicates: 0  Warnings: 1

3 使用create创建索引

mysql> create table user(
-> id INTEGER,
-> name VARCHAR(20),
-> address VARCHAR(100),
-> phone varchar(11)
-> );
Query OK, 0 rows affected (0.01 sec)

mysql> create index idx_1 on user(id);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> create unique index idx_2 on user(name);
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> create fulltext index idx_3 on user(address);
Query OK, 0 rows affected, 1 warning (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 1

删除索引就比较容易了,直接使用drop即可。

mysql> drop index idx_1 on user;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: