MySQL索引怎么使用
创始人
2024-12-18 21:42:27
0次

MySQL索引的使用实例


    • 一. 慢查询日志

    • 二. 查询分析器——explain

    • 三. 索引的基本使用

    • 四. 复合索引

    • 五. 覆盖索引

一. 慢查询日志

//查看是否开启慢查询日志
mysql> show variables like '%slow%';//临时开启慢查询日志
mysql> set global slow_query_log=ON;//查看是否开启慢查询日志
mysql> show variables like '%slow%';

MySQL索引怎么使用

//查询超过多少时间就可以记录,上面是如果超过10秒就要记录
mysql> show variables like '%long%';//改成一秒,如果超过一秒就写到慢日志里面去(一般一秒是最好的)mysql> set long_query_time=1;//查看日记存储方式,默认FILE
mysql> show variables like '%log_output%';// 慢查询日志文件所在位置
mysql> show variables like '%datadir%';

MySQL索引怎么使用

//响应时间是3秒,超过了原先设定的一秒
mysql> select sleep(3);

MySQL索引怎么使用
我们去文件夹里面查看时发现它已经被存入慢查询日记里面

MySQL索引怎么使用

这部分写明了如何通过慢日志找出比较慢的SQL,后面部分要说为什么慢,如何能更快一点。

二. 查询分析器——explain

作用:通过这个可以知道查看sql慢在哪里,需要朝那些方面优化

列:我们创建一个employee数据表

create table employee(
	id int not null auto_increment primary key,
	name varchar(30) comment '姓名',
	sex varchar(1) comment '性别',
	salary int comment '薪资(元)',
	dept varchar(30) comment '部门');insert into employee(name, sex, salary, dept) values('张三', '男', 5500, '部门A');insert into employee(name, sex, salary, dept) values('李洁', '女', 4500, '部门C');insert into employee(name, sex, salary, dept) values('李小梅', '女', 4200, '部门A');insert into employee(name, sex, salary, dept) values('欧阳辉', '男', 7500, '部门C');insert into employee(name, sex, salary, dept) values('李芳', '女', 8500, '部门A');insert into employee(name, sex, salary, dept) values('张江', '男', 6800, '部门A');insert into employee(name, sex, salary, dept) values('李四', '男', 12000, '部门B');insert into employee(name, sex, salary, dept) values('王五', '男', 3500, '部门B');insert into employee(name, sex, salary, dept) values('马小龙', '男', 6000, '部门A');insert into employee(name, sex, salary, dept) values('龙五', '男', 8000, '部门B');insert into employee(name, sex, salary, dept) values('冯小芳', '女', 10000, '部门C');insert into employee(name, sex, salary, dept) values('马小花', '女', 4000, '部门B');insert into employee(name, sex, salary, dept) values('柳峰', '男', 8800, '部门A');

MySQL索引怎么使用

//通过explain解读他,后面加一个\G便于阅读
mysql> explain select * from employee where name='柳峰'\G;//扫描快捷
mysql> explain select * from employee where id=13\G;

MySQL索引怎么使用

效果:如下图,可以看之前为什么那么慢,需要四秒响应时间

MySQL索引怎么使用

三. 索引的基本使用

mysql> show index from employee\G;//主键会默认建一个id索引

MySQL索引怎么使用

创建索引 效率提升

//查询分析
mysql> explain select * from employee where name='柳峰';//创建普通索引
mysql> create index idx_name on employee(name);

MySQL索引怎么使用

//删除
mysql> drop index idx_name on employee;

MySQL索引怎么使用
老师 事列:
MySQL索引怎么使用

如过用like检索,效率还是不变,所以要看你怎么用

MySQL索引怎么使用

四. 复合索引

//查的时候可以看到一个主键索引
mysql> show index from employee\G;

MySQL索引怎么使用

目前是all全局扫描

select * from employee where name ='柳峰';//查询分析
explain select * from employee where name ='柳峰'\G;

MySQL索引怎么使用

创建索引

//创建索引
create index idx_name_salary_dept on employee(name,salary,dept);//查询分析
explain select * from employee where name ='柳峰'\G;

MySQL索引怎么使用

验证有name就能索引

// name和salary
mysql> explain select * from employee where name ='柳峰' and salary=8800\G;//name和dept
mysql> explain select * from employee where name ='柳峰' and dept='部门A'\G;

MySQL索引怎么使用

没有name就不能使用索引

mysql> explain select * from employee where  salary=8800;mysql> explain select * from employee where  dept='部门A';

MySQL索引怎么使用

五. 覆盖索引

按照上面步骤,我们可以看到四个索引,第一个是主键索引,后面是复合索引name_salary_dept

mysql> show index from employee;

MySQL索引怎么使用
如何触发

我们用id作为查询数据

mysql> select * from employee;mysql> select * from employee where id =11;

MySQL索引怎么使用

只查id

mysql> explain select id from employee  employee where id=11\G;mysql> explain select id from employee\G;

MySQL索引怎么使用

//查name,salary
mysql> explain select name,salary from employee;//查name,salary,dept
mysql> explain select name,salary,dept from employee;//因为没有sxe条件,所以只能做全部扫描type为null
mysql> explain select name,sex,salary,dept from employee;

MySQL索引怎么使用

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...