Mysql高级2-SQL性能分析

这篇具有很好参考价值的文章主要介绍了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次

 文章来源地址https://www.toymoban.com/news/detail-607126.html

二、慢查询日志

  慢查询日志记录了所有执行时间超过指定参数(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性能分析的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【MySQL进阶】SQL性能分析

    MySQL 客户端连接成功后,通过 show [session|global] status 命令可以提供服务器状态信 息。通过如下指令,可以查看当前数据库的 INSERT 、 UPDATE 、 DELETE 、 SELECT 的访问频次: Com_delete: 删除次数    Com_insert: 插入次数 Com_select: 查询次数   Com_update: 更新次数 我们可以在当前数据库

    2024年02月07日
    浏览(43)
  • MySQL 优化—— SQL 性能分析

    MySQL 客户端连接成功后,通过 show [session | global] status 命令可以提供服务其状态信息。通过下面指令,可以查看当前数据库 CRUD 的访问频次: SHOW GLOBAL STATUS LIKE \\\'Com_______\\\'; 七个下划线代表这个七个占位。 查询数据库中整体的 CURD 频次,一般针对 select 比较多的数据库。 慢查询

    2024年02月13日
    浏览(46)
  • [MySQL] SQL优化之性能分析

    🌈键盘敲烂,年薪30万🌈 目录 一、索引优化 1、索引是什么: 2、索引的数据结构: 3、索引种类: 4、sql分析(回表查询) 二、定位慢查询语句 1、慢查询日志 2、profile详情 3、explain执行计划(重点) 4、查看执行频次   1、索引是什么: 通过一些约束,快速查询到相应字段

    2024年02月05日
    浏览(32)
  • Mysql的SQL性能分析【借助EXPLAIN分析】

    要说sql有问题,需要拿出证据,因此需要性能分析 Mysql中有专门负责优化SELECT语句的优化器模块,主要功能:通过计算分析系统中收集到的统计信息,为客户端请求的Query提供他认为最优的执行计划(它认为最优的数据检索方式,不见得是DBA认为是最优的,这部分最耗费时间,

    2024年02月12日
    浏览(54)
  • mysql-sql性能分析工具

            MySQL 客户端连接成功后,通过 show [session|global] status 命令可以提供服务器状态信息。通过如下指令,可以查看当前数据库的INSERT、UPDATE、DELETE、SELECT的访问频次: -- session 是查看当前会话 ; -- global 是查询全局数据 ; SHOW GLOBAL STATUS LIKE \\\'Com_\\\'; 慢查询日志记录了所有执

    2024年02月12日
    浏览(35)
  • 【MySQL数据库 | 第十九篇】SQL性能分析工具

    目录   前言: SQL执行频率: 慢查询日志: profile: profile各个指令: 总结:         本篇我们将为大家讲解SQL性能的分析工具,而只有熟练的掌握了性能分析的工具,才可以更好的对SQL语句进行优化。虽然我们在自己练习的时候对这种优化感知并不明显,但是如果我们要

    2024年02月09日
    浏览(36)
  • MySQL数据库进阶第二篇(索引,SQL性能分析,使用规则)

    本篇博客深入详细地介绍了数据库索引的概念和重要性。内容包含:索引的概念和目标、索引的优点与缺点。此外,博客还深入解析了三种主要的索引结构:B-Tree、B+Tree和Hash,提供了详细的结构解析和优化方法,并通过插图进一步增强了理解。 博客的部分内容专注于对B-Tr

    2024年02月21日
    浏览(42)
  • MySQL进阶篇:索引(概述,结构,分类,语法,SQL性能分析,索引使用,设计原则)

    索引(index)是帮助MysQL 高效获取数据的数据结构 ( 有序 )。 在数据之外,数据库系统还维护着满足特定查找算法的数据结构,这些数据结构以某种方式引用(指向)数据,这样就可以在这些数据结构上实现高级查找算法,这种数据结构就是索引。 优缺点: MySQL的索引是在存储

    2024年01月20日
    浏览(31)
  • Hive执行计划之只有map阶段SQL性能分析和解读

    目录 目录 概述 1.不带函数操作的select-from-where型简单SQL 1.1执行示例 1.2 运行逻辑分析 1.3 伪代码解释 2.带普通函数和运行操作符的普通型SQL执行计划解读 2.1 执行计划解读 2.2 伪代码解释逻辑 可能所有的SQLboy刚接触SQL语句的时候都是select xxx from xxx where xxx。在hive中,我们把这

    2024年02月08日
    浏览(35)
  • 【MYSQL高级】Mysql找出执行慢的SQL【慢查询日志使用与分析】

    慢查询的开启并捕获:开启慢查询日志,设置阈值,比如超过5秒钟的就是慢SQL,至少跑1天,看看生产的慢SQL情况,并将它抓取出来 explain + 慢SQL分析 show Profile。(比explain还要详细,可以查询SQL在MySQL数据库中的执行细节和生命周期情况) 运维经理 OR DBA,进行MySQL数据库服务

    2024年02月13日
    浏览(27)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包