mysql查询语句优化工具

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



把这个profiling功能打开,可以查看sql查询语句的整个过程中各种资源的消耗情况。

mysql> show profiles;
+----------+------------+---------------------------------+
| Query_ID | Duration   | Query                           |
+----------+------------+---------------------------------+
|        1 | 0.00049600 | show variables like "profiling" |
|        2 | 0.02267400 | show databases                  |
|        3 | 0.04235200 | SELECT DATABASE()               |
|        4 | 0.00012825 | SELECT DATABASE()               |
|        5 | 0.00025125 | show databases                  |
|        6 | 0.00145125 | show tables                     |
|        7 | 0.04207925 | show tables                     |
|        8 | 0.03485225 | select count(*) from host_item  |
+----------+------------+---------------------------------+


mysql> show profile cpu, block io  for query 8;
+----------------------+----------+----------+------------+--------------+---------------+
| Status               | Duration | CPU_user | CPU_system | Block_ops_in | Block_ops_out |
+----------------------+----------+----------+------------+--------------+---------------+
| starting             | 0.000634 | 0.000000 |   0.000000 |            0 |             0 |
| checking permissions | 0.000013 | 0.000000 |   0.000000 |            0 |             0 |
| Opening tables       | 0.000051 | 0.000000 |   0.000000 |            0 |             0 |
| System lock          | 0.000014 | 0.000000 |   0.000000 |            0 |             0 |
| init                 | 0.000015 | 0.000000 |   0.000000 |            0 |             0 |
| optimizing           | 0.033657 | 0.000000 |   0.000000 |           40 |             0 |
| statistics           | 0.000029 | 0.000000 |   0.000000 |            0 |             0 |
| preparing            | 0.000012 | 0.000000 |   0.000000 |            0 |             0 |
| executing            | 0.000006 | 0.000000 |   0.000000 |            0 |             0 |
| Sending data         | 0.000313 | 0.000000 |   0.000000 |            0 |             0 |
| end                  | 0.000008 | 0.000000 |   0.000000 |            0 |             0 |
| query end            | 0.000008 | 0.000000 |   0.000000 |            0 |             0 |
| closing tables       | 0.000047 | 0.000000 |   0.000000 |            0 |             0 |
| freeing items        | 0.000042 | 0.000000 |   0.000000 |            0 |             0 |
| logging slow query   | 0.000003 | 0.000000 |   0.000000 |            0 |             0 |
| cleaning up          | 0.000003 | 0.000000 |   0.000000 |            0 |             0 |
+----------------------+----------+----------+------------+--------------+---------------


show profile后面可以跟参数:all或者cpu,block io,page faults等。


也可以在information_schema.profiling数据库表中直接查询:

select state,sum(duration) as total_r,round(100*sum(duration)/(select sum(duration) from information_schema.profiling where query_id=@query_id),2) as pct_r,count(*) as calls,sum(duration)/count(*) as "r/call" from information_schema.profiling where query_id=@query_id group by state order by total_r desc;


最后,关闭set profiling=0该功能。





















本文转自chenzudao51CTO博客,原文链接: http://blog.51cto.com/victor2016/1875012,如需转载请自行联系原作者






相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
10天前
|
关系型数据库 MySQL 索引
mysql 分析5语句的优化--索引添加删除
mysql 分析5语句的优化--索引添加删除
11 0
|
16天前
|
存储 关系型数据库 MySQL
轻松入门MySQL:数据库设计之范式规范,优化企业管理系统效率(21)
轻松入门MySQL:数据库设计之范式规范,优化企业管理系统效率(21)
|
16天前
|
存储 SQL 关系型数据库
轻松入门MySQL:加速进销存!利用MySQL存储过程轻松优化每日销售统计(15)
轻松入门MySQL:加速进销存!利用MySQL存储过程轻松优化每日销售统计(15)
|
10天前
|
SQL 缓存 关系型数据库
mysql性能优化-慢查询分析、优化索引和配置
mysql性能优化-慢查询分析、优化索引和配置
76 0
|
2天前
|
关系型数据库 MySQL Shell
MySQL 查询
MySQL 查询
|
4天前
|
SQL 关系型数据库 MySQL
DQL语言之基础查询(mysql)
DQL语言之基础查询(mysql)
|
4天前
|
SQL 关系型数据库 MySQL
DQL语言之连接查询(mysql)
DQL语言之连接查询(mysql)
|
4天前
|
关系型数据库 MySQL
MySQL全局库表查询准确定位字段
information_schema.COLUMNS 详细信息查询
183 4
|
11天前
|
SQL 关系型数据库 MySQL
mysql多表查询、函数查询
mysql多表查询、函数查询
|
11天前
|
SQL 关系型数据库 MySQL
mysql基本查询、运算符、排序和分页
mysql基本查询、运算符、排序和分页