MySQL-索引(二)

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: MySQL-索引

添加唯一索引

添加索引

1. mysql> alter table book add unique index Uniqldx(bookid);
2. Query OK, 0 rows affected (0.05 sec)
3. Records: 0  Duplicates: 0  Warnings: 0

查看索引

1. mysql> show index from book\G
2. *************************** 1. row ***************************
3.         Table: book
4.    Non_unique: 0
5.      Key_name: Uniqldx
6.  Seq_in_index: 1
7.   Column_name: bookid
8.     Collation: A
9.   Cardinality: 0
10.      Sub_part: NULL
11.        Packed: NULL
12.          Null: 
13.    Index_type: BTREE
14.       Comment: 
15. Index_comment: 
16. *************************** 2. row ***************************
17.         Table: book
18.    Non_unique: 1
19.      Key_name: year_publication
20.  Seq_in_index: 1
21.   Column_name: year_publication
22.     Collation: A
23.   Cardinality: 0
24.      Sub_part: NULL
25.        Packed: NULL
26.          Null: 
27.    Index_type: BTREE
28.       Comment: 
29. Index_comment: 
30. *************************** 3. row ***************************
31.         Table: book
32.    Non_unique: 1
33.      Key_name: BKNameIdx
34.  Seq_in_index: 1
35.   Column_name: bookname
36.     Collation: A
37.   Cardinality: 0
38.      Sub_part: 30
39.        Packed: NULL
40.          Null: 
41.    Index_type: BTREE
42.       Comment: 
43. Index_comment: 
44. 3 rows in set (0.00 sec)

添加单列索引

添加索引

1. mysql> alter table book add index BKidex(comment(50));
2. Query OK, 0 rows affected (0.01 sec)
3. Records: 0  Duplicates: 0  Warnings: 0

查看索引

1. mysql> show index from book\G
2. *************************** 1. row ***************************
3.         Table: book
4.    Non_unique: 0
5.      Key_name: Uniqldx
6.  Seq_in_index: 1
7.   Column_name: bookid
8.     Collation: A
9.   Cardinality: 0
10.      Sub_part: NULL
11.        Packed: NULL
12.          Null: 
13.    Index_type: BTREE
14.       Comment: 
15. Index_comment: 
16. *************************** 2. row ***************************
17.         Table: book
18.    Non_unique: 1
19.      Key_name: year_publication
20.  Seq_in_index: 1
21.   Column_name: year_publication
22.     Collation: A
23.   Cardinality: 0
24.      Sub_part: NULL
25.        Packed: NULL
26.          Null: 
27.    Index_type: BTREE
28.       Comment: 
29. Index_comment: 
30. *************************** 3. row ***************************
31.         Table: book
32.    Non_unique: 1
33.      Key_name: BKNameIdx
34.  Seq_in_index: 1
35.   Column_name: bookname
36.     Collation: A
37.   Cardinality: 0
38.      Sub_part: 30
39.        Packed: NULL
40.          Null: 
41.    Index_type: BTREE
42.       Comment: 
43. Index_comment: 
44. *************************** 4. row ***************************
45.         Table: book
46.    Non_unique: 1
47.      Key_name: BKidex
48.  Seq_in_index: 1
49.   Column_name: comment
50.     Collation: A
51.   Cardinality: 0
52.      Sub_part: 50
53.        Packed: NULL
54.          Null: YES
55.    Index_type: BTREE
56.       Comment: 
57. Index_comment: 
58. 4 rows in set (0.00 sec)

添加全文索引

添加索引

1. mysql> CREATE TABLE t6
2.     ->  (
3.     ->  id INT NOT NULL,
4.     ->  info CHAR(255)
5.     ->  )ENGINE=MyISAM;
6. Query OK, 0 rows affected (0.00 sec)
7. 
8. mysql> ALTER TABLE t6 ADD FULLTEXT INDEX InfoFULIdx(info);
9. Query OK, 0 rows affected (0.00 sec)
10. Records: 0  Duplicates: 0  Warnings: 0

查看全文索引

