索引的基本信息`

tech2026-08-07  2

mysql性能下降,执行时间长,等待时间长

查询语句写的垃圾关联太多join服务器调优及各个参数设置(缓冲,线程数等)没索引或者索引失效

索引

概念:索引是帮助MYSQL高效获取数据的数据结构。

优势:提高数据的查找的效率,降低数据排序的成本

缺点:占空间,降低更新表的速度

mysql索引分类: 单值索引, 复合索引,唯一索引

唯一索引

索引列的值必须唯一,但允许有空值

create unique index index_student_name on student(name);

单值索引

一个索引只包含一个列 对于这条查询语句,查询条件是一个字段,使用单值索引。

select * from student where name="张三"; create index index_student_name on student(name);

复合索引

一个索引可以包含多个列 对于这条查询语句,查询条件有多个字段,使用复合索引

select * from student where name="张三" and age=27; create index index_student_name_age on student(name,age);

mysql索引结构

BTree索引Hash索引

有哪些情况需要创建索引

频繁作为查询条件的字段需要建立索引查询中与其他表关联的字段查询中排序的字段查询中统计或分组的字段主键自动建立唯一索引

有哪些情况不需要创建索引

表记录太少经常需要更新的表如果某个数据列包含太多重复的内容

7种JION的SQL编写

表a与表b的公有部分 select * from a inner join b on a.id=b.id; 表a与表b的公有加上表b的私有 select * from a right join b on a.id=b.id; 表a与表b的公有加上表a的私有 select * from a left join b on a.id=b.id; 表a的私有部分 select * from a left join b on a.id=b.id where b.id is null; 表b的私有部分 select * from a right join b on a.id=b.id where a.id is null; 表a与表b的公有加上表a的私有加上表b的私有 select * from a left join b on a.id=b.id union select * from a right join b on a.id=b.id; 表a的私有加上表b的私有 select * from a left join b on a.id=b.id where b.id=null union select * from a right join b on a.id=b.id where a.id=null;
最新回复(0)