Mysql高级2-SQL性能分析
一、SQL执行频率
MySQL客户端 连接成功后,通过show [session | global] status 命令可以提供服务器状态信息,通过如下指令,可以查看当前数据库的insert,update,dalete,select的访问冰刺
show [global | session] status like "Com_______"; # 七个_ 表示起个通配符
mysql> show global status like 'Com_______';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Com_binlog | 0 |
| Com_commit | 0 |
| Com_delete | 0 |
| Com_import | 0 |
| Com_insert | 0 |
| Com_repair | 0 |
| Com_revoke | 0 |
| Com_select | 4 |
| Com_signal | 0 |
| Com_update | 0 |
| Com_xa_end | 0 |
+---------------+-------+
11 rows in set (0.00 sec)
说明1:上面的数据库被执行查询4次
二、慢查询日志
慢查询日志记录了所有执行时间超过指定参数(long_query_time 单位:秒,默认10秒)的所有SQL语句的日志,Mysql的慢查询日志默认没有开启,需要在Mysql的配置文件中(通常在/etc/my.cnf)中配置如下信息:
可以使用一下语句查询慢查询是否开启
mysql> show variables like 'slow_query_log';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| slow_query_log | OFF |
+----------------+-------+
1 row in set (0.01 sec)
说明:慢查询默认是关闭的
# 开启慢查询
slow_query_log=1 # 设置慢查询的时间
long_query_time=2
再次查询
mysql> show variables like 'slow_query_log';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| slow_query_log | ON |
+----------------+-------+
1 row in set (0.00 sec)
慢日志文件通常指mysql的安装目录里面的data文件夹中。
三、profile
3.1 show profiles
可以查看每一条SQL的耗时基本情况
mysql> show profiles;
+----------+-------------+-----------------------------------------------------------------------+
| Query_ID | Duration | Query |
+----------+-------------+-----------------------------------------------------------------------+
| 11 | 0.00020000 | SELECT DATABASE() |
| 12 | 0.00029000 | SELECT DATABASE() |
| 13 | 0.00040900 | SELECT DATABASE() |
| 14 | 0.00145600 | show databases |
| 15 | 0.00279800 | show tables |
| 16 | 12.28066100 | select * from account_transaction |
| 17 | 0.00166700 | select * from account_transaction where id = 1 |
| 18 | 6.01525200 | select * from account_transaction where trade_no="164126925202017539" |
| 19 | 6.64749300 | select * from account_transaction where trade_no="164126925202017539" |
| 20 | 5.39658800 | select * from account_transaction where trade_no="164126923751014167" |
| 21 | 0.00067300 | select * from account_transaction where id=100 |
| 22 | 0.00046900 | select * from account_transaction where id=1000 |
| 23 | 0.00045200 | select * from account_transaction where id=10000 |
| 24 | 0.00052900 | select * from account_transaction where id=100000 |
| 25 | 0.00038300 | select * from account_transaction where id=20000 |
+----------+-------------+-----------------------------------------------------------------------+
15 rows in set, 1 warning (0.00 sec)
说明1:第16条查询全部数据花费了12.28秒,第17条根据id查询只花费了0.001秒,第18条通过普通字段查询花费了6.00秒
说明2:SQL中能不做全量查询就不要做全量查询。
说明3:SQL中能通过id查询就不要通过其他字段查询,因为毕竟其他字段的查询还是会根据二级索引查到id,再根据id查询到具体的数据的。
3.2 have_profiling
参数have_profiling能够看到当前mysql是否支持profile操作:
mysql> select @@have_profiling;
+------------------+
| @@have_profiling |
+------------------+
| YES |
+------------------+
1 row in set, 1 warning (0.00 sec)
说明1:这里的YES只是说明该版本的mysql是支持profile操作的,但是不代表profile操作是开始的,仅代表有这个功能而已!
默认profiling是关闭的,可以通过set语句在session/global级别开启profiling;
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 0 |
+-------------+
1 row in set, 1 warning (0.01 sec)
3.3 开启profiling
mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
1 row in set, 1 warning (0.00 sec)
3.4 查看指定SQL耗时
通过带query_id的SQL语句各个阶段的耗时情况
show profile for query query_id;
mysql> show profile for query 20;
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| starting | 0.000083 |
| Executing hook on transaction | 0.000007 |
| starting | 0.000007 |
| checking permissions | 0.000006 |
| Opening tables | 0.000107 |
| init | 0.000012 |
| System lock | 0.000010 |
| optimizing | 0.000012 |
| statistics | 0.000025 |
| preparing | 0.000041 |
| executing | 5.393642 |
| end | 0.000016 |
| query end | 0.000005 |
| waiting for handler commit | 0.000009 |
| closing tables | 0.000009 |
| freeing items | 0.002130 |
| logging slow query | 0.000426 |
| cleaning up | 0.000041 |
+--------------------------------+----------+
18 rows in set, 1 warning (0.01 sec)
3.5 查看指定SQL的CPU使用情况
show profile cpu for query query_id
mysql> show profile cpu for query 20;
+--------------------------------+----------+----------+------------+
| Status | Duration | CPU_user | CPU_system |
+--------------------------------+----------+----------+------------+
| starting | 0.000083 | 0.000072 | 0.000009 |
| Executing hook on transaction | 0.000007 | 0.000003 | 0.000004 |
| starting | 0.000007 | 0.000006 | 0.000002 |
| checking permissions | 0.000006 | 0.000004 | 0.000002 |
| Opening tables | 0.000107 | 0.000058 | 0.000017 |
| init | 0.000012 | 0.000005 | 0.000006 |
| System lock | 0.000010 | 0.000008 | 0.000002 |
| optimizing | 0.000012 | 0.000010 | 0.000002 |
| statistics | 0.000025 | 0.000023 | 0.000001 |
| preparing | 0.000041 | 0.000027 | 0.000014 |
| executing | 5.393642 | 2.294837 | 0.151005 |
| end | 0.000016 | 0.000007 | 0.000009 |
| query end | 0.000005 | 0.000003 | 0.000001 |
| waiting for handler commit | 0.000009 | 0.000009 | 0.000001 |
| closing tables | 0.000009 | 0.000008 | 0.000002 |
| freeing items | 0.002130 | 0.000037 | 0.000063 |
| logging slow query | 0.000426 | 0.000034 | 0.000175 |
| cleaning up | 0.000041 | 0.000021 | 0.000018 |
+--------------------------------+----------+----------+------------+
18 rows in set, 1 warning (0.00 sec)
四、explain执行计划
explain 或者 desc 命令获取Mysql如何执行select 语句的信息,包括在select 语句在执行过程中表如何连接,及连接的顺序
4.1 语法
explain/desc select 字段列表 from 表名 where 条件;
4.2 示例
mysql> select * from account_transaction where id=100;
+-----+--------------------+--------+--------+----------------------------+---------------+--------------+--------+---------+-----------------+-------------------+-----------+--------+
| id | trade_no | type | method | time | payment | out_trade_no | amount | balance | trader_staff_id | operator_staff_id | device_id | remark |
+-----+--------------------+--------+--------+----------------------------+---------------+--------------+--------+---------+-----------------+-------------------+-----------+--------+
| 100 | 156384784634000449 | TOP_UP | CASH | 2019-07-23 02:10:46.929559 | LOCAL_ACCOUNT | | 10000 | 10000 | 449 | 11 | 7 | |
+-----+--------------------+--------+--------+----------------------------+---------------+--------------+--------+---------+-----------------+-------------------+-----------+--------+
1 row in set (0.00 sec) mysql> explain select * from account_transaction where id=100;
+----+-------------+---------------------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------------------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | account_transaction | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL |
+----+-------------+---------------------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)
4.3 explain字段含义
参数id:select查询的序列号,表示查询语句中的执行顺序,如果id相同,执行顺序从上到下,id不同,值越大,越先执行
mysql> select s.*, c.* from student s, course c,student_course sc where s.id=sc.student_id and c.id = sc.course_id;
+----+--------+----+--------+
| id | name | id | name |
+----+--------+----+--------+
| 1 | 张三 | 1 | java |
| 1 | 张三 | 2 | python |
| 1 | 张三 | 3 | php |
| 2 | 李四 | 2 | python |
| 2 | 李四 | 3 | php |
| 3 | 王五 | 4 | C |
+----+--------+----+--------+
6 rows in set (0.03 sec) mysql> explain select s.*, c.* from student s, course c,student_course sc where s.id=sc.student_id and c.id = sc.course_id;
+----+-------------+-------+------------+--------+----------------------------+---------+---------+-------------------------+------+----------+--------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+--------+----------------------------+---------+---------+-------------------------+------+----------+--------------------------------------------+
| 1 | SIMPLE | s | NULL | ALL | PRIMARY | NULL | NULL | NULL | 4 | 100.00 | NULL |
| 1 | SIMPLE | sc | NULL | ALL | fk_course_id,fk_student_id | NULL | NULL | NULL | 6 | 33.33 | Using where; Using join buffer (hash join) |
| 1 | SIMPLE | c | NULL | eq_ref | PRIMARY | PRIMARY | 4 | mysql_test.sc.course_id | 1 | 100.00 | NULL |
+----+-------------+-------+------------+--------+----------------------------+---------+---------+-------------------------+------+----------+--------------------------------------------+
3 rows in set, 1 warning (0.00 sec)
说明1:这一个select语句中,涉及到了三个表,所以有三条执行记录。
说明2:虽然搜索的顺序是student,course,student_course,但是执行顺序是student,student_course,course,因为两个表是没有关系的,需要依靠第三张关系表维系
说明3:这是一个三个都是相同id的案例
mysql> select * from student where id in(select student_id from student_course where course_id = (select id from course where name = "python"));
+----+--------+
| id | name |
+----+--------+
| 1 | 张三 |
| 2 | 李四 |
+----+--------+
2 rows in set (0.00 sec) mysql> explain select * from student where id in(select student_id from student_course where course_id = (select id from course where name = "python"));
+----+--------------+----------------+------------+--------+----------------------------+--------------+---------+------------------------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+--------------+----------------+------------+--------+----------------------------+--------------+---------+------------------------+------+----------+-------------+
| 1 | PRIMARY | <subquery2> | NULL | ALL | NULL | NULL | NULL | NULL | NULL | 100.00 | NULL |
| 1 | PRIMARY | student | NULL | eq_ref | PRIMARY | PRIMARY | 4 | <subquery2>.student_id | 1 | 100.00 | NULL |
| 2 | MATERIALIZED | student_course | NULL | ref | fk_course_id,fk_student_id | fk_course_id | 4 | const | 2 | 100.00 | Using where |
| 3 | SUBQUERY | course | NULL | ALL | NULL | NULL | NULL | NULL | 4 | 25.00 | Using where |
+----+--------------+----------------+------------+--------+----------------------------+--------------+---------+------------------------+------+----------+-------------+
4 rows in set, 1 warning (0.00 sec)
说明1:id值越大,越先被执行,所以这个查询,先执行course表的查询,在执行student_course表,最后执行student表
参数select_type:表示select的类型,常见的取值有,SIMPLE、PRIMARY、UNION、SUBQUERY
参数type:表示连接的类型,性能由好到差的链接类型为NULL、system、const、eq_ref、ref、range、index、all;
- 当查询语句中不使用任何表,则查询类型为最优的,但是却在实际工作中,很难做到,不查询表,不然查询的意义是什么呢。
- 当查询系统表的时候,type会为system,所以一般系统表查询比较快
- 当查询id主键或者唯一索引的时候,会出现const类型
- 当查询使用非唯一索引的时候,会出现ref
- 当全表查询的时候会出现all
参数possible_key:可能的索引,一个或者多个
参数key:是实际用到的索引,如果为NULL,则表示没有使用索引
参数key_len:表示索引中使用的字节数,该值为索引字段最大可能长度,并非实际使用长度,在不损失精确性的前提下,长度越短越好。
参数rows:MySQL认为必须要执行的查询的行数,在InnoDB引擎中,是一个估计值,可能并不总是准确的
参数filtered:表示返回结果的行数占需要读取行数的百分比,filtered的值越大越好
Mysql高级2-SQL性能分析的更多相关文章
- mysql高级教程(二)-----性能分析
MySQL常见瓶颈 1.cpu SQL中对大量数据进行比较.关联.排序.分组 2.IO a.实例内存满足不了缓存数据或排序等需要,导致产生大量物理 IO. b.查询执行效率低,扫描过多数据行. 3.锁 ...
- Mysql高级操作学习笔记:索引结构、树的区别、索引优缺点、创建索引原则(我们对哪种数据创建索引)、索引分类、Sql性能分析、索引使用、索引失效、索引设计原则
Mysql高级操作 索引概述: 索引是高效获取数据的数据结构 索引结构: B+Tree() Hash(不支持范围查询,精准匹配效率极高) 树的区别: 二叉树:可能产生不平衡,顺序数据可能会出现链表结构 ...
- 「MySQL高级篇」explain分析SQL,索引失效&&常见优化场景
大家好,我是melo,一名大三后台练习生 专栏回顾 索引的原理&&设计原则 欢迎关注本专栏:MySQL高级篇 本篇速览 在我们上一篇文章中,讲到了索引的原理&&设计原则 ...
- 【MySQL 高级】索引优化分析
MySQL高级 索引优化分析 SQL 的效率问题 出现性能下降,SQL 执行慢,执行时间长,等待时间长等情况,可能的原因有: 查询语句写的不好 索引失效 单值索引:在 user 表中给 name 属性 ...
- 单条sql性能分析与优化
性能分析 1. explain 查看sql执行计划,得出索引使用情况等信息 2. show profiling 查看sql所有执行步骤以及用时,使用步骤如下 1)开启性能剖析 mysql> se ...
- SQL性能分析之执行计划
一直想找一些关于SQL语句性能调试的权威参考,但是有参考未必就能够做好调试的工作.我深信实践中得到的经验是最珍贵的,书本知识只是一个引导.本篇来源于<Inside Microsoft SQL S ...
- SQL性能分析
MySQL常见瓶颈: CPU:CPU在饱和的时候一般发生在数据装入内存或从磁盘上读取数据的时候. IO:磁盘I/O瓶颈发生在装入数据远大于内存容量的时候. 服务器硬件的性能瓶颈:top.free.io ...
- 五分钟打造自己的sql性能分析工具
1.首先要有一个trace文件 2. 打开trace文件 3. 另存为跟踪表 4.登录你要保存到的目标sqlserver服务器 5. 选择要保存的数据库和表名称 6. 保存完成(左下角出现进度直到显示 ...
- [转]Mysql explain用法和性能分析
本文转自:http://blog.csdn.net/haifu_xu/article/details/16864933 from @幸福男孩 MySQL中EXPLAIN解释命令是显示mysql如何 ...
- 数据库隔离级别(mysql+Spring)与性能分析
数据库隔离级别与Spring配置事务的联系及性能影响,以下是个人理解,如果有瑕疵请及时指正. 这里以mysql为例,先明确以下几个问题: 一.一般项目如果不自己配置事务的话,一般默认的是au ...
随机推荐
- 性能_2 Jmeter脚本增强
一.写脚本注意事项(回顾): 协议: http,https必须写 域名或ip: 不能有/ 请求方法: 看清楚接口文档 路径: 不要把 域名和ip再次 路径中,前后空格要看清楚 %20 空格的urlen ...
- php获取未解码之前的原始接口请求参数
前言 目前的几个项目,业务方基本都使用POST方式请求接口,我们本机磁盘会保留一份请求的原始参数用于请求分析和问题排查使用,一般有问题,也会基于seqid(请求唯一id)捞到日志,copy参数模拟请求 ...
- 【11个适合毕设的Python可视化大屏】用pyecharts开发拖拽式可视化数据大屏
你好,我是@马哥python说,一枚10年程序猿. 一.效果演示 以下是我近期用Python开发的原创可视化数据分析大屏,非常适合毕设用,下面逐一展示:(以下是截图,实际上有动态交互效果哦) 以下大屏 ...
- 2023-03-30:用Go语言改写FFmpeg示例decode_audio.c,实现高效音频解码。
2023-03-30:用Go语言改写FFmpeg示例decode_audio.c,实现高效音频解码. 答案2023-03-30: 这个程序的主要功能是将 MP2 音频文件解码为 PCM 格式,并输出到 ...
- 2022-09-10:以下go语言代码输出什么?A:编译错误;B:49.0;C:49。 package main import ( “fmt“ ) func main() { ch
2022-09-10:以下go语言代码输出什么?A:编译错误:B:49.0:C:49. package main import ( "fmt" ) func main() { ch ...
- 2020-11-18:java中,到底多大的对象会被直接扔到老年代?
福哥答案2020-11-18: HotSpot 虚拟机提供了-XX:PretenureSizeThreshold 参数,指定大于该设置值的对象直接在老年代分配,这样做的目的就是避免在 Eden 区及两 ...
- 2021-03-22:小虎去买苹果,商店只提供两种类型的塑料袋,每种类型都有任意数量。1.能装下6个苹果的袋子,2.能装下8个苹果的袋子。小虎可以自由使用两种袋子来装苹果,但是小虎有强迫症,他要求自己使用的袋子数量必须最少,且使用的每个袋子必须装满。给定一个正整数N,返回至少使用多少袋子。如果N无法让使用的每个袋子必须装满,返回-1。
2021-03-22:小虎去买苹果,商店只提供两种类型的塑料袋,每种类型都有任意数量.1.能装下6个苹果的袋子,2.能装下8个苹果的袋子.小虎可以自由使用两种袋子来装苹果,但是小虎有强迫症,他要求自己 ...
- ABP - 初识 ABP
ABP框架 ABP是用于创建现代化Web应用程序的完整体系结构和强大的基础架构,以模块化的方式进行开发,所有模块以nuget包的方式提供,开箱即用,遵循最佳实践和约定,提供SOLID开发经验. 缩写 ...
- Redis使用lua脚本实现库存扣减
为什么使用Lua脚本为什么能合并多个原子操作? Redis官方文档:https://redis.io/docs/manual/programmability/eval-intro/ Redis 保证脚 ...
- 2023-05-18:有 n 名工人。 给定两个数组 quality 和 wage , 其中,quality[i] 表示第 i 名工人的工作质量,其最低期望工资为 wage[i] 。 现在我们想雇佣
2023-05-18:有 n 名工人. 给定两个数组 quality 和 wage , 其中,quality[i] 表示第 i 名工人的工作质量,其最低期望工资为 wage[i] . 现在我们想雇佣 ...