1. mysql> show index from t6\G
2. *************************** 1. row ***************************
3.         Table: t6
4.    Non_unique: 1
5.      Key_name: InfoFULIdx
6.  Seq_in_index: 1
7.   Column_name: info
8.     Collation: NULL
9.   Cardinality: NULL
10.      Sub_part: NULL
11.        Packed: NULL
12.          Null: YES
13.    Index_type: FULLTEXT
14.       Comment: 
15. Index_comment: 
16. 1 row in set (0.00 sec)

添加组合索引

添加索引

1. mysql>  ALTER TABLE book ADD INDEX BKAUthAndInfoIdx(authors(20),info(50));
2. Query OK, 0 rows affected (0.01 sec)
3. Records: 0  Duplicates: 0  Warnings: 0

查看索引

1. mysql> show index from book\G
2. *************************** 1. row ***************************
3.         Table: book
4.    Non_unique: 0
5.      Key_name: Uniqldx
6.  Seq_in_index: 1
7.   Column_name: bookid
8.     Collation: A
9.   Cardinality: 0
10.      Sub_part: NULL
11.        Packed: NULL
12.          Null: 
13.    Index_type: BTREE
14.       Comment: 
15. Index_comment: 
16. *************************** 2. row ***************************
17.         Table: book
18.    Non_unique: 1
19.      Key_name: year_publication
20.  Seq_in_index: 1
21.   Column_name: year_publication
22.     Collation: A
23.   Cardinality: 0
24.      Sub_part: NULL
25.        Packed: NULL
26.          Null: 
27.    Index_type: BTREE
28.       Comment: 
29. Index_comment: 
30. *************************** 3. row ***************************
31.         Table: book
32.    Non_unique: 1
33.      Key_name: BKNameIdx
34.  Seq_in_index: 1
35.   Column_name: bookname
36.     Collation: A
37.   Cardinality: 0
38.      Sub_part: 30
39.        Packed: NULL
40.          Null: 
41.    Index_type: BTREE
42.       Comment: 
43. Index_comment: 
44. *************************** 4. row ***************************
45.         Table: book
46.    Non_unique: 1
47.      Key_name: BKidex
48.  Seq_in_index: 1
49.   Column_name: comment
50.     Collation: A
51.   Cardinality: 0
52.      Sub_part: 50
53.        Packed: NULL
54.          Null: YES
55.    Index_type: BTREE
56.       Comment: 
57. Index_comment: 
58. *************************** 5. row ***************************
59.         Table: book
60.    Non_unique: 1
61.      Key_name: BKAUthAndInfoIdx
62.  Seq_in_index: 1
63.   Column_name: authors
64.     Collation: A
65.   Cardinality: 0
66.      Sub_part: 20
67.        Packed: NULL
68.          Null: 
69.    Index_type: BTREE
70.       Comment: 
71. Index_comment: 
72. *************************** 6. row ***************************
73.         Table: book
74.    Non_unique: 1
75.      Key_name: BKAUthAndInfoIdx
76.  Seq_in_index: 2
77.   Column_name: info
78.     Collation: A
79.   Cardinality: 0
80.      Sub_part: 50
81.        Packed: NULL
82.          Null: YES
83.    Index_type: BTREE
84.       Comment: 
85. Index_comment: 
86. 6 rows in set (0.00 sec)

添加空间索引

添加索引

1. mysql> CREATE TABLE t7
2.     ->  (
3.     ->  g GEOMETRY NOT NULL
4.     ->  )ENGINE=MyISAM;
5. Query OK, 0 rows affected (0.01 sec)
6. 
7. mysql> ALTER TABLE t7 ADD SPATIAL INDEX SpatIdx(g);
8. Query OK, 0 rows affected (0.01 sec)
9. Records: 0  Duplicates: 0  Warnings: 0

查看索引

1. mysql> show index from t7\G
2. *************************** 1. row ***************************
3.         Table: t7
4.    Non_unique: 1
5.      Key_name: SpatIdx
6.  Seq_in_index: 1
7.   Column_name: g
8.     Collation: A
9.   Cardinality: NULL
10.      Sub_part: 32
11.        Packed: NULL
12.          Null: 
13.    Index_type: SPATIAL
14.       Comment: 
15. Index_comment: 
16. 1 row in set (0.00 sec)

创建索引


创建一个book1表

