使用SQL Profiler 性能分析器
2011-10-14 11:40
337 查看
mysql 的 sql 性能分析器主要用途是显示 sql 执行的整个过程中各项资源的使用情况。分析器可以更好的展示出不良 SQL 的性能问题所在。
下面我们举例介绍一下 MySQL SQL Profiler 的使用方法:
首先,选择库
mysql> use test;
然后查看 MySQL SQL Profiler 是否开启【0表示off】
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 0 |
+-------------+
开启profile功能
mysql> set profiling=1;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
下面通过对比执行两条相同命令他们的执行过程的差异
创建表zhaoyj
插入三条记录
执行下列查询语句(两次)
mysql> select count(*) from zhaoyj2;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
mysql> select count(*) from zhaoyj2;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
下面看一下profiles的记录
mysql> show profiles;
+----------+------------+-------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------------+
| 1 | 0.00040100 | select count(*) from zhaoyj2 |
| 2 | 0.00007500 | select count(*) from zhaoyj2 |
+----------+------------+-------------------------------------------+
看一下具体的执行过程
mysql> show profile for query 1;
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| starting | 0.000021 |
| Waiting for query cache lock | 0.000005 |
| checking query cache for query | 0.000039 |
| checking permissions | 0.000009 |
| Opening tables | 0.000021 |
| System lock | 0.000010 |
| Waiting for query cache lock | 0.000037 |
| init | 0.000013 |
| optimizing | 0.000006 |
| statistics | 0.000011 |
| preparing | 0.000010 |
| executing | 0.000007 |
| Sending data | 0.000048 |
| end | 0.000006 |
| query end | 0.000006 |
| closing tables | 0.000009 |
| freeing items | 0.000007 |
| Waiting for query cache lock | 0.000004 |
| freeing items | 0.000112 |
| Waiting for query cache lock | 0.000003 |
| freeing items | 0.000004 |
| storing result in query cache | 0.000005 |
| logging slow query | 0.000003 |
| cleaning up | 0.000005 |
+--------------------------------+----------+
24 rows in set (0.00 sec)
mysql> show profile for query 2;
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| starting | 0.000024 |
| Waiting for query cache lock | 0.000005 |
| checking query cache for query | 0.000009 |
| checking privileges on cached | 0.000006 |
| checking permissions | 0.000009 |
| sending cached result to clien | 0.000013 |
| logging slow query | 0.000005 |
| cleaning up | 0.000004 |
+--------------------------------+----------+
8 rows in set (0.00 sec)
从上面的例子中我们可以清晰的看出 2 次执行 count 语句的差别, SHOW PROFILE FOR QUERY 1 展现的是第一次 count 统计的执行过程,包含了 Opening tables 、 Table lock 等操作 。而 SHOW PROFILE FOR QUERY 2 展示了第二次 count 统计的执行过程 , 第二次 count 直接从查询缓存中返回 count 统计结果,通过对比 2 次统计的总执行时间发现,缓存读的速度接近物理读的 10 倍。通过使用 SQL 性能分析器可以帮助我们对一些比较难以确定性能问题的
SQL 进行诊断,找出问题根源。
下面我们举例介绍一下 MySQL SQL Profiler 的使用方法:
首先,选择库
mysql> use test;
然后查看 MySQL SQL Profiler 是否开启【0表示off】
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 0 |
+-------------+
开启profile功能
mysql> set profiling=1;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 1 |
+-------------+
下面通过对比执行两条相同命令他们的执行过程的差异
创建表zhaoyj
插入三条记录
执行下列查询语句(两次)
mysql> select count(*) from zhaoyj2;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
mysql> select count(*) from zhaoyj2;
+----------+
| count(*) |
+----------+
| 3 |
+----------+
下面看一下profiles的记录
mysql> show profiles;
+----------+------------+-------------------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-------------------------------------------+
| 1 | 0.00040100 | select count(*) from zhaoyj2 |
| 2 | 0.00007500 | select count(*) from zhaoyj2 |
+----------+------------+-------------------------------------------+
看一下具体的执行过程
mysql> show profile for query 1;
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| starting | 0.000021 |
| Waiting for query cache lock | 0.000005 |
| checking query cache for query | 0.000039 |
| checking permissions | 0.000009 |
| Opening tables | 0.000021 |
| System lock | 0.000010 |
| Waiting for query cache lock | 0.000037 |
| init | 0.000013 |
| optimizing | 0.000006 |
| statistics | 0.000011 |
| preparing | 0.000010 |
| executing | 0.000007 |
| Sending data | 0.000048 |
| end | 0.000006 |
| query end | 0.000006 |
| closing tables | 0.000009 |
| freeing items | 0.000007 |
| Waiting for query cache lock | 0.000004 |
| freeing items | 0.000112 |
| Waiting for query cache lock | 0.000003 |
| freeing items | 0.000004 |
| storing result in query cache | 0.000005 |
| logging slow query | 0.000003 |
| cleaning up | 0.000005 |
+--------------------------------+----------+
24 rows in set (0.00 sec)
mysql> show profile for query 2;
+--------------------------------+----------+
| Status | Duration |
+--------------------------------+----------+
| starting | 0.000024 |
| Waiting for query cache lock | 0.000005 |
| checking query cache for query | 0.000009 |
| checking privileges on cached | 0.000006 |
| checking permissions | 0.000009 |
| sending cached result to clien | 0.000013 |
| logging slow query | 0.000005 |
| cleaning up | 0.000004 |
+--------------------------------+----------+
8 rows in set (0.00 sec)
从上面的例子中我们可以清晰的看出 2 次执行 count 语句的差别, SHOW PROFILE FOR QUERY 1 展现的是第一次 count 统计的执行过程,包含了 Opening tables 、 Table lock 等操作 。而 SHOW PROFILE FOR QUERY 2 展示了第二次 count 统计的执行过程 , 第二次 count 直接从查询缓存中返回 count 统计结果,通过对比 2 次统计的总执行时间发现,缓存读的速度接近物理读的 10 倍。通过使用 SQL 性能分析器可以帮助我们对一些比较难以确定性能问题的
SQL 进行诊断,找出问题根源。
相关文章推荐
- 如何使用SQL Profiler 性能分析器
- [MySQL优化] -- 如何使用SQL Profiler 性能分析器
- 如何使用SQL Profiler 性能分析器
- 如何使用SQL Profiler 性能分析器
- [MySQL优化] -- 如何使用SQL Profiler 性能分析器
- 如何使用SQL Profiler 性能分析器
- mysql使用Profiling分析query语句,sql语句性能优化
- 使用SQL Profiler trace(2005)的经验分享(新建跟踪、分析跟踪文件)
- 数据库性能优化-1-使用SQL Server Profiler工具和执行计划分析
- Sql2005性能工具(SQL Server Profiler和数据库引擎优化顾问)使用方法详解
- .NET性能分析利器 : CLR Profiler的使用
- mysql show profiles 使用分析sql 性能
- 用dotTace模仿下老赵的“使用Profiler分析程序性能”
- MySQL的SQL Profiler性能分析器
- 使用ORM Profiler分析数据访问性能
- MySQL中使用explain和profile分析SQL执行计划和性能
- .NET批量大数据插入性能分析及比较(5.使用SqlBulkCopy)
- MySQL的SQL Profiler性能分析器
- 使用Trepn Profiler工具分析Dragonboard410c的功耗和性能(二)
- 实例分析ASP.NET在MVC5中使用MiniProfiler监控MVC性能的方法