mysql索引 index的类型和使用

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

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

 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的语法增长索引。spa

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

下面分别是建立索引 的三种状况。code

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 unique (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建立索引table

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
相关文章
相关标签/搜索