索引
创建普通索引 普通索引是最基础的索引类型,没有唯一性的限制。作用是只加快对数据的访问速度。
mysql>CREATE TABLE book
(bookid INT NOT NULL,
bookname VARCHAR(255) NOT NULL,
authors VARCHAR(255) NOT NULL,
info VARCHAR(255) NULL,
comment VARCHAR(255) NULL,
year_publication YEAR NOT NULL,
INDEX(year_publication)
);
Query OK, 0 rows affected (0.03 sec)
查看创建的索引
mysql> show create table book\G
*************************** 1. row ***************************
Table: book
Create Table: CREATE TABLE `book` (
`bookid` int(11) NOT NULL,
`bookname` varchar(255) NOT NULL,
`authors` varchar(255) NOT NULL,
`info` varchar(255) DEFAULT NULL,
`comment` varchar(255) DEFAULT NULL,
`year_publication` year(4) NOT NULL,
KEY `year_publication` (`year_publication`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.05 sec)
用explain 判断索引是否正在被使用
mysql> explain select * from book where year_publication=1999\G\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: book
partitions: NULL
type: ref
possible_keys: year_publication
key: year_publication
key_len: 1
ref: const
rows: 1
filtered: 100.00
Extra: Using index condition
1 row in set, 1 warning (0.00 sec)
TYPE的取值范围 System const ref eq_ref index all range
【唯一索引】 创建唯一索引 唯一索引主要原因是减少查询索引列操作的执行时间。尤其是对比比较庞大的数据
表。与普通索引类似,不同点在于:索引列的值必须唯一,但允许有空值。如果是组合索引,则列值的组合必
须唯一。
mysql> CREATE TABLE T1
-> (
-> ID INT NOT NULL,
-> NAME CHAR(30) NOT NULL,
-> UNIQUE INDEX UNIQIDX(ID)
-> );
Query OK, 0 rows affected (0.02 sec)
查看
mysql> SHOW CREATE table T1\G
*************************** 1. row ***************************
Table: T1
Create Table: CREATE TABLE `T1` (
`ID` int(11) NOT NULL,
`NAME` char(30) NOT NULL,
UNIQUE KEY `UNIQIDX` (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
【单列索引】 单列索引:是在数据表中的某一字段上创建的索引,一个表中可以创建多个单列索引。
mysql> create table t2
-> (
-> id int not null,
-> name CHAR(50) NULL,
-> index singleidx(name)
-> );
Query OK, 0 rows affected (0.03 sec)
查看
mysql> SHOW CREATE table t2\G
*************************** 1. row ***************************
Table: t2
Create Table: CREATE TABLE `t2` (
`id` int(11) NOT NULL,
`name` char(50) DEFAULT NULL,
KEY `singleidx` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
【组合索引】 组合索引:是在多个字段上创建一个索引。遵循最左前缀原则。最左前缀 索引最左边的列来匹配
行 创建
mysql> create table t3
(
id int not null,
name CHAR(30) NOT NULL,
age int not null,
info VARCHAR(255),
index syt3(id,name,age)
);
Query OK, 0 rows affected (0.02 sec)
查看
mysql> SHOW CREATE table t3\G
*************************** 1. row ***************************
Table: t3
Create Table: CREATE TABLE `t3` (
`id` int(11) NOT NULL,
`name` char(30) NOT NULL,
`age` int(11) NOT NULL,
`info` varchar(255) DEFAULT NULL,
KEY `syt3` (`id`,`name`,`age`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
组合索引可以起几个索引的作用,但是使用时并不是随意查询哪个字段都是可以使用索引。而是遵循最左前
缀:利用索引中最左边的列集来匹配行。这样的列集称为最左前缀。
【全文索引】 全文索引:FULLTEXT,可以用于全文搜索,只有MyISAM存储引擎支持,并且只为
CHAR\VARCHAR和TEXT 列。索引总是对整个列进行,不支持局部索引,适合大型数据的表创建
mysql> CREATE TABLE t4
( id INT NOT NULL,
name CHAR(30) NOT NULL,
age INT NOT NULL,
info VARCHAR(255),
FULLTEXT INDEX FullIdx(info(100))
)ENGINE=MyISAM;
Query OK, 0 rows affected (0.02 sec)
查看
mysql> SHOW CREATE table t4\G
*************************** 1. row ***************************
Table: t4
Create Table: CREATE TABLE `t4` (
`id` int(11) NOT NULL,
`name` char(30) NOT NULL,
`age` int(11) NOT NULL,
`info` varchar(255) DEFAULT NULL,
FULLTEXT KEY `FullIdx` (`info`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
【空间索引】 空间索引:必须在MyISAM类型的表中创建,且空间类型的字段必须为非空。 创建
mysql> CREATE TABLE t5
-> (
-> g GEOMETRY NOT NULL,
-> SPATIAL INDEX spaIdx(g)
-> )ENGINE=MyISAM;
Query OK, 0 rows affected (0.00 sec)
查看
mysql> SHOW CREATE table t5\G
*************************** 1. row ***************************
Table: t5
Create Table: CREATE TABLE `t5` (
`g` geometry NOT NULL,
SPATIAL KEY `spaidx` (`g`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.01 sec)
在已经存在的表上创建索引
Alter table 表名 add [unique唯一索引|fulltext全文索引|spatial空间索引] [index|key] [默认索引名] (定义
索引名[length]) [asc|desc]
mysql> ALTER TABLE book ADD INDEX BKNameIdx(bookname(30));
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看
mysql> SHOW INDEX FROM book\G
*************************** 1. row ***************************
Table: book
Non_unique: 1
Key_name: year_publication
Seq_in_index: 1
Column_name: year_publication
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: book
Non_unique: 1
Key_name: BKNameIdx
Seq_in_index: 1
Column_name: bookname
Collation: A
Cardinality: 0
Sub_part: 30
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
2 rows in set (0.02 sec)
添加唯一性索引
mysql> ALTER TABLE book ADD UNIQUE INDEX UniqIdx(bookid);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看
mysql> SHOW INDEX from book\G
*************************** 1. row ***************************
Table: book
Non_unique: 0
Key_name: UniqIdx
Seq_in_index: 1
Column_name: bookid
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: book
Non_unique: 1
Key_name: year_publication
Seq_in_index: 1
Column_name: year_publication
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 3. row ***************************
Table: book
Non_unique: 1
Key_name: BKNameIdx
Seq_in_index: 1
Column_name: bookname
Collation: A
Cardinality: 0
Sub_part: 30
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
3 rows in set (0.00 sec)
添加单列索引
mysql> ALTER TABLE book ADD INDEX BKIdex(comment(50));
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看
mysql> SHOW INDEX FROM book\G
*************************** 1. row ***************************
Table: book
Non_unique: 0
Key_name: UniqIdx
Seq_in_index: 1
Column_name: bookid
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: book
Non_unique: 1
Key_name: year_publication
Seq_in_index: 1
Column_name: year_publication
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 3. row ***************************
Table: book
Non_unique: 1
Key_name: BKNameIdx
Seq_in_index: 1
Column_name: bookname
Collation: A
Cardinality: 0
Sub_part: 30
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 4. row ***************************
Table: book
Non_unique: 1
Key_name: BKIdex
Seq_in_index: 1
Column_name: comment
Collation: A
Cardinality: 0
Sub_part: 50
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
4 rows in set (0.00 sec)
添加全文索引
mysql> CREATE TABLE t6
-> (
-> id INT NOT NULL,
-> info CHAR(255)
-> )ENGINE=MyISAM;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER TABLE t6 ADD FULLTEXT INDEX InfoFULIdx(info);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看
mysql> SHOW INDEX FROM t6\G
*************************** 1. row ***************************
Table: t6
Non_unique: 1
Key_name: InfoFULIdx
Seq_in_index: 1
Column_name: info
Collation: NULL
Cardinality: NULL
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: FULLTEXT
Comment:
Index_comment:
1 row in set (0.00 sec)
添加组合索引
mysql> ALTER TABLE book ADD INDEX BKAUthAndInfoIdx(authors(20),info(50));
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看
mysql> SHOW INDEX FROM book\G
*************************** 1. row ***************************
Table: book
Non_unique: 0
Key_name: UniqIdx
Seq_in_index: 1
Column_name: bookid
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 2. row ***************************
Table: book
Non_unique: 1
Key_name: year_publication
Seq_in_index: 1
Column_name: year_publication
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 3. row ***************************
Table: book
Non_unique: 1
Key_name: BKNameIdx
Seq_in_index: 1
Column_name: bookname
Collation: A
Cardinality: 0
Sub_part: 30
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 4. row ***************************
Table: book
Non_unique: 1
Key_name: BKIdex
Seq_in_index: 1
Column_name: comment
Collation: A
Cardinality: 0
Sub_part: 50
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
*************************** 5. row ***************************
Table: book
Non_unique: 1
Key_name: BKAUthAndInfoIdx
Seq_in_index: 1
Column_name: authors
Collation: A
Cardinality: 0
Sub_part: 20
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
*************************** 6. row ***************************
Table: book
Non_unique: 1
Key_name: BKAUthAndInfoIdx
Seq_in_index: 2
Column_name: info
Collation: A
Cardinality: 0
Sub_part: 50
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
6 rows in set (0.00 sec)
添加空间索引
mysql> CREATE TABLE t7
-> (
-> g GEOMETRY NOT NULL
-> )ENGINE=MyISAM;
Query OK, 0 rows affected (0.00 sec)
mysql> ALTER TABLE t7 ADD SPATIAL INDEX SpatIdx(g);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
查看
mysql> SHOW INDEX from t7\G
*************************** 1. row ***************************
Table: t7
Non_unique: 1
Key_name: SpatIdx
Seq_in_index: 1
Column_name: g
Collation: A
Cardinality: NULL
Sub_part: 32
Packed: NULL
Null:
Index_type: SPATIAL
Comment:
Index_comment:
1 row in set (0.00 sec)
【创建索引】
Create index 已存在的表上创建索引 Create [unique唯一性索引|fulltext全文索引|spatial空间索引] index
索引名
On 表名(col_name[lenth]…)[asc|desc]
用create table创建的索引可以不添加索引名(不添加索引名默认为字段名) 但是用create index 添加索引名必
须加索引名
创建一个book1的表
CREATE TABLE book1 ( bookid INT NOT NULL, bookname VARCHAR(255) NOT NULL,
authors VARCHAR(255) NOT NULL, info VARCHAR(255) NULL, comment VARCHAR(255) NULL,
year_publication YEAR NOT NULL );
Query OK, 0 rows affected (0.02 sec)
普通索引
mysql> CREATE INDEX BKNameIdex ON book1(bookname);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
单列索引
mysql> CREATE INDEX BKcmtIdex ON book1 (comment(50));
Query OK, 0 rows affected (0.08 sec)
Records: 0 Duplicates: 0 Warnings: 0
组合索引
mysql> CREATE INDEX BKAuthAndInfoIdex ON book1(authors(30),info(50));
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
全文索引
mysql> drop table t6 ;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE t6 ( id INT NOT NULL, info CHAR(255) )ENGINE=MyISAM;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE FULLTEXT INDEX FullIdex ON t6(info);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
唯一性索引
mysql> CREATE UNIQUE INDEX UniqIdx ON book1(bookId);
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0
空间索引
mysql> drop table t7;
Query OK, 0 rows affected (0.00 sec)
mysql> CREATE TABLE t7 ( g GEOMETRY NOT NULL )ENGINE=MyISAM;
Query OK, 0 rows affected (0.01 sec)
mysql> CREATE SPATIAL INDEX SpaIdx ON t7(g);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
【删除索引】
Alter table 表名 drop index 索引名;
查看book中有多少索引,准备开始删除
mysql> SHOW CREATE table book1\G
*************************** 1. row ***************************
Table: book1
Create Table: CREATE TABLE `book1` (
`bookid` int(11) NOT NULL,
`bookname` varchar(255) NOT NULL,
`authors` varchar(255) NOT NULL,
`info` varchar(255) DEFAULT NULL,
`comment` varchar(255) DEFAULT NULL,
`year_publication` year(4) NOT NULL,
UNIQUE KEY `UniqIdx` (`bookid`),
KEY `BKNameIdex` (`bookname`),
KEY `BKcmtIdex` (`comment`(50)),
KEY `BKAuthAndInfoIdex` (`authors`(30),`info`(50))
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
用ALTER TABLE 删除
mysql> ALTER TABLE book DROP INDEX UniqIdx;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> ALTER TABLE book1 DROP INDEX BKNameIdex;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
添加AUTO_INCREMENT 的约束字段的唯一索引不能删除
用DROP INDEX删除
mysql> SHOW CREATE TABLE t7\G
*************************** 1. row ***************************
Table: t7
Create Table: CREATE TABLE `t7` (
`g` geometry NOT NULL,
SPATIAL KEY `SpaIdx` (`g`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> DROP INDEX SpaIdx ON t7;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
删除表中的列时,如果要删除的列为索引的组成部分,则该列也会从索引中删除。如果组成索引的所有列都被
删除,那么整个索引将被删除。