资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

profile怎么在mysql中使用-创新互联

这篇文章给大家介绍profile怎么在mysql中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

创新互联的客户来自各行各业,为了共同目标,我们在工作上密切配合,从创业型小企业到企事业单位,感谢他们对我们的要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。专业领域包括网站建设、做网站、电商网站开发、微信营销、系统平台开发。

profile是什么

当我们要对某一条sql的性能进行分析时,可以使用它。

Profiling是从 mysql5.0.3版本以后才开放的。

启动profile之后,所有查询包括错误的语句都会记录在内。

关闭会话或者set profiling=0 就关闭了。(如果将profiling_history_size参数设置为0,同样具有关闭MySQL的profiling效果。)

此工具可用来查询SQL执行状态,System lock和Table lock 花多少时间等等,

对定位一条语句的I/O消耗和CPU消耗 非常重要。(SQL 语句执行所消耗的大两部分资源就是IO和CPU)

--在mysql5.7之后,profile信息将逐渐被废弃,mysql推荐使用performance schema

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 |
+----------------------+----------+

关于profile怎么在mysql中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前题目:profile怎么在mysql中使用-创新互联
URL网址:http://cdkjz.cn/article/dpdooo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220