一、mysql 索引
1. 索引基础认知
索引是数据库中排好序的索引值 + 数据行物理地址的列表,作用类似图书目录,能通过二分查找快速定位数据,避免全表扫描,大幅提升查询效率;但索引会占用额外磁盘空间,且增删改操作会因维护索引变慢,需按需创建。
补充知识点
- mysql 默认索引结构是 b + 树,查询效率极高。
- 索引会加快查询,但会减慢增删改。
- 小表、重复值多的字段不适合建索引。
2. 索引的分类
(1)物理分类:聚簇索引 & 非聚簇索引
- 聚簇索引:按数据物理存储位置排序,多行检索速度快(innodb 主键默认是聚簇索引)
- 非聚簇索引:索引与数据物理存储分离,单行检索速度快(myisam 默认非聚簇索引)
补充知识点
- 一张表只能有一个聚簇索引(主键)。
- 非聚簇索引需要回表查询,效率略低。
(2)逻辑分类(核心常用)
表格
| 索引类型 | 核心特点 | 适用场景 |
|---|---|---|
| 普通索引 | 无任何限制,最基础 | where/join 中频繁出现的字段 |
| 唯一索引 | 索引列值唯一,允许空值 | 需字段唯一但可空的场景 |
| 主键索引 | 特殊唯一索引,一个表仅一个,不允许空值 | 表的唯一标识 |
| 组合索引 | 多个字段组成,遵循最左前缀原则 | 多条件联合查询 |
| 全文索引 | 针对文本内容检索 | 大文本模糊查询 |
3. 索引的创建(标准格式 + 代码)
(1)普通索引
标准命令格式
create index 索引名 on 表名(字段名); alter table 表名 add index 索引名(字段名);
代码
-- 直接创建 create index index_name on table_name(column(length)); -- 修改表结构创建 alter table table_name add index index_name (column(length)); -- 建表时创建 create table `table` ( `id` int(11) not null auto_increment, `title` char(255) not null, primary key (`id`), index index_name (title(50)) );
补充知识点
- 普通索引无约束,只用于加速查询。
- 前缀索引只索引前 n 个字符,节省空间。
(2)唯一索引
标准命令格式
create unique index 索引名 on 表名(字段名); alter table 表名 add unique index 索引名(字段名);
代码
-- 直接创建 create unique index index_name on table_name(column(length)); -- 修改表结构创建 alter table table_name add unique index_name (column(length)); -- 建表时创建 create table `table` ( `id` int(11) not null auto_increment, `title` char(255) not null, primary key (`id`), unique indexname (title(50)) );
补充知识点
- 唯一索引值不可重复,但允许 null。
- 一个表可以创建多个唯一索引。
(3)主键索引
标准命令格式
create table 表名( 字段名 类型 primary key auto_increment );
代码
create table `table_name` ( `id` int(11) not null auto_increment, `title` char(255) not null, primary key (`id`) );
补充知识点
- 主键索引不能为空、不能重复。
- 一张表只能有一个主键。
(4)组合索引
标准命令格式
create index 索引名 on 表名(字段1,字段2,字段3);
代码
create table user( `name` varchar(9), `age` int(3), `sex` tinyint(1), index user_index(name, age, sex) ); -- 有效查询 select * from user where name = '张三' and age = 20 and sex = 1; -- 无效查询 select * from user where age = 20 and sex = 1;
补充知识点
- 组合索引遵循最左前缀原则,必须从第一个字段开始匹配。
- 建一个组合索引 ≈ 建多个单列索引。
(5)全文索引
标准命令格式
alter table 表名 add fulltext index 索引名(字段名); create fulltext index 索引名 on 表名(字段名);
代码
create table `table` ( `id` int(11) not null auto_increment, `title` char(255) not null, `content` text null, primary key (`id`), fulltext (content) ); alter table article add fulltext index_content(content); create fulltext index index_content on article(content);
补充知识点
- 全文索引比 like 快很多,专门用于文本搜索。
- 只支持 char、varchar、text 类型。
4. 索引的查看与删除
(1)查看索引
标准命令格式
show index from 表名;
代码
show index from tablename; show keys from tablename; show index from renyuan\g;
(2)删除索引
标准命令格式
drop index 索引名 on 表名; alter table 表名 drop index 索引名;
代码
alter table 表名 drop index 索引名; drop index 索引名 on 表名;
5. 创建索引的核心原则
- 查多增删改少的表适合建索引,小表无需建索引
- where、join、group by、order by 字段优先建索引
- 低基数字段(性别、状态)不建索引
- 大字段用前缀索引
- 组合索引严格遵循最左前缀原则
二、mysql 事务
1. 事务基础认知
事务是一组不可分割的操作,要么全部成功,要么全部回滚,保证数据安全。
- 只有 innodb 支持事务
- 只管理 insert/update/delete
补充知识点
- 事务用于保证数据一致性,如转账、订单、支付。
- 事务失败会自动回滚,不会产生脏数据。
2. 事务的 acid 四大特性
- 原子性:不可分割,全成功或全失败
- 一致性:数据前后规则不变
- 隔离性:并发事务互不干扰
- 持久性:提交后永久生效
补充知识点
- acid 是事务的核心标准。
- 原子性保证操作安全,一致性保证数据不乱。
3. 事务的隔离级别
innodb 默认:可重复读(repeatable read)
- 读未提交:脏读
- 读提交:不可重复读
- 可重复读:幻读
- 串行化:无问题,但性能低
补充知识点
- 脏读:读到未提交的数据
- 不可重复读:同一事务两次查询结果不同
- 幻读:查询时突然多 / 少了数据
4. 事务控制语句(格式 + 代码)
标准命令格式
begin; -- 开启事务 commit; -- 提交 rollback; -- 回滚 savepoint 名称; -- 保存点 set autocommit=0; -- 关闭自动提交
代码
begin; commit; rollback; savepoint sp1; rollback to sp1; release savepoint sp1; set autocommit=0; set autocommit=1;
补充知识点
- mysql 默认自动提交,执行 sql 立即生效。
- 手动事务必须手动 commit 才会保存。
事务实操示例
create database if not exists kgc; use kgc; create table test( id int(5)) engine=innodb; begin; insert into test value(1); insert into test value(2); commit; begin; insert into test values(3); rollback;
三、补充:库 & 表操作(格式 + 知识点)
1. 数据库操作
标准格式
create database 库名; use 库名; drop database 库名; show databases;
代码
create database if not exists test; use test;
补充知识点
- 必须 use 库名,否则无法操作表。
- if not exists 防止重复创建报错。
2. 数据表操作
标准格式
create table 表名(字段 类型 约束) engine=innodb; desc 表名; show tables;
代码
create table user(
id int primary key auto_increment
) engine=innodb;
desc user;补充知识点
- 必须用 innodb 才能用事务和索引。
- desc 表名:快速查看表结构。
四、最终总结
- 索引 = 查询加速,有固定创建 / 删除格式
- 事务 = 数据安全,要么全成要么全滚
- 操作表前必须 use 库名
- 表引擎必须是 innodb
- 索引和事务要按需使用,平衡性能与安全
到此这篇关于mysql 索引与事务核心知识点全解的文章就介绍到这了,更多相关myslq索引与事务内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论