`

MySQL Query Profiler 介绍

 
阅读更多

MySQL Query Profiler

查看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;

分享到:
评论

相关推荐

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

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

    mysql profiling

    MySQL的Query Profiler是一种强大的工具,用于诊断和优化SQL查询的性能。在MySQL 5.0及更高版本中,这个功能使得用户能够深入了解查询的执行过程,找出可能的瓶颈,从而提升数据库的运行效率。 首先,为了启用Query...

    Devart dbForge Studio for MySQL Professional Edition v7.1.13

    Improve multi-second queries using Query Profiler. The tool helps you detect problems and optimize SQL queries via GUI. Profiler: Offers visual query profiling Compares profiling results More on ...

    MySQL使用profile查询性能的操作教程

    MySQL的Profiler功能是一种用于分析SQL语句执行性能的工具,它可以帮助数据库管理员和开发者了解查询在执行过程中的各个阶段所消耗的时间,从而优化查询效率。Profiler功能自MySQL 5.0.37版本开始引入,提供了对SQL...

    Mysql DBA试题

    常用的MySQL优化工具包括MySQL Query Profiler(查询分析器)、EXPLAIN(查询执行计划)、pt-query-digest(性能评估)、Percona Toolkit等。监控工具有MySQL Enterprise Monitor、MySQL Workbench、SHOW STATUS...

    mysql 性能优化 How to Analyze and Tune SQL Queries for Better Perfor

    * MySQL Profiler:用于监控查询执行情况 * MySQL tuning adviser:用于优化查询执行计划 Influencing the Optimizer 影响优化器的决策需要了解 MySQL 的优化器工作原理和 Cost-Based Optimizer 的机制。在本资源...

    mysql-installer-community-5.7.36.1

    - **查询优化器改进**:引入了Query Profiler,帮助用户分析和优化查询性能,同时增强了Cost-Based Optimizer(CBO)。 - **JSON支持**:添加了对JSON数据类型的原生支持,使得MySQL能够处理非结构化数据。 - **...

    mysql 5.7 Percona Server

    - **Percona Query Profiler**:更细致的查询分析,帮助找出性能瓶颈并优化SQL语句。 3. **数据安全性与恢复**: - **增强的备份工具**:Percona XtraBackup 提供无锁热备份,可以在不中断服务的情况下备份大规模...

    mysql-5.6.31.tar.gz

    7. 查询分析:新的查询分析工具(Query Profiler)帮助开发者识别性能瓶颈,优化查询性能。同时,性能schema表的增强,让监控数据库状态变得更加直观。 8. 并行复制:MySQL 5.6.31引入了并行复制,允许备库并行处理...

    mysql5.6.64.tar.gz

    Performance Schema提供了一个监控和分析MySQL服务器性能的框架,而Query Profiler可以帮助识别慢查询并提出改进建议。 总的来说,MySQL 5.6.64是一个强大且稳定的数据库版本,其RPM包形式使得在Linux系统上的安装...

    mysql 精简版5.1 5.5 5.7 mariaadb 合集

    新的性能分析工具如Performance Schema和Query Profiler也使得数据库调优变得更加便捷。 **MariaDB** 是由MySQL创始人开发的一个分支,它保持与MySQL的兼容性,同时提供了更多的创新特性。MariaDB在存储引擎方面...

    MySQL缓存研究

    - 使用性能分析工具,如MySQL Profiler,进行深入的性能分析。 4. MySQL缓存研究报告 在研究报告中,会详细探讨不同缓存组件的运作机制,如查询缓存的缓存命中率、InnoDB缓冲池的脏页比例等,并结合实际案例分析...

    MySql数据库.rar

    MySQL Performance Schema和Query Profiler可以帮助分析和改进性能。 总结,MySQL数据库是一个强大且灵活的数据库系统,涵盖了从基础的数据存储到复杂的企业级应用的各种需求。理解并熟练掌握上述知识点,将有助于...

    MySQL数据库后端语句执行监测工具

    MySQL的Profiler功能可以记录每个查询的执行细节,包括总执行时间、获取结果集的时间、发送数据的时间等,有助于识别性能问题。 7. **监控指标**: 关注的关键监控指标包括QPS(Queries Per Second)、TPS...

    MYSQL面试题.zip

    - Query Profiler:分析查询性能。 10. **MySQL最新发展** - MySQL 8.0的新特性:窗口函数、JSON增强、新的存储引擎。 - MySQL与NoSQL的比较:适用场景、优缺点。 掌握以上知识点,将有助于你在MySQL相关的面试...

    MySQL OCP 实战视频 16-19

    6. **故障排查**:通过日志分析(如错误日志、慢查询日志)找出系统瓶颈,学习使用Performance Schema和Profiler进行性能监控。 7. **高可用性与容错**:探讨高可用性解决方案,如集群、Group Replication、Percona...

    mysql-5.5.40-win64.zip

    9. **监控与调优**:MySQL 5.5提供了性能监视器(Performance Schema)和查询分析器(Query Profiler),帮助管理员监控数据库性能,定位和优化慢查询。 10. **社区支持**:作为开源软件,MySQL 5.5拥有庞大的...

    MySQL Performance Tuning Active Guide MySQL Performance Tuning

    7. **性能分析工具**:如使用MySQL Profiler分析查询性能,或者使用第三方工具如Percona Toolkit和pt-query-digest进行更深入的分析。 8. **服务器参数调优**:根据系统资源调整如max_connections,thread_cache_...

Global site tag (gtag.js) - Google Analytics