搜索
首页数据库mysql教程实例介绍MySQL索引的使用

实例介绍MySQL索引的使用

Dec 11, 2020 pm 05:34 PM
mysql索引

mysql教程栏目实例讲解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;

在这里插入图片描述

以上是实例介绍MySQL索引的使用的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文转载于:CSDN。如有侵权,请联系admin@php.cn删除
MySQL的角色:Web应用程序中的数据库MySQL的角色:Web应用程序中的数据库Apr 17, 2025 am 12:23 AM

MySQL在Web应用中的主要作用是存储和管理数据。1.MySQL高效处理用户信息、产品目录和交易记录等数据。2.通过SQL查询,开发者能从数据库提取信息生成动态内容。3.MySQL基于客户端-服务器模型工作,确保查询速度可接受。

mysql:构建您的第一个数据库mysql:构建您的第一个数据库Apr 17, 2025 am 12:22 AM

构建MySQL数据库的步骤包括:1.创建数据库和表,2.插入数据,3.进行查询。首先,使用CREATEDATABASE和CREATETABLE语句创建数据库和表,然后用INSERTINTO语句插入数据,最后用SELECT语句查询数据。

MySQL:一种对数据存储的初学者友好方法MySQL:一种对数据存储的初学者友好方法Apr 17, 2025 am 12:21 AM

MySQL适合初学者,因为它易用且功能强大。1.MySQL是关系型数据库,使用SQL进行CRUD操作。2.安装简单,需配置root用户密码。3.使用INSERT、UPDATE、DELETE、SELECT进行数据操作。4.复杂查询可使用ORDERBY、WHERE和JOIN。5.调试需检查语法,使用EXPLAIN分析查询。6.优化建议包括使用索引、选择合适数据类型和良好编程习惯。

MySQL初学者友好吗?评估学习曲线MySQL初学者友好吗?评估学习曲线Apr 17, 2025 am 12:19 AM

MySQL适合初学者,因为:1)易于安装和配置,2)有丰富的学习资源,3)SQL语法直观,4)工具支持强大。尽管如此,初学者需克服数据库设计、查询优化、安全管理和数据备份等挑战。

SQL是一种编程语言吗?澄清术语SQL是一种编程语言吗?澄清术语Apr 17, 2025 am 12:17 AM

是的,sqlisaprogramminglanguges pecialized fordatamanage.1)它具有焦点,focusingonwhattoachieveratherthanhow.2)sqlisessential forquerying forquerying,插入,更新,更新,和detletingdatainrelationalDatabases.3)

解释酸的特性(原子,一致性,隔离,耐用性)。解释酸的特性(原子,一致性,隔离,耐用性)。Apr 16, 2025 am 12:20 AM

ACID属性包括原子性、一致性、隔离性和持久性,是数据库设计的基石。1.原子性确保事务要么完全成功,要么完全失败。2.一致性保证数据库在事务前后保持一致状态。3.隔离性确保事务之间互不干扰。4.持久性确保事务提交后数据永久保存。

MySQL:数据库管理系统与编程语言MySQL:数据库管理系统与编程语言Apr 16, 2025 am 12:19 AM

MySQL既是数据库管理系统(DBMS),也与编程语言紧密相关。1)作为DBMS,MySQL用于存储、组织和检索数据,优化索引可提高查询性能。2)通过SQL与编程语言结合,嵌入在如Python中,使用ORM工具如SQLAlchemy可简化操作。3)性能优化包括索引、查询、缓存、分库分表和事务管理。

mySQL:使用SQL命令管理数据mySQL:使用SQL命令管理数据Apr 16, 2025 am 12:19 AM

MySQL使用SQL命令管理数据。1.基本命令包括SELECT、INSERT、UPDATE和DELETE。2.高级用法涉及JOIN、子查询和聚合函数。3.常见错误有语法、逻辑和性能问题。4.优化技巧包括使用索引、避免SELECT*和使用LIMIT。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
1 个月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

适用于 Eclipse 的 SAP NetWeaver 服务器适配器

将Eclipse与SAP NetWeaver应用服务器集成。

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

Dreamweaver Mac版

Dreamweaver Mac版

视觉化网页开发工具