热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

MySQL显示SQL语句执行时间的实例详解

本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQLQueryProfiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。

本节内容:

显示SQL语句执行时间

MySQL 的 SQL 语法调整主要使用 EXPLAIN,不过该命令无法获取详细的 Ram(Memory)/CPU 等使用量.

于 MySQL 5.0.37 以上开始支持 MySQL Query Profiler, 可以查詢到此 SQL执行多长时间,并 並看出 CPU/Memory 使用量, 执行过程中 System lock, Table lock 花多少时间等.

效能分析主要分下述三种:

Bottleneck analysis - focuses on answering the questions: What is my database server waiting on; what is a user connection waiting on; what is a piece of SQL code waiting on?

Workload analysis - examines the server and who is logged on to determine the resource usage and activity of each.

Ratio-based analysis - utilizes a number of rule-of-thumb ratios to gauge performance of a database, user connection, or piece of code.

MySQL Query Profile 使用方法

启动:

mysql> set profiling=1; # 此命令于 MySQL 会于 information_schema 的 database 建立一个 PROFILING 的 table 来记录.

SQL profiles show

mysql> show profiles; # 从启动之后所有语法及使用时间, 含错误语法都会记录.

例如:

代码示例:

(root@jbxue.com) [test]> show profiles; # 注意 Query_ID, 下面执行时间统计等, 都是依 Query_ID 在记录

+----------+------------+---------------------------+

| Query_ID | Duration   | Query                     |

+----------+------------+---------------------------+

|        1 | 0.00090400 | show profile for query 1  |

|        2 | 0.00008700 | select * from users       |

|        3 | 0.00183800 | show tables               |

|        4 | 0.00027600 | mysql> show profiles      |

+----------+------------+---------------------------+

1,查询所有花费时间加总

代码示例:

mysql> select sum(duration) from information_schema.profiling where query_id=1; # Query ID = 1

+---------------+

| sum(duration) |

+---------------+

|      0.000447 |

+---------------+

2,查询各执行阶段花费多少时间

代码示例:

mysql> show profile for query 1; # Query ID = 1

+--------------------+------------+

| Status             | Duration   |

+--------------------+------------+

| (initialization)   | 0.00006300 |

| Opening tables     | 0.00001400 |

| System lock        | 0.00000600 |

| Table lock         | 0.00001000 |

| init               | 0.00002200 |

| optimizing         | 0.00001100 |

| statistics         | 0.00009300 |

| preparing          | 0.00001700 |

| executing          | 0.00000700 |

| Sending data       | 0.00016800 |

| end                | 0.00000700 |

| query end          | 0.00000500 |

| freeing items      | 0.00001200 |

| closing tables     | 0.00000800 |

| logging slow query | 0.00000400 |

+--------------------+------------+

3,查询各执行阶段花费的各种资源列表

代码示例:

mysql> show profile cpu for query 1; # Query ID = 1

+--------------------------------+----------+----------+------------+

| Status                         | Duration | CPU_user | CPU_system |

+--------------------------------+----------+----------+------------+

| (initialization)               | 0.000007 | 0        | 0          |

| checking query cache for query | 0.000071 | 0        | 0          |

| Opening tables                 | 0.000024 | 0        | 0          |

| System lock                    | 0.000014 | 0        | 0          |

| Table lock                     | 0.000055 | 0.001    | 0          |

| init                           | 0.000036 | 0        | 0          |

| optimizing                     | 0.000013 | 0        | 0          |

| statistics                     | 0.000021 | 0        | 0          |

| preparing                      | 0.00002  | 0        | 0          |

| executing                      | 0.00001  | 0        | 0          |

| Sending data                   | 0.015072 | 0.011998 | 0          |

| end                            | 0.000021 | 0        | 0          |

| query end                      | 0.000011 | 0        | 0          |

| storing result in query cache  | 0.00001  | 0        | 0          |

| freeing items                  | 0.000018 | 0        | 0          |

| closing tables                 | 0.000019 | 0        | 0          |

| logging slow query             | 0.000009 | 0        | 0          |

+--------------------------------+----------+----------+------------+

mysql> show profile IPC for query 1;

+--------------------------------+----------+---------------+-------------------+

| Status                         | Duration | Messages_sent | Messages_received |

