MySQL 的SQL性能分析器主要用途是显示SQL执行的整个过程中各项资源的使用情况。分析器可以更好的展示
出不良SQL的性能问题所在。

mysql sql profile的使用方法

1.开启mysql sql profile

检查mysql sql profile是否启用
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 | 
+-------------+
1 row in set (0.01 sec)

默认情况下 profiling 的值为0表示 MySQL SQL Profiler处于OFF状态,如果开启SQL性能分析器后, profiling 的值将为1.

mysql> set profiling=1;
Query OK, 0 rows affected (0.03 sec)

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           1 | 
+-------------+
1 row in set (0.01 sec)

上面可以看到profiling已经变为1了,但是这个是session级别的,系统是不支持的。如下测试

退出mysql
mysql> quit
Bye
[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.0.45-log Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

查看profiling的值
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 | 
+-------------+
1 row in set (0.01 sec)

发现已经变为默认值0了,那如果设置系统级会如何呢?

mysql> set global profiling=1;
ERROR 1228 (HY000): Variable 'profiling' is a SESSION variable and can't be used with SET GLOBAL
mysql>

看到这里报错了。所以mysql sql profile是session级别的。

2. 举个例如,看如何使用

mysql> create table t5 as select * from t1;
ERROR 1046 (3D000): No database selected
mysql> use backup;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table t5 as select * from t1;
Query OK, 2 rows affected (0.06 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select count(*) from t5;
+----------+
| count(*) |
+----------+
|        2 | 
+----------+
1 row in set (0.00 sec)

mysql> select count(*) from t5;
+----------+
| count(*) |
+----------+
|        2 | 
+----------+
1 row in set (0.00 sec)

mysql> show profiles;
+----------+------------+-------------------------------------+
| Query_ID | Duration   | Query                               |
+----------+------------+-------------------------------------+
|        1 | 0.00382400 | select @@profiling                  | 
|        2 | 0.00268500 | create table t5 as select * from t1 | 
|        3 | 0.00017200 | SELECT DATABASE()                   | 
|        4 | 0.01985400 | show databases                      | 
|        5 | 0.00018900 | show tables                         | 
|        6 | 0.06225200 | create table t5 as select * from t1 | 
|        7 | 0.00368800 | select count(*) from t5             | 
|        8 | 0.00322200 | select count(*) from t5             | 
+----------+------------+-------------------------------------+
8 rows in set (0.01 sec)

mysql> 
mysql> show profile for query 7;
+--------------------+----------+
| Status             | Duration |
+--------------------+----------+
| (initialization)   | 0.000414 | 
| Opening tables     | 0.000599 | 
| System lock        | 0.000254 | 
| Table lock         | 0.000175 | 
| init               | 0.000052 | 
| optimizing         | 0.00001  | 
| executing          | 0.002107 | 
| end                | 0.000042 | 
| query end          | 0.000005 | 
| freeing items      | 0.000014 | 
| closing tables     | 0.000011 | 
| logging slow query | 0.000005 | 
+--------------------+----------+
12 rows in set (0.03 sec)

mysql> show profile for query 8;
+--------------------+----------+
| Status             | Duration |
+--------------------+----------+
| (initialization)   | 0.000064 | 
| Opening tables     | 0.000018 | 
| System lock        | 0.00001  | 
| Table lock         | 0.000013 | 
| init               | 0.00002  | 
| optimizing         | 0.00001  | 
| executing          | 0.002589 | 
| end                | 0.000459 | 
| query end          | 0.000007 | 
| freeing items      | 0.000015 | 
| closing tables     | 0.000012 | 
| logging slow query | 0.000005 | 
+--------------------+----------+
12 rows in set (0.00 sec)

mysql> select sum(format(duration,6)) as duration from information_schema.profiling where query_id=7;
+----------+
| duration |
+----------+
| 0.003688 | 
+----------+
1 row in set (0.02 sec)

mysql> select sum(format(duration,6)) as duration from information_schema.profiling where query_id=8;
+----------+
| duration |
+----------+
| 0.003222 | 
+----------+
1 row in set (0.00 sec)

mysql>

从如上的信息可以看出这两个sql的profile统计信息里,前4项差别比较大,这是两个sql主要区别,第二次查询有很多

缓存了了。SQL 性能分析器可以帮助我们对一些比较难以确定性能问题的 SQL 进行诊断,找出问题根源。

------end-----

mysql的sql性能分析器的更多相关文章

  1. Oracle DB SQL 性能分析器

    • 确定使用SQL 性能分析器的优点 • 描述SQL 性能分析器工作流阶段 • 使用SQL 性能分析器确定数据库更改所带来的性能改进 SQL 性能分析器:概览 • 11g 的新增功能 • 目标用户:D ...

  2. Oracle 11g 中SQL性能优化新特性之SQL性能分析器(SQLPA)

    Oracle11g中,真实应用测试选项(the Real Application Testing Option)提供了一个有用的特点,叫SQL性能分析器(SQL Performance Analyze ...

  3. OCM_第十五天课程:Section6 —》数据库性能调优 _SQL 访问建议 /SQL 性能分析器/配置基线模板/SQL 执行计划管理/实例限制

    注:本文为原著(其内容来自 腾科教育培训课堂).阅读本文注意事项如下: 1:所有文章的转载请标注本文出处. 2:本文非本人不得用于商业用途.违者将承当相应法律责任. 3:该系列文章目录列表: 一:&l ...

  4. mysql之sql性能调优

    sql调优大致分为两步:1 如何定位慢查询   2 如何优化sql语句. 一:定位慢查询 -- 显示到mysql数据库的连接数 -- show status like 'connections'; - ...

  5. [MySQL优化] -- 如何使用SQL Profiler 性能分析器

    mysql 的 sql 性能分析器主要用途是显示 sql 执行的整个过程中各项资源的使用情况.分析器可以更好的展示出不良 SQL 的性能问题所在. 下面我们举例介绍一下 MySQL SQL Profi ...

  6. 【转】MySQL批量SQL插入各种性能优化

    原文:http://mp.weixin.qq.com/s?__biz=MzA5MzY4NTQwMA==&mid=403182899&idx=1&sn=74edf28b0bd29 ...

  7. SQL优化 MySQL版 - 索引分类、创建方式、删除索引、查看索引、SQL性能问题

    SQL优化 MySQL版  - 索引分类.创建方式.删除索引.查看索引.SQL性能问题 作者 Stanley 罗昊 [转载请注明出处和署名,谢谢!] 索引分类 单值索引 单的意思就是单列的值,比如说有 ...

  8. 警惕 MySql 更新 sql 的 WHERE 从句中的 IN() 子查询时出现的性能陷阱

    警惕 MySql 更新 sql 的 WHERE 从句中的 IN() 子查询时出现的性能陷阱 以下文章来源:https://blog.csdn.net/defonds/article/details/4 ...

  9. mysql show profiles使用分析sql性能

    mysql show profiles使用分析sql性能 Show profiles是5.0.37之后添加的,要想使用此功能,要确保版本在5.0.37之后. 查看一下我的数据库版本 mysql> ...

随机推荐

  1. DBA 招聘

    数据库管理员(资深) 眼控科技 10-19万 72小时反馈 上海 6小时前 大专及以上 2年以上经验 普通话 25-35岁 绩效奖金 带薪年假 午餐补助 定期体检 年底双薪 五险一金 职位描述: 工作 ...

  2. 常用mysql text 类型,varchar最大长度

    MySQL 3种text类型的最大长度如下: TEXT 65,535 bytes ~64kb MEDIUMTEXT 16,777,215 bytes ~16Mb LONGTEXT 4,294,967, ...

  3. Java连接MySQL数据库及操作

    Java操作MySQL数据库,需要驱动mysql-connector-java 来进行操作,去下载对应的jar包   一.导入需要的jar包 我用的是maven对包进行管理,在maven中添加如下内容 ...

  4. python内置函数之attr【反射】

    #Auther Bob#--*--conding:utf-8 --*-- #我们来循序渐进的学习反射 import s1 #阶段1# def run():# url = input("请输入 ...

  5. 5-Qt读取文件失败原因

    刚开始准备读取当前目录下的txt文件: QFile file(":/xuhao.txt");或者: QFile file("./xuhao.txt"); 但是一 ...

  6. Devexpress ChartControl 柱状图简单例子

    //using DevExpress.XtraEditors; //using DevExpress.XtraCharts; // Create an empty chart. ChartContro ...

  7. sc start service 1063 1053 错误原因

    在进入点函数里面要完成ServiceMain的初始化,准确点说是初始化一个SERVICE_TABLE_ENTRY结构数组,这个结构记录了这个服务程序里面所包含的所有服务的名称和服务的进入点函数,下面是 ...

  8. Laravel 5.x 启动过程分析

     Posted on 2015年9月11日 by  学院君 1.初始化Application 1.1 注册基本绑定 app -> Application实例(Illuminate\Foundat ...

  9. [SoapUI] 在执行某个TestSuite之前先执行login或者其他什么前置步骤

    打开TestSuite有一个地方可以设置Setup Script import com.eviware.soapui.model.support.PropertiesMap log.info &quo ...

  10. 用户体验要好,App动画得这么做

    以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具.   在用户体验设计方面,App动画的设计和添加,带给设计师无限的创造空间的同时,也成为设计师群体最具 ...