百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT技术 > 正文

mysql的表索引相关(mysql索引的用法)

wptr33 2025-05-03 16:58 11 浏览

索引的创建以及删除

1.alter table

2.create/drop index

mysql> create index idx_b on t (cls_id);
ERROR 1072 (42000): Key column 'cls_id' doesn't exist in table

desc方式查看

mysql> desc students;
+------------+-----------------+------+-----+---------+----------------+
| Field      | Type            | Null | Key | Default | Extra          |
+------------+-----------------+------+-----+---------+----------------+
| id         | bigint unsigned | NO   | PRI | NULL    | auto_increment |
| created_at | datetime(3)     | YES  |     | NULL    |                |
| updated_at | datetime(3)     | YES  |     | NULL    |                |
| deleted_at | datetime(3)     | YES  | MUL | NULL    |                |
| sno        | bigint          | YES  |     | NULL    |                |
| pwd        | varchar(32)     | NO   |     | NULL    |                |
| tel        | varchar(12)     | NO   |     | NULL    |                |
| birth      | datetime(3)     | YES  |     | NULL    |                |
| cls_id     | bigint          | YES  | MUL | NULL    |                |
+------------+-----------------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
show create table 方式查看
mysql> show create table students;
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  `deleted_at` datetime(3) DEFAULT NULL,
  `sno` bigint DEFAULT NULL,
  `pwd` varchar(32) NOT NULL,
  `tel` varchar(12) NOT NULL,
  `birth` datetime(3) DEFAULT NULL,
  `cls_id` bigint DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_students_deleted_at` (`deleted_at`),
  KEY `idx_b` (`cls_id`)
) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.01 sec)

mysql> show index from students
    -> ;
+----------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table    | Non_unique | Key_name                | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+----------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| students |          0 | PRIMARY                 |            1 | id          | A         |          45 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| students |          1 | idx_students_deleted_at |            1 | deleted_at  | A         |           1 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
| students |          1 | idx_b                   |            1 | cls_id      | A         |           2 |     NULL |   NULL | YES  | BTREE      |         |               | YES     | NULL       |
+----------+------------+-------------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
3 rows in set (0.04 sec)

索引核心是降低io的操作

explain select的查询序列号

1.数字越小,表示最外面

2.数字越大表示优先级最高(最里面的先执行)

3.数字相同表示是同一组的 同一组里的按顺序来从上到下执行

mysql> explain select * from su a ,(select c2 from su where id=10) b where a.c2=b.c2;
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+-------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key     | key_len | ref   | rows  | filtered | Extra       |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+-------+----------+-------------+
|  1 | SIMPLE      | su    | NULL       | const | PRIMARY       | PRIMARY | 4       | const |     1 |   100.00 | NULL        |
|  1 | SIMPLE      | a     | NULL       | ALL   | NULL          | NULL    | NULL    | NULL  | 87778 |    10.00 | Using where |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+-------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)

4.当type 中出现了all 表示走了全表扫描 possible_keys (可能用到的索引) key(索引名称) rows (扫描的行数 一般超过5000行就有问题需要进行优化)

5.update 或者delete也会使用到对应的索引(select,update,delete都是能加速的)


索引分为单列索引和联合索引

创建方法: create index idx_c3_c4 on su(c3,c4)

查看索引的信息:

mysql> show index from su;
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table | Non_unique | Key_name  | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| su    |          0 | PRIMARY   |            1 | id          | A         |      472487 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| su    |          1 | idx_c2    |            1 | c2          | A         |      305177 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| su    |          1 | idx_c3_c4 |            1 | c3          | A         |      304103 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
| su    |          1 | idx_c3_c4 |            2 | c4          | A         |      305613 |     NULL |   NULL |      | BTREE      |         |               | YES     | NULL       |
+-------+------------+-----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
4 rows in set (0.03 sec)

查询

mysql> explain select * from su where c3="222";  #用到索引了
+----+-------------+-------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key       | key_len | ref   | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | su    | NULL       | ref  | idx_c3_c4     | idx_c3_c4 | 4       | const |    2 |   100.00 | NULL  |
+----+-------------+-------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.01 sec)

mysql> explain select * from su where c4="222";    #当使用c4查询时没有使用到对应的索引
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
|  1 | SIMPLE      | su    | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 472487 |    10.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

 explain select * from su where c4="222" order by c3;
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-----------------------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra                       |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-----------------------------+
|  1 | SIMPLE      | su    | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 472487 |    10.00 | Using where; Using filesort |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-----------------------------+
1 row in set, 1 warning (0.00 sec)

mysql> explain select * from su where c3="222" order by c4;
+----+-------------+-------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key       | key_len | ref   | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | su    | NULL       | ref  | idx_c3_c4     | idx_c3_c4 | 4       | const |    2 |   100.00 | NULL  |
+----+-------------+-------+------------+------+---------------+-----------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
#all 运算符没有使用到索引
mysql> explain select * from su where c3="222" or c4="123";
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
|  1 | SIMPLE      | su    | NULL       | ALL  | idx_c3_c4     | NULL | NULL    | NULL | 472487 |    19.00 | Using where |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

总结: 1.创建联合索引时 最左前缀原则 当单独使用c4时是无法使用索引的

2.在orderby 进行排序时也是遵循最左前缀原则的 走索引(c3必须在前面才行)

3.or 关键字在联合索引中不起作用(单列索引是可以) index merge(索引的合并),只能用一个索引(同时使用到单列和联合索引) 转化为 合并索引

4.不支持中文索引,但是支持全文索引如何创建全文索引

5.索引中不会包含有null值的列,当使用count(*)时对null值不走,对于创建索引时我们建议创建非空字段 不建议在null上的字段简历索引

6.在进行select语句时不要进行函数运算 ,否则你创建索引了也用不到索引

mysql> create index idx_c4 on su(c4);
Query OK, 0 rows affected (3.83 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> explain select * from su where c3="222" or c4="123";
+----+-------------+-------+------------+-------------+------------------+------------------+---------+------+------+----------+-------------------------------------------------+
| id | select_type | table | partitions | type        | possible_keys    | key              | key_len | ref  | rows | filtered | Extra                                           |
+----+-------------+-------+------------+-------------+------------------+------------------+---------+------+------+----------+-------------------------------------------------+
|  1 | SIMPLE      | su    | NULL       | index_merge | idx_c3_c4,idx_c4 | idx_c3_c4,idx_c4 | 4,4     | NULL |    3 |   100.00 | Using sort_union(idx_c3_c4,idx_c4); Using where |
+----+-------------+-------+------------+-------------+------------------+------------------+---------+------+------+----------+-------------------------------------------------+
1 row in set, 1 warning (0.01 sec)

删除索引: alter table su drop index idx_c4;

mysql> ALTER TABLE su drop index idx_c4;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

尽量少使用or 否则就健单列索引

全文索引

create fulltext index idx_su on su(1);

覆盖索引

通过索引即可查到数据(就是覆盖素引) Extra 中使用 Usingindex

查询列就是索引列

mysql> explain select c2 from su where c2=123;
+----+-------------+-------+------------+------+---------------+--------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key    | key_len | ref   | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+--------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | su    | NULL       | ref  | idx_c2        | idx_c2 | 4       | const |    1 |   100.00 | Using index |
+----+-------------+-------+------------+------+---------------+--------+---------+-------+------+----------+-------------+

当extra 中使用的是 filesort 时需要优化

函数运算索引

mysql> explain select * from su where YEAR(C5)<2007;
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+
|  1 | SIMPLE      | su    | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 497738 |   100.00 | Using where |
+----+-------------+-------+---------
  
  未使用函数时
  mysql> explain select * from su where C5<"2007-12-12:14:00";
+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-----------------------+
| id | select_type | table | partitions | type  | possible_keys | key    | key_len | ref  | rows | filtered | Extra                 |
+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-----------------------+
|  1 | SIMPLE      | su    | NULL       | range | idx_c5        | idx_c5 | 4       | NULL |    1 |   100.00 | Using index condition |
+----+-------------+-------+------------+-------+---------------+--------+---------+------+------+----------+-----------------------+
1 row in set, 2 warnings (0.00 sec)

1.提高排队效率

2.创建索引在非null的字段上进行创建,在有null的字段会扫描对应 的null数据

3.联合索引降低搜索的范围(单行索引和联合索引没有绝对的好与不好)

1.通过索引 扫描超过30%时走全表扫描

2.函数 以及模糊匹配时无法使用索引 like 语句

3.不综训最左前缀原则不走索引

4.两个单读索引 一个用排序 orderby 需要创建联合索引


相关推荐

每天一个编程技巧!掌握这7个神技,代码效率飙升200%

“同事6点下班,你却为改BUG加班到凌晨?不是你不努力,而是没掌握‘偷懒’的艺术!本文揭秘谷歌工程师私藏的7个编程神技,每天1分钟,让你的代码从‘能用’变‘逆天’。文末附《Python高效代码模板》,...

Git重置到某个历史节点(Sourcetree工具)

前言Sourcetree回滚提交和重置当前分支到此次提交的区别?回滚提交是指将改动的代码提交到本地仓库,但未推送到远端仓库的时候。...

git工作区、暂存区、本地仓库、远程仓库的区别和联系

很多程序员天天写代码,提交代码,拉取代码,对git操作非常熟练,但是对git的原理并不甚了解,借助豆包AI,写个文章总结一下。Git的四个核心区域(工作区、暂存区、本地仓库、远程仓库)是版本控制的核...

解锁人生新剧本的密钥:学会让往事退场

开篇:敦煌莫高窟的千年启示在莫高窟321窟的《降魔变》壁画前,讲解员指着斑驳色彩说:"画师刻意保留了历代修补痕迹,因为真正的传承不是定格,而是流动。"就像我们的人生剧本,精彩章节永远...

Reset local repository branch to be just like remote repository HEAD

技术背景在使用Git进行版本控制时,有时会遇到本地分支与远程分支不一致的情况。可能是因为误操作、多人协作时远程分支被更新等原因。这时就需要将本地分支重置为与远程分支的...

Git恢复至之前版本(git恢复到pull之前的版本)

让程序回到提交前的样子:两种解决方法:回退(reset)、反做(revert)方法一:gitreset...

如何将文件重置或回退到特定版本(怎么让文件回到初始状态)

技术背景在使用Git进行版本控制时,经常会遇到需要将文件回退到特定版本的情况。可能是因为当前版本出现了错误,或者想要恢复到之前某个稳定的版本。Git提供了多种方式来实现这一需求。...

git如何正确回滚代码(git命令回滚代码)

方法一,删除远程分支再提交①首先两步保证当前工作区是干净的,并且和远程分支代码一致$gitcocurrentBranch$gitpullorigincurrentBranch$gi...

[git]撤销的相关命令:reset、revert、checkout

基本概念如果不清晰上面的四个概念,请查看廖老师的git教程这里我多说几句:最开始我使用git的时候,我并不明白我为什么写完代码要用git的一些列指令把我的修改存起来。后来用多了,也就明白了为什么。gi...

利用shell脚本将Mysql错误日志保存到数据库中

说明:利用shell脚本将MYSQL的错误日志提取并保存到数据库中步骤:1)创建数据库,创建表CreatedatabaseMysqlCenter;UseMysqlCenter;CREATET...

MySQL 9.3 引入增强的JavaScript支持

MySQL,这一广泛采用的开源关系型数据库管理系统(RDBMS),发布了其9.x系列的第三个更新版本——9.3版,带来了多项新功能。...

python 连接 mysql 数据库(python连接MySQL数据库案例)

用PyMySQL包来连接Python和MySQL。在使用前需要先通过pip来安装PyMySQL包:在windows系统中打开cmd,输入pipinstallPyMySQL ...

mysql导入导出命令(mysql 导入命令)

mysql导入导出命令mysqldump命令的输入是在bin目录下.1.导出整个数据库  mysqldump-u用户名-p数据库名>导出的文件名  mysqldump-uw...

MySQL-SQL介绍(mysql sqlyog)

介绍结构化查询语言是高级的非过程化编程语言,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以具有完全不同底层结构的不同数据库系统,可以使用相同...

MySQL 误删除数据恢复全攻略:基于 Binlog 的实战指南

在MySQL的世界里,二进制日志(Binlog)就是我们的"时光机"。它默默记录着数据库的每一个重要变更,就像一位忠实的史官,为我们在数据灾难中提供最后的救命稻草。本文将带您深入掌握如...