mysql中profile的使用

栏目: IT技术 · 发布时间: 6年前 · 82

是什么

mysql官网定义

The SHOW PROFILE and SHOW PROFILES statements display profiling information that indicates resource usage for statements executed during the course of the current session.

简单的说,当前会话资源的消耗情况。

注意:show profile和show Profiles都是不建议使用的,在 mysql 后期的版本中可能会被删除;官网建议使用Performance Schema

怎么使用

profile默认关闭,生产环境中也建议关闭。

查看当前环境的profile设置

mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| have_profiling         | YES   |
| profiling              | OFF   |
| profiling_history_size | 15    |
+------------------------+-------+

profiling off表示profile关闭,profiling_history_size 15表示保存最近15条 SQL 的资源消耗情况。

开启profile功能,可以使用命令

set global profiling = 1;

然后就可以使用下面命令

show profiles;

查看最近15条SQL的情况;
如果要查看某一条的具体情况,SQL格式为:

SHOW PROFILE [type [, type] ... ]
    [FOR QUERY n]
    [LIMIT row_count [OFFSET offset]]

type: {
    ALL
  | BLOCK IO
  | CONTEXT SWITCHES
  | CPU
  | IPC
  | MEMORY
  | PAGE FAULTS
  | SOURCE
  | SWAPS
}

官网对type中各个字段的解释为:

    ALL displays all information

    BLOCK IO displays counts for block input and output operations

    CONTEXT SWITCHES displays counts for voluntary and involuntary context switches

    CPU displays user and system CPU usage times

    IPC displays counts for messages sent and received

    MEMORY is not currently implemented

    PAGE FAULTS displays counts for major and minor page faults

    SOURCE displays the names of functions from the source code, together with the name and line number of the file in which the function occurs

    SWAPS displays swap counts

profiling 对每个会话有效,当会话结束后,当前的profiling信息就会丢失。

使用案例

mysql> show profiles;
+----------+------------+----------------------------+
| Query_ID | Duration   | Query                      |
+----------+------------+----------------------------+
|        1 | 0.00060275 | select * from customers    |
|        2 | 0.00222450 | show tables                |
|        3 | 0.00567425 | select * from offices      |
|        4 | 0.00052050 | show tables                |
|        5 | 0.01123300 | select * from payments     |
|        6 | 0.00111675 | show tables                |
|        7 | 0.02049625 | select * from productlines |
+----------+------------+----------------------------+

在排查SQL执行情况,或者是哪条SQL执行非常慢,慢在哪里;profile都是非常的辅助工具。

显示一条SQL的具体花销在哪里

mysql> show profile for query 7;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000043 |
| checking permissions | 0.000005 |
| Opening tables       | 0.014552 |
| init                 | 0.000025 |
| System lock          | 0.000009 |
| optimizing           | 0.000004 |
| statistics           | 0.000011 |
| preparing            | 0.000010 |
| executing            | 0.000003 |
| Sending data         | 0.005653 |
| end                  | 0.000010 |
| query end            | 0.000009 |
| closing tables       | 0.000020 |
| freeing items        | 0.000121 |
| cleaning up          | 0.000023 |
+----------------------+----------+

信息一目了然,这样我就能对SQL执行情况有个大概的了解。

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

区块链核心算法解析

区块链核心算法解析

【瑞士】Roger Wattenhofer(罗格.瓦唐霍费尔) / 陈晋川、薛云志、林强、祝庆 / 电子工业出版社 / 2017-8 / 59.00

《区块链核心算法解析》介绍了构建容错的分布式系统所需的基础技术,以及一系列允许容错的协议和算法,并且讨论一些实现了这些技术的实际系统。 《区块链核心算法解析》中的主要概念将独立成章。每一章都以一个小故事开始,从而引出该章节的内容。算法、协议和定义都将以形式化的方式描述,以便于读者理解如何实现。部分结论会在定理中予以证明,这样读者就可以明白为什么这些概念或算法是正确的,并且理解它们可以确保实现......一起来看看 《区块链核心算法解析》 这本书的介绍吧!

图片转BASE64编码

图片转BASE64编码

在线图片转Base64编码工具

RGB HSV 转换

RGB HSV 转换

RGB HSV 互转工具

HSV CMYK 转换工具

HSV CMYK 转换工具

HSV CMYK互换工具