1. mysql> create table book1 (bookid int not null, bookname varchar(255) not null,
2. -> authors varchar(255) not null, info varchar(255) null, comment varchar(255) null,
3. -> year_publication year not null );
4. Query OK, 0 rows affected (0.01 sec)

普通索引

1. mysql> create index bknameidex on book1(bookname);
2. Query OK, 0 rows affected (0.34 sec)
3. Records: 0 Duplicates: 0 Warnings: 0

单列索引

1. mysql> create index bkcmtidex on book1 (comment(50));
2. Query OK, 0 rows affected (0.00 sec)
3. Records: 0 Duplicates: 0 Warnings: 0

组合索引

1. mysql> create index bkauthandinfoidex on book1(authors(30),info(50));
2. Query OK, 0 rows affected (0.00 sec)
3. Records: 0 Duplicates: 0 Warnings: 0

全文索引

1. mysql> drop table t6;
2. Query OK, 0 rows affected (0.00 sec)
3. mysql> create table t6 ( id int not null, info char(255))engine=myisam;
4. Query OK, 0 rows affected (0.00 sec)
5. mysql> CREATE FULLTEXT INDEX FullIdex ON t6(info);
6. Query OK, 0 rows affected (0.00 sec)
7. Records: 0 Duplicates: 0 Warnings: 0

唯一索引

1. mysql> create unique index uniqidx on book1(bookid);
2. Query OK, 0 rows affected (0.00 sec)
3. Records: 0 Duplicates: 0 Warnings: 0

空间索引

1. mysql> drop table t7;
2. Query OK, 0 rows affected (0.00 sec)
3. mysql> create table t7 ( g geometry not null )engine=myisam;
4. Query OK, 0 rows affected (0.00 sec)
5. mysql> create spatial index spaidx on t7(g);
6. Query OK, 0 rows affected (0.00 sec)
7. 
8. Records: 0 Duplicates: 0 Warnings: 0

删除索引


先查看book表的索引

