• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >Mysql > mysql 显示SQL语句执行时间的代码

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

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-11

通过本文主要向大家介绍了mysql分页sql语句,mysql的sql语句,mysql动态sql语句,mysql数据库sql语句,mysql执行sql语句等相关知识,希望本文的分享对您有所帮助

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               

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • SQL语句详解 MySQL update的正确用法
  • mysql SQL语句积累
  • 用SQL语句解决mysql导入大数据文件的问题
  • mysql sql语句总结
  • mysql 显示SQL语句执行时间的代码
  • MySQL复制表结构和内容到另一张表中的SQL语句
  • MySQL SQL语句优化的10条建议
  • mysql建表常用sql语句个人经验分享

相关文章

  • 2018-12-05如何使用mysql语句创建联结
  • 2018-12-05MySQL之递归小问题实例分享
  • 2017-05-11解析SQLite中的常见问题与总结详解
  • 2018-12-05MySQL 5.7新特性| Json Column和Generated Column(下)
  • 2018-12-05教你如何在阿里云与腾讯云轻松搭建传统主从复制环境教程
  • 2018-12-05SQLServer 全文检索(full-text)语法
  • 2018-12-05Mysql row number()排序函数的用法和注意_MySQL
  • 2018-12-05SQL 导入导出Excel数据的语句
  • 2018-12-05MySQL InnoDB和MyISAM数据引擎的差别分析
  • 2018-12-05insert into tbl() select * from tb2中加入多个条件

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • MySQL中timestamp字段的一些规则
    • mysql 性能调优五种方式
    • mysql联合查询实例分享
    • mysql中索引与FROM_UNIXTIME的问题详解
    • mysql ndb集群备份数据库和还原数据库的方法
    • sqlserver 支持定位当前页,自定义排序的分页SQL(拒绝动态SQL)
    • powerdesigner 的 PDM文件转excel的方法
    • 从以下几个方面来处理,MySQL主从复制不一致的问题
    • MySQL索引背后的之使用策略及优化(高性能索引策略)
    • mysql中的用户id如何使用nginx访问日志来记录?

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有