热门标签 | 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;



推荐阅读
  • 本章将深入探讨移动 UI 设计的核心原则,帮助开发者构建简洁、高效且用户友好的界面。通过学习设计规则和用户体验优化技巧,您将能够创建出既美观又实用的移动应用。 ... [详细]
  • 本文详细介绍了优化DB2数据库性能的多种方法,涵盖统计信息更新、缓冲池调整、日志缓冲区配置、应用程序堆大小设置、排序堆参数调整、代理程序管理、锁机制优化、活动应用程序限制、页清除程序配置、I/O服务器数量设定以及编入组提交数调整等方面。通过这些技术手段,可以显著提升数据库的运行效率和响应速度。 ... [详细]
  • 本文介绍了如何利用JavaScript或jQuery来判断网页中的文本框是否处于焦点状态,以及如何检测鼠标是否悬停在指定的HTML元素上。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 在使用 DataGridView 时,如果在当前单元格中输入内容但光标未移开,点击保存按钮后,输入的内容可能无法保存。只有当光标离开单元格后,才能成功保存数据。本文将探讨如何通过调用 DataGridView 的内置方法解决此问题。 ... [详细]
  • 本文由瀚高PG实验室撰写,详细介绍了如何在PostgreSQL中创建、管理和删除模式。文章涵盖了创建模式的基本命令、public模式的特性、权限设置以及通过角色对象简化操作的方法。 ... [详细]
  • 本文详细探讨了JDBC(Java数据库连接)的内部机制,重点分析其作为服务提供者接口(SPI)框架的应用。通过类图和代码示例,展示了JDBC如何注册驱动程序、建立数据库连接以及执行SQL查询的过程。 ... [详细]
  • PostgreSQL 10 离线安装指南
    本文详细介绍了如何在无法联网的服务器上进行 PostgreSQL 10 的离线安装,并涵盖了从下载安装包到配置远程访问的完整步骤。 ... [详细]
  • 作为一名 Ember.js 新手,了解如何在路由和模型中正确加载 JSON 数据是至关重要的。本文将探讨两者之间的差异,并提供实用的建议。 ... [详细]
  • 本文介绍 SQL Server 的基本概念和操作,涵盖系统数据库、常用数据类型、表的创建及增删改查等基础操作。通过实例帮助读者快速上手 SQL Server 数据库管理。 ... [详细]
  • 云函数与数据库API实现增删查改的对比
    本文将深入探讨使用云函数和数据库API实现数据操作(增删查改)的不同方法,通过详细的代码示例帮助读者更好地理解和掌握这些技术。文章不仅提供代码实现,还解释了每种方法的特点和适用场景。 ... [详细]
  • 本文详细介绍了如何在Android 4.4及以上版本中配置WebView以实现内容的自动高度调整和屏幕适配,确保中文显示正常,并提供代码示例。 ... [详细]
  • 本文详细介绍了 Apache Jena 库中的 Txn.executeWrite 方法,通过多个实际代码示例展示了其在不同场景下的应用,帮助开发者更好地理解和使用该方法。 ... [详细]
  • 本文探讨了MariaDB在当前数据库市场中的地位和挑战,分析其可能面临的困境,并提出了对未来发展的几点看法。 ... [详细]
  • 本文探讨了如何通过WebBrowser控件在用户点击输入框时自动显示图片验证码。该过程可能涉及JavaScript事件的触发与响应。 ... [详细]
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社区 版权所有