MySQL索引怎么使用
发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,小编给大家分享一下MySQL索引怎么使用,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!MySQL索引的使用实例一. 慢查询日志二. 查询分析器--explain三. 索引的基本使用四.
千家信息网最后更新 2025年02月04日MySQL索引怎么使用
小编给大家分享一下MySQL索引怎么使用,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!
MySQL索引的使用实例
一. 慢查询日志
二. 查询分析器--explain
三. 索引的基本使用
四. 复合索引
五. 覆盖索引
一. 慢查询日志
//查看是否开启慢查询日志mysql> show variables like '%slow%';//临时开启慢查询日志mysql> set global slow_query_log=ON;//查看是否开启慢查询日志mysql> show variables like '%slow%';
//查询超过多少时间就可以记录,上面是如果超过10秒就要记录mysql> show variables like '%long%';//改成一秒,如果超过一秒就写到慢日志里面去(一般一秒是最好的)mysql> set long_query_time=1;//查看日记存储方式,默认FILEmysql> show variables like '%log_output%';// 慢查询日志文件所在位置mysql> show variables like '%datadir%';
//响应时间是3秒,超过了原先设定的一秒mysql> select sleep(3);
我们去文件夹里面查看时发现它已经被存入慢查询日记里面
这部分写明了如何通过慢日志找出比较慢的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');
//通过explain解读他,后面加一个\G便于阅读mysql> explain select * from employee where name='柳峰'\G;//扫描快捷mysql> explain select * from employee where id=13\G;
效果:如下图,可以看之前为什么那么慢,需要四秒响应时间
三. 索引的基本使用
mysql> show index from employee\G;//主键会默认建一个id索引
创建索引 效率提升
//查询分析mysql> explain select * from employee where name='柳峰';//创建普通索引mysql> create index idx_name on employee(name);
//删除mysql> drop index idx_name on employee;
老师 事列:
如过用like检索,效率还是不变,所以要看你怎么用
四. 复合索引
//查的时候可以看到一个主键索引mysql> show index from employee\G;
目前是all全局扫描
select * from employee where name ='柳峰';//查询分析explain select * from employee where name ='柳峰'\G;
创建索引
//创建索引create index idx_name_salary_dept on employee(name,salary,dept);//查询分析explain select * from employee where name ='柳峰'\G;
验证有name就能索引
// name和salarymysql> explain select * from employee where name ='柳峰' and salary=8800\G;//name和deptmysql> explain select * from employee where name ='柳峰' and dept='部门A'\G;
没有name就不能使用索引
mysql> explain select * from employee where salary=8800;mysql> explain select * from employee where dept='部门A';
五. 覆盖索引
按照上面步骤,我们可以看到四个索引,第一个是主键索引,后面是复合索引name_salary_dept
mysql> show index from employee;
如何触发
我们用id作为查询数据
mysql> select * from employee;mysql> select * from employee where id =11;
只查id
mysql> explain select id from employee employee where id=11\G;mysql> explain select id from employee\G;
//查name,salarymysql> explain select name,salary from employee;//查name,salary,deptmysql> explain select name,salary,dept from employee;//因为没有sxe条件,所以只能做全部扫描type为nullmysql> explain select name,sex,salary,dept from employee;
看完了这篇文章,相信你对MySQL索引怎么使用有了一定的了解,想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!
索引
部门
查询
日志
分析
时间
分析器
效率
数据
文件
日记
篇文章
普通
位置
作用
全局
姓名
完了
实例
小花
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
hp服务器售后新华三
扬州网络安全处理办法
网络安全的重要性案例
戴尔网络安全rsa
漠河市网络安全宣传活动
国研网数据库怎么样
淄博电商软件开发推荐
软件开发行业增值税即征即退政策
如何查看本机软件开发商
网络安全发从草案
数据库的创建与维护实验心得体会
服务器带外管理 产生
服务器临时存文件路径
虹口区特定软件开发服务产品介绍
长城宽带未连接上服务器
什么是通信软件开发
基因表达谱数据库
以网络安全为题目写作文
静安区银联软件开发管理系统
光盘数据库检索方法
idea生产数据库配置
桓台售后服务crm软件开发
什么云服务器好用又安全
pg数据库设置接收只读连接
计算机软件开发在哪里学
请检查服务器是怎么回事
服务器怎么查硬盘序列号
金山区创新数据库服务商服务费
计算机培训数据库
个人与公司签的软件开发协议