1. mysql> show create table book\G
2. *************************** 1. row ***************************
3.        Table: book
4. Create Table: CREATE TABLE `book` (
5.   `bookid` int(11) NOT NULL,
6.   `bookname` varchar(255) COLLATE utf8_bin NOT NULL,
7.   `authors` varchar(255) COLLATE utf8_bin NOT NULL,
8.   `info` varchar(255) COLLATE utf8_bin DEFAULT NULL,
9.   `comment` varchar(255) COLLATE utf8_bin DEFAULT NULL,
10.   `year_publication` year(4) NOT NULL,
11.   KEY `year_publication` (`year_publication`),
12.   KEY `bknameidx` (`bookname`(30))
13. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
14. 1 row in set (0.00 sec)

使用alter命令删除索引后,再次查看book表已经删除bknameidx索引。

1. mysql> alter table book drop index bknameidx;
2. Query OK, 0 rows affected (0.03 sec)
3. Records: 0  Duplicates: 0  Warnings: 0
4. 
5. mysql> show create table book\G
6. *************************** 1. row ***************************
7.        Table: book
8. Create Table: CREATE TABLE `book` (
9.   `bookid` int(11) NOT NULL,
10.   `bookname` varchar(255) COLLATE utf8_bin NOT NULL,
11.   `authors` varchar(255) COLLATE utf8_bin NOT NULL,
12.   `info` varchar(255) COLLATE utf8_bin DEFAULT NULL,
13.   `comment` varchar(255) COLLATE utf8_bin DEFAULT NULL,
14.   `year_publication` year(4) NOT NULL,
15.   KEY `year_publication` (`year_publication`)
16. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
17. 1 row in set (0.00 sec)

使用drop index删除

1. mysql> show create table t7\G
2. *************************** 1. row ***************************
3.        Table: t7
4. Create Table: CREATE TABLE `t7` (
5.   `g` geometry NOT NULL,
6.   SPATIAL KEY `spatidx` (`g`)
7. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin
8. 1 row in set (0.00 sec)
9. 
10. mysql> drop index spatidx on t7;
11. Query OK, 0 rows affected (0.00 sec)
12. Records: 0  Duplicates: 0  Warnings: 0
13. 
14. mysql> show create table t7\G
15. *************************** 1. row ***************************
16.        Table: t7
17. Create Table: CREATE TABLE `t7` (
18.   `g` geometry NOT NULL
19. ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin
20. 1 row in set (0.00 sec)
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
5天前
|
缓存 算法 关系型数据库
MySQL底层概述—8.JOIN排序索引优化
本文主要介绍了MySQL中几种关键的优化技术和概念,包括Join算法原理、IN和EXISTS函数的使用场景、索引排序与额外排序(Using filesort)的区别及优化方法、以及单表和多表查询的索引优化策略。
MySQL底层概述—8.JOIN排序索引优化
|
6天前
|
存储 关系型数据库 MySQL
MySQL底层概述—6.索引原理
本文详细回顾了:索引原理、二叉查找树、平衡二叉树(AVL树)、红黑树、B-Tree、B+Tree、Hash索引、聚簇索引与非聚簇索引。
MySQL底层概述—6.索引原理
|
8天前
|
SQL 存储 关系型数据库
MySQL原理简介—9.MySQL索引原理
本文详细介绍了MySQL索引的设计与使用原则,涵盖磁盘数据页的存储结构、页分裂机制、主键索引设计及查询过程、聚簇索引和二级索引的原理、B+树索引的维护、联合索引的使用规则、SQL排序和分组时如何利用索引、回表查询对性能的影响以及索引覆盖的概念。此外还讨论了索引设计的案例,包括如何处理where筛选和order by排序之间的冲突、低基数字段的处理方式、范围查询字段的位置安排,以及通过辅助索引来优化特定查询场景。总结了设计索引的原则,如尽量包含where、order by、group by中的字段,选择离散度高的字段作为索引,限制索引数量,并针对频繁查询的低基数字段进行特殊处理等。
MySQL原理简介—9.MySQL索引原理
|
25天前
|
存储 关系型数据库 MySQL
MySQL索引学习笔记
本文深入探讨了MySQL数据库中慢查询分析的关键概念和技术手段。
101 13
|
28天前
|
存储 关系型数据库 MySQL
浅入浅出——MySQL索引
本文介绍了数据库索引的概念和各种索引结构,如哈希表、B+树、InnoDB引擎的索引运作原理等。还分享了覆盖索引、联合索引、最左前缀原则等优化技巧,以及如何避免索引误用,提高数据库性能。
|
1月前
|
SQL 存储 关系型数据库
MySQL秘籍之索引与查询优化实战指南
最左前缀原则。不冗余原则。最大选择性原则。所谓前缀索引,说白了就是对文本的前几个字符建立索引(具体是几个字符在建立索引时去指定),比如以产品名称的前 10 位来建索引,这样建立起来的索引更小,查询效率更快!
115 22
 MySQL秘籍之索引与查询优化实战指南
|
1月前
|
存储 关系型数据库 MySQL
MySQL中为什么要使用索引合并(Index Merge)?
通过这些内容的详细介绍和实际案例分析,希望能帮助您深入理解索引合并及其在MySQL中的
134 10
|
2月前
|
存储 关系型数据库 MySQL
【MYSQL】 ——索引(B树B+树)、设计栈
索引的特点,使用场景,操作,底层结构,B树B+树,MYSQL设计栈
|
2月前
|
存储 Oracle 关系型数据库
索引在手,查询无忧:MySQL索引简介
MySQL 是一款广泛使用的关系型数据库管理系统,在2024年5月的DB-Engines排名中得分1084,仅次于Oracle。本文介绍MySQL索引的工作原理和类型,包括B+Tree、Hash、Full-text索引,以及主键、唯一、普通索引等,帮助开发者优化查询性能。索引类似于图书馆的分类系统,能快速定位数据行,极大提高检索效率。
78 8
|
2月前
|
SQL 关系型数据库 MySQL
深入解析MySQL的EXPLAIN:指标详解与索引优化
MySQL 中的 `EXPLAIN` 语句用于分析和优化 SQL 查询,帮助你了解查询优化器的执行计划。本文详细介绍了 `EXPLAIN` 输出的各项指标,如 `id`、`select_type`、`table`、`type`、`key` 等,并提供了如何利用这些指标优化索引结构和 SQL 语句的具体方法。通过实战案例,展示了如何通过创建合适索引和调整查询语句来提升查询性能。
358 9