当前位置 博文首页 > 韩超的博客 (hanchao5272):一篇文章掌握MySql索引的语法、分类
排序
的方法。字段数值
以及指向相关记录的指针
。使用索引的目的就是为了提高查询效率。
假定表中存在5,000,000
条记录,共需要1,000,000
个磁盘块,当前查找字段为身份证号码
,为无序的。
1,000,000
个磁盘块。log2 (1000000) = 19.93 = 20
个数据块。实际上,索引使用的B+Tree
,其查询算法比二分查找
更加高效。
最左前缀原则
,依照字段的检索频率的高低排列字段。ci_index(col1,col2,col3)
,则可用索引列组合为:col1,col2,col3
,col1,col2
,col1
。ci_index(col1(5),col2,col3(5))
。text
类型。下面的创建语句包含了的五种索引:
PRIMARY KEY (id)
UNIQUE INDEX ui_isbn (ISBN)
INDEX i_author(author), INDEX i_name(name), INDEX i_publisher(publisher), INDEX i_page(page)
INDEX ci_author_name(name,author)
FULLTEXT INDEX fti_summary(summary)
CREATE TABLE `book` (
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '图书ID',
`name` varchar(50) NOT NULL COMMENT '书名',
`author` varchar(20) NOT NULL COMMENT '作者',
`publisher` varchar(50) NOT NULL COMMENT '出版社',
`ISBN` varchar(16) NOT NULL COMMENT 'ISBN编码',
`summary` text NULL COMMENT '简介',
`page` int NOT NULL DEFAULT '1' COMMENT '页数',
PRIMARY KEY (`id`),
UNIQUE INDEX ui_isbn (`ISBN`),
INDEX i_author(`author`),
INDEX i_name(`name`),
INDEX i_publisher(`publisher`),
INDEX i_page(`page`),
INDEX ci_author_name(`name`,`author`),
FULLTEXT INDEX fti_summary(`summary`)
) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARSET = utf8mb4 COMMENT '图书表';
CREATE TABLE `book` (
`id` bigint NOT NULL COMMENT '图书ID',
`name` varchar(50) NOT NULL COMMENT '书名',
`author` varchar(20) NOT NULL COMMENT '作者',
`publisher` varchar(50) NOT NULL COMMENT '出版社',
`ISBN` varchar(16) NOT NULL COMMENT 'ISBN编码',
`summary` text NULL COMMENT '简介',
`page` int NOT NULL DEFAULT '1' COMMENT '页数'
) ENGINE = InnoDB CHARSET = utf8mb4 COMMENT '图书表';
-- 主键索引
alter table `book` add PRIMARY KEY (`id`);
-- 唯一索引
alter table `book` add UNIQUE INDEX ui_isbn (`ISBN`);
-- 普通索引
alter table `book` add (
INDEX i_author(`author`),
INDEX i_name(`name`),
INDEX i_publisher(`publisher`),
INDEX i_page(`page`)
);
-- 组合索引
alter table `book` add INDEX ci_author_name(`name`,`author`);
-- 全文索引
alter table `book` add FULLTEXT INDEX fti_summary(`summary`);
mysql> show index from `book`;
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| book | 0 | PRIMARY | 1 | id | A | 0 | NULL | NULL | | BTREE | | |
| book | 0 | ui_isbn | 1 | ISBN | A | 0 | NULL | NULL | | BTREE | | |
| book | 1 | i_author | 1 | author | A | 0 | NULL | NULL | | BTREE | | |
| book | 1 | i_name | 1 | name | A | 0 | NULL | NULL | | BTREE | | |
| book | 1 | i_publisher | 1 | publisher | A | 0 | NULL | NULL | | BTREE | | |
| book | 1 | i_page | 1 | page | A | 0 | NULL | NULL | | BTREE | | |
| book | 1 | ci_author_name | 1 | name | A | 0 | NULL | NULL | | BTREE | | |
| book | 1 | ci_author_name | 2 | author | A | 0 | NULL | NULL | | BTREE | | |
| book | 1 | fti_summary | 1 | summary | NULL | 0 | NULL | NULL | YES | FULLTEXT | | |
+-------+------------+----------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
9 rows in set (0.00 sec)
explain {Sql Statement}
此处只例举几种类型,关于索引的使用注意事项,后面的章节会细说。
-- 普通查询 key = ui_isbn
mysql> explain select name,author from book where ISBN = 'ISNB00001';
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | book | const | ui_isbn | ui_isbn | 66 | const | 1 | NULL |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.01 sec)
-- 模糊查询 key = i_name
mysql> explain select name,author from book where name like '张%' limit 1;
+----+-------------+-------+-------+-----------------------+--------+---------+------+------+-----------------------+
| id | select_type | table | type