`
zhaoshijie
  • 浏览: 2242533 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

性能优化重要的mysql profile功能

阅读更多


关键字:性能优化重要的mysql profile功能


mysql的sql语句优化都使用explain,但是这个没有办法知道详细的Memory/CPU等使用量
MySQL Query Profiler, 可以查询到此 SQL 语句会执行多少, 并看出 CPU/Memory 使用


量, 执行过程 System lock, Table lock 花多少时间等等.
mysql> show variables like 'profiling%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| profiling              | OFF   |
| profiling_history_size | 15    |
+------------------------+-------+

开启此功能
mysql>set profiling=1;
mysql> show variables like 'profiling%';
# 此命令会让mysql在 information_schema 的 database 建立一個 PROFILING 的

table 来记录.
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| profiling              | ON    |
| profiling_history_size | 15    |
+------------------------+-------+
profiling_history_size记录多少次查询

mysql> show profiles;
+----------+------------+------------------------------------+
| Query_ID | Duration   | Query                              |
+----------+------------+------------------------------------+
|        1 | 0.00018100 | show variables like 'profiling%'   |
|        2 | 0.00020400 | show variables like 'profiling%'   |
|        3 | 0.00007800 | set profiling=1                    |
|        4 | 0.00011000 | show variables like 'profiling%'   |
|        5 | 0.00002400 | select count(1) from `mrhao_stats` |
|        6 | 1.52181400 | select count(*) from `mrhao_stats` |
|        7 | 0.00026900 | show variables like 'profiling%'   |

mysql> show profile for query 6;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| (initialization)               | 0.000003 |
| checking query cache for query | 0.000042 |
| Opening tables                 | 0.00001 |
| System lock                    | 0.000004 |
| Table lock                     | 0.000025 |
| init                           | 0.000009 |
| optimizing                     | 0.000003 |
| statistics                     | 0.000007 |
| preparing                      | 0.000007 |
| executing                      | 0.000004 |
| Sending data                   | 1.521676 |
| end                            | 0.000007 |
| query end                      | 0.000003 |
| storing result in query cache | 0.000002 |
| freeing items                  | 0.000006 |
| closing tables                 | 0.000004 |
| logging slow query             | 0.000002 |
+--------------------------------+----------+
17 rows in set (0.00 sec)

mysql> show profile cpu for query 6;
+--------------------------------+----------+----------+------------+
| Status                         | Duration | CPU_user | CPU_system |
+--------------------------------+----------+----------+------------+
| (initialization)               | 0.000003 | 0        | 0          |
| checking query cache for query | 0.000042 | 0.001    | 0          |
| Opening tables                 | 0.00001 | 0        | 0          |
| System lock                    | 0.000004 | 0        | 0          |
| Table lock                     | 0.000025 | 0        | 0          |
| init                           | 0.000009 | 0        | 0          |
| optimizing                     | 0.000003 | 0        | 0          |
| statistics                     | 0.000007 | 0        | 0          |
| preparing                      | 0.000007 | 0        | 0          |
| executing                      | 0.000004 | 0        | 0          |
| Sending data                   | 1.521676 | 1.631752 | 0.036995   |
| end                            | 0.000007 | 0        | 0          |
| query end                      | 0.000003 | 0        | 0          |
| storing result in query cache | 0.000002 | 0        | 0          |
| freeing items                  | 0.000006 | 0        | 0          |
| closing tables                 | 0.000004 | 0        | 0          |
| logging slow query             | 0.000002 | 0        | 0          |
+--------------------------------+----------+----------+------------+
17 rows in set (0.00 sec)
    * 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

查询时间跟cpu的使用
mysql> select min(seq) seq,state,count(*) numb_ops,
    round(sum(duration),5) sum_dur, round(avg(duration),5) avg_dur,
    round(sum(cpu_user),5) sum_cpu, round(avg(cpu_user),5) avg_cpu
    from information_schema.profiling
    where query_id = 7
    group by state
    order by seq;
关闭此功能

mysql> set profiling=0;
mysql> show variables like 'profiling%';
+------------------------+-------+
| Variable_name          | Value |
+------------------------+-------+
| profiling              | OFF   |
| profiling_history_size | 15    |
+------------------------+-------+
分享到:
评论

相关推荐

    MySQL性能分析工具profile使用教程

    分析SQL执行带来的开销是优化SQL的重要手段。在MySQL数据库中,可以通过配置profiling参数来启用SQL剖析。该参数可以在全局和session级别来设置。对于全局级别则作用于整个MySQL实例,而session级别紧影响当前...

    MySQL中使用SHOW PROFILE命令分析性能的用法整理

    主要介绍了MySQL中使用show profile命令分析性能的用法整理,show profiles是数据库性能优化的常用命令,需要的朋友可以参考下

    mysql高级视频教程百度云(2019).txt

    51.MySQL高级_用Show Profile进行sql分析.avi 50.MySQL高级_批量插入数据脚本.avi 49.MySQL高级_慢查询日志.avi 48.MySQL高级_为排序使用索引OrderBy优化.avi 47.MySQL高级_in和exists.avi 46.MySQL高级_...

    mysql lunix的安装

    这个是MySQL5.5再contos6下的安装方法 MySQL 是...索引的优化策略,了解innodb和myisam存储引擎,熟悉MySQL锁机制,能熟练配置MySQL主从复制,熟练掌握explain、show profile、慢查询日志等日常SQL诊断和性能分析策略。

    尚硅谷Java视频教程_MySQL高级视频

    尚硅谷_MySQL高级_用Show Profile进行sql分析 · 52.尚硅谷_MySQL高级_全局查询日志 · 53.尚硅谷_MySQL高级_数据库锁理论概述 · 54.尚硅谷_MySQL高级_读锁案例讲解 · 55.尚硅谷_MySQL高级_读锁案例讲解2 · ...

    2018最新Mysql高级视频教程

    MySQL是目前流行的关系型数据库管理系统,在WEB...索引的优化策略,了解innodb和myisam存储引擎,熟悉MySQL锁机制,能熟练配置MySQL主从复制,熟练掌握explain、show profile、慢查询日志等日常SQL诊断和性能分析策略。

    04尚硅谷_MySQL高级_BD版.docx

    MySQL是目前流行的关系型数据库管理系统,在WEB...索引的优化策略,了解innodb和myisam存储引擎,熟悉MySQL锁机制,能熟练配置MySQL主从复制,熟练掌握explain、show profile、慢查询日志等日常SQL诊断和性能分析策略。

    MySQL利用profile分析慢sql详解(group left join效率高于子查询)

    最近遇到一个查询比较慢的sql语句,用了子查询,大概需要0.8秒左右,这个消耗时间比较长,严重影响了性能,所以需要进行优化。单独查询单表或者子查询记录都很快,下面来看看详细的介绍。 开启profile mysql> show...

    MySql整理(基础进阶运维).docx

    外连接查询,自连接查询,联合查询union,unionall,子查询,事务,四大特性ACID,并发事务,进阶篇,存储引擎,InnoDB,MyISAM,Memory,存储引擎特点,存储引擎的选择,性能分析,查看执行频次,慢查询日志,profile,explain,索引,...

    通俗易懂的MySQL高级教程(含配套资料)

    本教程为授权出品 MySQL是目前流行的关系型...索引的优化策略,了解innodb和myisam存储引擎,熟悉MySQL锁机制,能熟练配置MySQL主从复制,熟练掌握explain、show profile、慢查询日志等日常SQL诊断和性能分析策略。

    Mysql自带profiling性能分析工具使用分享

    1. show variables like ‘%profiling%’;(查看profiling信息)  ...4. show profile for query 4;   show profile 的格式如下: SHOW PROFILE [type [, type] … ]  [FOR QUERY n]  [LIMIT row_

    利用profiles来查看sql 语句执行计划

    用于MYSQL数据库关键SQL语句的资源消耗的量化以及针对性地分析和优化

    Mastering The Faster Web with PHP, MySQL, and JavaScript 1st pdf

    This book will get you started with the latest benchmarking, profiling and monitoring tools for PHP, MySQL and JavaScript using Docker-based technologies. From optimizing ...

    异步并行加载工具Asyncload.zip

    背景前段时间在做应用的性能优化时,分析了下整体请求,profile看到90%的时间更多的是一些外部服务的I/O等待,cpu利用率其实不高,在10%以 下。 单次请求的响应时间在50ms左右,所以tps也不会太高,测试环境压力...

    java8集合源码分析-pangdan:面试相关技能

    数组、链表、二叉树、队列、栈的各种操作(性能,场景) 二分查找和各种变种的二分查找 各类排序算法以及复杂度分析(快排、归并、堆) 各类算法题(手写) 理解并可以分析时间和空间复杂度。 动态规划(笔试回回有...

    asp.net知识库

    SQL Server 2005的30个最重要特点 同时安装sql2000和sql2005的经验 类如何与界面绑定 在Asp.net中如何用SQLDMO来获取SQL Server中的对象信息 使用Relations建立表之间的关系并却使用PagedDataSource类对DataList进行...

Global site tag (gtag.js) - Google Analytics