索引的基本信息`

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;
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值