`

mysql显示SQL语句执行时间

 
阅读更多

查看 MySQL 語法 詳細執行時間 與 CPU/記憶體使用量: MySQL Query Profiler

 

 

MySQL 的 SQL 語法調整主要都是使用 EXPLAIN , 但是這個並沒辦法知道詳細的 Ram(Memory)/CPU 等使用量.

於 MySQL 5.0.37 以上開始支援 MySQL Query Profiler, 可以查詢到此 SQL 會執行多少時間, 並看出 CPU/Memory 使用量, 執行過程中 System lock, Table lock 花多少時間等等.

MySQL Query Profile 詳細介紹可見: Using the New MySQL Query Profiler (2007.04.05 發表)

效能分析主要分下述三種(轉載自上篇):

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; # 從啟動之後所有語法及使用時間, 含錯誤語法都會紀錄.

ex: (root@localhost) [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      |

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

查詢所有花費時間加總

 

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

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

| sum(duration) |

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

|      0.000447 |

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

查詢各執行階段花費多少時間

 

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 |

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

查詢各執行階段花費的各種資源列表

 

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 |

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

其它屬性列表

 

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

設定 Profiling 存的 Size

 

mysql> show variables where variable_name='profiling_history_size'; # 預設是 15筆

關閉

 

mysql> set profiling=0;

分享到:
评论

相关推荐

    Oracle Sql语句转换成Mysql Sql语句

    本项目提供了一个Java源码工具,能够帮助用户便捷地将Oracle SQL语句转换为MySQL SQL语句。 Oracle SQL与MySQL SQL的主要差异在于以下几个方面: 1. **数据类型**:Oracle支持的数据类型如NUMBER、LONG、RAW等在...

    Effective MySQL之SQL语句最优化.pdf

    尽管如此,我将基于标题和描述中提供的关键词“Effective MySQL之SQL语句最优化”来构建知识点。 1. SQL语句最优化的概念:在数据库管理中,对SQL语句进行优化是提高数据库性能的关键环节。最优化的SQL语句能够在...

    mysql 显示SQL语句执行时间的代码

    於 MySQL 5.0.37 以上開始支援 MySQL Query Profiler, 可以查詢到此 SQL 會執行多少時間, 並看出 CPU/Memory 使用量, 執行過程中 System lock, Table lock 花多少時間等等. MySQL Query Profile 詳細介紹可見: ...

    Effective MySQL之SQL语句最优化(高清)

    《Effective MySQL之SQL语句最优化》提供了很多可以用于改进数据库和应用程序性能的最佳实践技巧,并对这些技巧做了详细的解释。《Effective MySQL之SQL语句最优化》希望能够通过一步步详细介绍SQL优化的方法,帮助...

    Mysql常用SQL语句

    MySQL常用SQL语句 MySQL是一种关系型数据库管理系统,使用SQL(Structured Query Language)语言来管理和操作数据库。下面是MySQL中一些常用的SQL语句: 创建、删除和基本查询 * 显示数据库:`show databases;` *...

    《Effective MySQL之SQL语句最优化》手册

    《Effective MySQL之SQL语句最优化》是一本深入探讨如何提升数据库性能的专业手册,它针对SQL语句的优化提供了一系列实用且高效的策略。在数据库管理领域,SQL优化是提升系统性能的关键环节,尤其是在处理大数据量时...

    连接mysql,及简单sql语句执行

    本文将详细讲解如何连接MySQL以及执行基本的SQL语句,旨在帮助初学者快速上手。 首先,我们需要了解连接MySQL的必要工具。在命令行环境下,可以使用`mysql`客户端直接与服务器交互;而在编程环境中,如Python,我们...

    Mysql运行SQL语句中文乱码问题的解决方法

    在MySQL数据库中运行包含中文字符的SQL语句时,经常会遇到中文显示为乱码的问题。这一现象不仅降低了开发效率,还可能导致数据错误。本文将详细介绍如何彻底解决MySQL运行SQL语句中文乱码的问题。 #### 一、理解...

    《Effective MySQL之SQL语句最优化》数据库SQL

    《Effective MySQL之SQL语句最优化》是一本深入探讨如何提升数据库性能,特别是针对SQL查询进行优化的专业书籍。在数据库管理中,SQL语句的优化是至关重要的,它直接影响到系统的响应速度和整体效率。本篇文章将依据...

    介绍六个有用的MySQL的SQL语句

    MySQL SQL 语句实践指南 MySQL 是一种广泛使用的关系数据库管理系统,SQL 语句是其核心组成部分。今天,我们将介绍六个有用的 MySQL SQL 语句,帮助您更好地使用 MySQL 数据库。 1. 计算年数 计算年数是日常生活...

    MySQL SQL语句练习题及答案

    MySQL SQL语句练习题及答案 本资源提供了 MySQL SQL 语句的练习题及答案,涵盖了创建表、插入数据、删除数据、更新数据、查询数据等多方面的知识点。 一、创建表 在 MySQL 中,创建表使用 CREATE TABLE 语句。...

    SQL语句执行器

    SQL语句执行器是一款专为初学者和数据库管理员设计的在线工具,允许用户在网站后台直接输入并执行SQL(Structured Query Language)语句,以便于查询、管理以及操作数据库。这款程序极大地简化了数据库交互的过程,...

    java定时执行sql语句

    通过配置数据库连接信息和要执行的sql语句,可实现定时执行多个sql语句。 所要执行的语句只能是写死的,可支持sqlserver mysql oracle。 配置说明: config/sys.properties 中指定数据库类型及连接信息,执行间隔...

    PB 从SQL语句获取数据存储(MySQL)

    标题 "PB 从SQL语句获取数据存储(MySQL)" 指的是使用PowerBuilder (PB) 开发工具,通过SQL语句从MySQL数据库中检索和处理数据存储的过程。在这个Demo中,PB11.5 版本被用作开发环境,而MySQL作为后台数据库系统。...

    MySQL mysql_query 函数执行SQL语句.docx

    MySQL mysql_query 函数执行 SQL 语句 mysql_query() 函数是 PHP MySQL 函数库中的一种函数,用于向 MySQL 发送并执行 SQL 语句。该函数可以对数据库进行增删改查等操作,並返回执行结果。 参数说明: * query:...

    mysql sql常用语句大全

    mysql sql常用语句大全

    mysql转化成sql server sql转化成mysql工具

    2. 转换规则应用:根据SQL Server的语法特性,工具会将MySQL的SQL语句转化为相应的SQL Server语法。 3. 数据导出:将MySQL中的数据导出为SQL脚本,或者直接导入到SQL Server中。 4. 验证与调整:迁移后,需要验证...

    MySQL常用sql语句生成器

    下面将详细介绍MySQL中的SQL语句及其生成器的相关知识。 1. SQL基础概念 SQL,全称为结构化查询语言,是用于创建、查询、更新和删除数据库中数据的标准语言。它分为DQL(查询语言)、DML(数据操纵语言)、DDL...

Global site tag (gtag.js) - Google Analytics