mysql教程栏目实例讲解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;//查看日记存储方式,默认FILE mysql> 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和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;
没有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,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;
消息,Binance交易所宣布将于2026年6月30日上线Lumentum、Meta、Microsoft、Palantir和Invesco QQQ Trust交易...
2 德意志银行上调美债收益率预测消息,德意志银行利率策略师修正了对两年期和10年期美国国债收益率的年底预测。他们的最新...
3 USDT在印度交易溢价达7%至10%消息,USDT在印度加密平台的交易价格远高于面值,溢价达7%至10%。当地交易所表示,这一现象...
4 两名巨鲸以高杠杆做空BTC,空单总规模超消息,据Lookonchain监测,两名巨鲸正在做空BTC,空单总规模超过1亿美元。其中一名巨鲸以40倍杠...
5 马来亚银行:日本拥有充足的日元干预“消息,马来亚银行分析师指出,日元兑美元汇率突破162关口引发了干预风险,因为该水平被视...
6 Ignas:欧洲用户失去对全球中心化交易所消息,DeFi分析师Ignas表示,欧洲用户正在失去对全球版中心化交易所的访问权限。他指出,O...
7 币安将于2026年7月1日进行比特币网络钱包消息,币安宣布将于2026年7月1日01:00对比特币网络进行钱包维护。在维护期间,比特币网络的存...
8 鲸鱼地址0xa6e以25倍杠杆开设22000枚ETH空单消息,鲸鱼地址0xa6e最近以25倍杠杆开设了一笔新的22,000枚ETH空单,名义价值约为3500万美元。...
9 Pump.fun累计回购超4亿美元PUMP代币,平台总消息,Pump.fun平台已累计回购超过4亿美元的PUMP代币,涉及约1455亿枚PUMP。回购计划自2025年7月启...
10 欧盟已发放244张MiCA牌照,德国与法国占比消息,欧盟截至6月29日已发放244张MiCA牌照,其中德国以57张居首,法国以26张位列第二。希腊、...
成都来彰科技 蜀ICP备2025134723号-1
资讯来源互联网,如有版权问题请联系管理员删除。