mysql profile及其对应表使用

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:


--mysql的profile可用于查看一个sql的具体消耗
show profile all for query 1\G;

--profiling has a default value of 0 (OFF)
mysql> SELECT @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 |
+-------------+

mysql> SET profiling = 1;


--profile 的查询id可能通过如下sql找到
mysql> show profiles;      
+----------+-------------+-------------------------+
| Query_ID | Duration    | Query                   |
+----------+-------------+-------------------------+
|        1 |  0.03872950 | select count(*) from t1 |
|        2 | 10.93732000 | select count(*) from t2 |

-- 变量 profiling_history_size 记录了保存profile记录的条数
mysql> select @@profiling_history_size;
+--------------------------+
| @@profiling_history_size |
+--------------------------+
|                       15 |
+--------------------------+

MariaDB [test]> show profile all for query 1\G;
*************************** 1. row ***************************
             Status: starting
           Duration: 0.000080
           CPU_user: 0.000000
         CPU_system: 0.000000
  Context_voluntary: 0
Context_involuntary: 0
       Block_ops_in: 0
      Block_ops_out: 0
      Messages_sent: 0
  Messages_received: 0
  Page_faults_major: 0
  Page_faults_minor: 0
              Swaps: 0
    Source_function: NULL
        Source_file: NULL
        Source_line: NULL
*************************** 2. row ***************************
             Status: checking permissions
           Duration: 0.000008
           CPU_user: 0.000000
         CPU_system: 0.000000
  Context_voluntary: 0
Context_involuntary: 0
       Block_ops_in: 0
      Block_ops_out: 0
      Messages_sent: 0
  Messages_received: 0
  Page_faults_major: 0
  Page_faults_minor: 0
              Swaps: 0
    Source_function: check_access
        Source_file: sql_parse.cc
        Source_line: 6051
		

--在mysql5.7之后,profile信息将逐渐被废弃,mysql推荐使用performance schema
--setup_actors用于记录哪些信息会被记录
As of MySQL 5.7.8, the setup_actors table can be used to limit the collection of historical events by host, user, or account to reduce runtime overhead and the amount of data collected in history tables
mysql> SELECT * FROM performance_schema.setup_actors; 
+------+------+------+---------+---------+
| HOST | USER | ROLE | ENABLED | HISTORY |
+------+------+------+---------+---------+
| %    | %    | %    | YES     | YES     |
+------+------+------+---------+---------+		

--当然也可以修改它
UPDATE performance_schema.setup_actors SET ENABLED = 'NO', HISTORY = 'NO' WHERE HOST = '%' AND USER = '%';
INSERT INTO performance_schema.setup_actors (HOST,USER,ROLE,ENABLED,HISTORY) VALUES('localhost','test_user','%','YES','YES');

--其中还有一些初始化的表,具体信息请参阅mysql官方文档
mysql> SELECT table_schema,table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME LIKE 'setup%' and table_schema='performance_schema';  
+--------------------+-------------------+
| table_schema       | table_name        |
+--------------------+-------------------+
| performance_schema | setup_actors      |
| performance_schema | setup_consumers   |
| performance_schema | setup_instruments |
| performance_schema | setup_objects     |
| performance_schema | setup_timers      |
+--------------------+-------------------+
5 rows in set (0.00 sec)

--开启以下性能记录
 UPDATE performance_schema.setup_instruments SET ENABLED = 'YES', TIMED = 'YES' WHERE NAME LIKE '%statement/%';
 UPDATE performance_schema.setup_instruments SET ENABLED = 'YES', TIMED = 'YES' WHERE NAME LIKE '%stage/%';
 UPDATE performance_schema.setup_consumers SET ENABLED = 'YES' WHERE NAME LIKE '%events_statements_%';
 UPDATE performance_schema.setup_consumers SET ENABLED = 'YES' WHERE NAME LIKE '%events_stages_%';

--查看指定sql的event_id
mysql> SELECT EVENT_ID, TRUNCATE(TIMER_WAIT/1000000000000,6) as Duration, SQL_TEXT FROM performance_schema.events_statements_history_long WHERE SQL_TEXT like '%select count(*) from t1%';
+----------+----------+-------------------------+
| EVENT_ID | Duration | SQL_TEXT                |
+----------+----------+-------------------------+
|       78 | 0.022121 | select count(*) from t1 |
+----------+----------+-------------------------+

--查看sql消耗如下:
mysql> SELECT event_name AS Stage, TRUNCATE(TIMER_WAIT/1000000000000,6) AS Duration FROM performance_schema.events_stages_history_long WHERE NESTING_EVENT_ID=78; 
+--------------------------------+----------+
| Stage                          | Duration |
+--------------------------------+----------+
| stage/sql/starting             | 0.000093 |
| stage/sql/checking permissions | 0.000007 |
| stage/sql/Opening tables       | 0.000024 |
| stage/sql/init                 | 0.000018 |
| stage/sql/System lock          | 0.000013 |
| stage/sql/optimizing           | 0.021755 |
| stage/sql/executing            | 0.000015 |
| stage/sql/end                  | 0.000004 |
| stage/sql/query end            | 0.000016 |
| stage/sql/closing tables       | 0.000015 |
| stage/sql/freeing items        | 0.000153 |
| stage/sql/cleaning up          | 0.000001 |
+--------------------------------+----------+

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
6月前
|
存储 数据挖掘 关系型数据库
数仓学习---6、数据仓库概述、 数据仓库建模概述、维度建模理论之事实表、维度建模理论之维度表
数仓学习---6、数据仓库概述、 数据仓库建模概述、维度建模理论之事实表、维度建模理论之维度表
|
4月前
|
存储 大数据 数据管理
数据仓库(08)数仓事实表和维度表技术
所谓的事实表和维度表技术,指的就是如何和构造一张事实表和维度表,是的事实表和维度表,可以涵盖现在目前的需要和方便后续下游数据应用的开发
50 1
|
关系型数据库 MySQL 数据库
MySQL数据库(14):表关系
MySQL数据库(14):表关系
|
存储 JSON 算法
AnalyticDB MySQL-表和索引与MySQL的差异
AnalyticDB MySQL在语法上兼容MySQL,但是它的技术架构不同于MySQL,表和索引也和MySQL差异较大,这篇文章列出了这些差异,在使用AnalyticDB MySQL创建表时可以参考一下。
374 0
|
SQL 存储 分布式计算
对 Hive 数仓表进行高效小文件合并 | 学习笔记
快速学习对 Hive 数仓表进行高效小文件合并。
188 0
|
SQL 存储 分布式计算
数据湖实操讲解【 JindoTable 计算加速】第二十二讲:对 Hive 数仓表进行高效小文件合并
数据湖 JindoFS+OSS 实操干货 36讲 每周二16点准时直播! 扫文章底部二维码入钉群,线上准时观看~ Github链接: https://github.com/aliyun/alibabacloud-jindofs
数据湖实操讲解【 JindoTable 计算加速】第二十二讲:对 Hive 数仓表进行高效小文件合并
|
关系型数据库 MySQL 索引