一、索引
--索引像书的目录,可以加快查询速度,mysql优化的一种。
--查询数据库,按照主键查询是最快的,每个表只有一个主键列,可以有抖个普通的,主键要求列要求列的所有内容必须唯一,索引不要求。
--索引创建在表的列上
--索引会加快查询速度,但会影响更新速度,因为要维护索引
--索引不是越多越好,要在频繁查询的where后条件列上创建索引
--小表或唯一值极少的列上不建索引,要在大表以及不同内容多的列上创建
--联合索引是有前缀生效特性,例:index(a,b,c)仅a,ab,abc三个查询条件可以用索引。b,bc,ac,c等就无法使用索引。
二、创建索引
1. 主建索引
key 索引名(字段)
例:创建表student设置ID列为主键,name列为索引
>CREATE TABLE student1 ( id int(4) NOT NULL AUTO_INCREMENT, name char(20) NOT NULL, age tinyint(2) NOT NULL DEFAULT '0', dept varchar(16) DEFAULT NULL, primary key (id), key index_name (name) ); mysql> desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | MUL | NULL | | | age | tinyint(2) | NO | | 0 | | | dept | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) alter table 表名 change id id int primary key auto_increment;
2.唯一索引
>CREATE TABLE student1 ( id int(4) NOT NULL AUTO_INCREMENT, name char(20) NOT NULL, age tinyint(2) NOT NULL DEFAULT '0', dept varchar(16) DEFAULT NULL, primary key (id), UNIQUE index_id (id) );
3.普通索引
>CREATE TABLE student1 ( id int(4) NOT NULL AUTO_INCREMENT, name char(20) NOT NULL, age tinyint(2) NOT NULL DEFAULT '0', dept varchar(16) DEFAULT NULL, primary key (id), index (name) );
3.单列索引
>CREATE TABLE student1 ( id int(4) NOT NULL AUTO_INCREMENT, name char(20) NOT NULL, age tinyint(2) NOT NULL DEFAULT '0', dept varchar(16) DEFAULT NULL, primary key (id), index index_name (name (10)) );
3.组合索引
>CREATE TABLE student1 ( id int(4) NOT NULL AUTO_INCREMENT, name char(20) NOT NULL, age tinyint(2) NOT NULL DEFAULT '0', dept varchar(16) DEFAULT NULL, primary key (id), index index_mut (id,name,age(100)) );
删除索引
alter table 表名 drop index 索引名;
> alter table student drop index index_name; Query OK, 0 rows affected (0.24 sec) Records: 0 Duplicates: 0 Warnings: 0 > desc student1 ; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | | NULL | | | age | tinyint(2) | NO | | 0 | | | dept | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
drop index 索引名 on 表名;
drop index index_name on student;
3.添加索引
alter table 表名 index add index 索引名;
> alter table student add index index_name(name); Query OK, 0 rows affected (0.08 sec) Records: 0 Duplicates: 0 Warnings: 0 > desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | MUL | NULL | | | age | tinyint(2) | NO | | 0 | | | dept | varchar(16) | YES | | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
4.创建指定前n个字符的索引
create index 索引名 on 表名(列名())
>create index index_dept on student (dept(8)); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 > desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | MUL | NULL | | | age | tinyint(2) | NO | | 0 | | | dept | varchar(16) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
5.创建联合索引
create index 索引名 on 表名 (字段1,字段2)
例:>create index ind_name_dept on studen(name,dept); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 >show index from student\G *************************** 5. row *************************** Table: student1 Non_unique: 1 Key_name: ind_name_dept Seq_in_index: 2 Column_name: dept Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: 5 rows in set (0.00 sec)
6.为多列的前n个字符列创建联合索引
create index 索引名 on 库名(字段1(),字段列2());
例:创建student表索引ind_name_dept ,索引name列的前10个字符和dept列的前8个字符。
>create index ind_name_dept on student1(name(10),dept(8));
7.创建唯一索引(非主键)
create unique index 索引名 on 表名(列名);
>create unique index uni_name on student(name); Query OK, 0 rows affected (0.46 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc student1; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | UNI | NULL | | | age | tinyint(2) | NO | | 0 | | | dept | varchar(16) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.01 sec)
二、查看索引
1.desc 表名;
> desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(4) | NO | PRI | NULL | auto_increment | | name | char(20) | NO | MUL | NULL | | | age | tinyint(2) | NO | | 0 | | | dept | varchar(16) | YES | MUL | NULL | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec)
2.show index from 表名\G
例:show index from student\G *************************** 1. row *************************** Table: student1 Non_unique: 0 Key_name: PRIMARY Seq_in_index: 1 Column_name: id Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: *************************** 2. row *************************** Table: student1 Non_unique: 1 Key_name: index_name Seq_in_index: 1 Column_name: name Collation: A Cardinality: 0 Sub_part: NULL Packed: NULL Null: Index_type: BTREE Comment: Index_comment: *************************** 3. row *************************** Table: student1 Non_unique: 1 Key_name: index_dept Seq_in_index: 1 Column_name: dept Collation: A Cardinality: 0 Sub_part: 8 Packed: NULL Null: YES Index_type: BTREE Comment: Index_comment: 3 rows in set (0.00 sec