+--------------------------------+----------+---------------+-------------------+

| (initialization)               | 0.000007 |             0 |                 0 |

| checking query cache for query | 0.000071 |             0 |                 0 |

| Opening tables                 | 0.000024 |             0 |                 0 |

| System lock                    | 0.000014 |             0 |                 0 |

| Table lock                     | 0.000055 |             0 |                 0 |

| init                           | 0.000036 |             0 |                 0 |

| optimizing                     | 0.000013 |             0 |                 0 |

| statistics                     | 0.000021 |             0 |                 0 |

| preparing                      | 0.00002  |             0 |                 0 |

| executing                      | 0.00001  |             0 |                 0 |

| Sending data                   | 0.015072 |             0 |                 0 |

| end                            | 0.000021 |             0 |                 0 |

| query end                      | 0.000011 |             0 |                 0 |

| storing result in query cache  | 0.00001  |             0 |                 0 |

| freeing items                  | 0.000018 |             0 |                 0 |

| closing tables                 | 0.000019 |             0 |                 0 |

| logging slow query             | 0.000009 |             0 |                 0 |

+--------------------------------+----------+---------------+-------------------+

4,其它属性列表

ALL - displays all information

BLOCK IO - displays counts for block input and output operations

CONTEXT SWITCHES - displays counts for voluntary and involuntary context switches

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

5,设定 Profiling 存的 Size:

代码示例:

mysql> show variables where variable_name='profiling_history_size'; # 预设是 15笔

关闭:

代码示例:

mysql> set profiling=0;



推荐阅读
  • 本文介绍了在使用Laravel和sqlsrv连接到SQL Server 2016时,如何在插入查询中使用输出子句,并返回所需的值。同时讨论了使用CreatedOn字段返回最近创建的行的解决方法以及使用Eloquent模型创建后,值正确插入数据库但没有返回uniqueidentifier字段的问题。最后给出了一个示例代码。 ... [详细]
  • WhenIusepythontoapplythepymysqlmoduletoaddafieldtoatableinthemysqldatabase,itdo ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • PDO MySQL
    PDOMySQL如果文章有成千上万篇,该怎样保存?数据保存有多种方式,比如单机文件、单机数据库(SQLite)、网络数据库(MySQL、MariaDB)等等。根据项目来选择,做We ... [详细]
  • Postgresql备份和恢复的方法及命令行操作步骤
    本文介绍了使用Postgresql进行备份和恢复的方法及命令行操作步骤。通过使用pg_dump命令进行备份,pg_restore命令进行恢复,并设置-h localhost选项,可以完成数据的备份和恢复操作。此外,本文还提供了参考链接以获取更多详细信息。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 图解redis的持久化存储机制RDB和AOF的原理和优缺点
    本文通过图解的方式介绍了redis的持久化存储机制RDB和AOF的原理和优缺点。RDB是将redis内存中的数据保存为快照文件,恢复速度较快但不支持拉链式快照。AOF是将操作日志保存到磁盘,实时存储数据但恢复速度较慢。文章详细分析了两种机制的优缺点,帮助读者更好地理解redis的持久化存储策略。 ... [详细]
  • 解决VS写C#项目导入MySQL数据源报错“You have a usable connection already”问题的正确方法
    本文介绍了在VS写C#项目导入MySQL数据源时出现报错“You have a usable connection already”的问题,并给出了正确的解决方法。详细描述了问题的出现情况和报错信息,并提供了解决该问题的步骤和注意事项。 ... [详细]
  • 本文详细介绍了MySQL表分区的创建、增加和删除方法,包括查看分区数据量和全库数据量的方法。欢迎大家阅读并给予点评。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 本文讨论了在使用sp_msforeachdb执行动态SQL命令时,当发生错误时如何捕获数据库名称。提供了两种解决方案,并介绍了如何正确使用'?'来显示数据库名称。 ... [详细]
  • Windows7 64位系统安装PLSQL Developer的步骤和注意事项
    本文介绍了在Windows7 64位系统上安装PLSQL Developer的步骤和注意事项。首先下载并安装PLSQL Developer,注意不要安装在默认目录下。然后下载Windows 32位的oracle instant client,并解压到指定路径。最后,按照自己的喜好对解压后的文件进行命名和压缩。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
author-avatar
pet宠物情缘
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有