美文网首页
MySQL 解析单条查询

MySQL 解析单条查询

作者: Huang98 | 来源:发表于2020-11-23 19:40 被阅读0次

读《高性能MySQL》第三版,笔记。
官方文档:https://dev.mysql.com/doc/refman/5.7/en/show-profile.html

使用 SHOW PROFILE

 set profiling = 1;
 
 show profiles;
 
 show profile for query 1;

使用 help 命令查看说明:

mysql> help show profile
Name: 'SHOW PROFILE'
Description:
Syntax:
SHOW PROFILE [type [, type] ... ]
    [FOR QUERY n]
    [LIMIT row_count [OFFSET offset]]

type:
    ALL
  | BLOCK IO
  | CONTEXT SWITCHES
  | CPU
  | IPC
  | MEMORY
  | PAGE FAULTS
  | SOURCE
  | SWAPS

The SHOW PROFILE and SHOW PROFILES statements display profiling
information that indicates resource usage for statements executed
during the course of the current session.

*Note*:

These statements are deprecated and will be removed in a future MySQL
release. Use the Performance Schema instead; see
http://dev.mysql.com/doc/refman/5.7/en/performance-schema-query-profili
ng.html.

Profiling is controlled by the profiling session variable, which has a
default value of 0 (OFF). Profiling is enabled by setting profiling to
1 or ON:

mysql> SET profiling = 1;

SHOW PROFILES displays a list of the most recent statements sent to the
server. The size of the list is controlled by the
profiling_history_size session variable, which has a default value of
15. The maximum value is 100. Setting the value to 0 has the practical
effect of disabling profiling.

All statements are profiled except SHOW PROFILE and SHOW PROFILES, so
you will find neither of those statements in the profile list.
Malformed statements are profiled. For example, SHOW PROFILING is an
illegal statement, and a syntax error occurs if you try to execute it,
but it will show up in the profiling list.

SHOW PROFILE displays detailed information about a single statement.
Without the FOR QUERY n clause, the output pertains to the most
recently executed statement. If FOR QUERY n is included, SHOW PROFILE
displays information for statement n. The values of n correspond to the
Query_ID values displayed by SHOW PROFILES.

The LIMIT row_count clause may be given to limit the output to
row_count rows. If LIMIT is given, OFFSET offset may be added to begin
the output offset rows into the full set of rows.

By default, SHOW PROFILE displays Status and Duration columns. The
Status values are like the State values displayed by SHOW PROCESSLIST,
although there might be some minor differences in interpretion for the
two statements for some status values (see
http://dev.mysql.com/doc/refman/5.7/en/thread-information.html).

Optional type values may be specified to display specific additional
types of information:

o ALL displays all information

o BLOCK IO displays counts for block input and output operations

o CONTEXT SWITCHES displays counts for voluntary and involuntary
  context switches

o CPU displays user and system CPU usage times

o IPC displays counts for messages sent and received

o MEMORY is not currently implemented

o PAGE FAULTS displays counts for major and minor page faults

o 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

o SWAPS displays swap counts

Profiling is enabled per session. When a session ends, its profiling
information is lost.

URL: http://dev.mysql.com/doc/refman/5.7/en/show-profile.html

Examples:
mysql> SELECT @@profiling;
+-------------+
| @@profiling |
+-------------+
|           0 |
+-------------+
1 row in set (0.00 sec)

mysql> SET profiling = 1;
Query OK, 0 rows affected (0.00 sec)

mysql> DROP TABLE IF EXISTS t1;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CREATE TABLE T1 (id INT);
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW PROFILES;
+----------+----------+--------------------------+
| Query_ID | Duration | Query                    |
+----------+----------+--------------------------+
|        0 | 0.000088 | SET PROFILING = 1        |
|        1 | 0.000136 | DROP TABLE IF EXISTS t1  |
|        2 | 0.011947 | CREATE TABLE t1 (id INT) |
+----------+----------+--------------------------+
3 rows in set (0.00 sec)

mysql> SHOW PROFILE;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| checking permissions | 0.000040 |
| creating table       | 0.000056 |
| After create         | 0.011363 |
| query end            | 0.000375 |
| freeing items        | 0.000089 |
| logging slow query   | 0.000019 |
| cleaning up          | 0.000005 |
+----------------------+----------+
7 rows in set (0.00 sec)

mysql> SHOW PROFILE FOR QUERY 1;
+--------------------+----------+
| Status             | Duration |
+--------------------+----------+
| query end          | 0.000107 |
| freeing items      | 0.000008 |
| logging slow query | 0.000015 |
| cleaning up        | 0.000006 |
+--------------------+----------+
4 rows in set (0.00 sec)

mysql> SHOW PROFILE CPU FOR QUERY 2;
+----------------------+----------+----------+------------+
| Status               | Duration | CPU_user | CPU_system |
+----------------------+----------+----------+------------+
| checking permissions | 0.000040 | 0.000038 |   0.000002 |
| creating table       | 0.000056 | 0.000028 |   0.000028 |
| After create         | 0.011363 | 0.000217 |   0.001571 |
| query end            | 0.000375 | 0.000013 |   0.000028 |
| freeing items        | 0.000089 | 0.000010 |   0.000014 |
| logging slow query   | 0.000019 | 0.000009 |   0.000010 |
| cleaning up          | 0.000005 | 0.000003 |   0.000002 |
+----------------------+----------+----------+------------+
7 rows in set (0.00 sec)

栗子

使用 world 数据库 为例:

mysql> set profiling = 1;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> select max(id) from city;
+---------+
| max(id) |
+---------+
|    4079 |
+---------+
1 row in set (0.00 sec)
mysql>  show profiles;
+----------+------------+--------------------------+
| Query_ID | Duration   | Query                    |
+----------+------------+--------------------------+
|        1 | 0.00019075 | select max(id) from city |
+----------+------------+--------------------------+
1 row in set, 1 warning (0.00 sec)
mysql> show profile for query 1;
+----------------------+----------+
| Status               | Duration |
+----------------------+----------+
| starting             | 0.000054 |
| checking permissions | 0.000005 |
| Opening tables       | 0.000015 |
| init                 | 0.000019 |
| System lock          | 0.000009 |
| optimizing           | 0.000029 |
| executing            | 0.000008 |
| end                  | 0.000002 |
| query end            | 0.000006 |
| closing tables       | 0.000012 |
| freeing items        | 0.000026 |
| cleaning up          | 0.000007 |
+----------------------+----------+
12 rows in set, 1 warning (0.01 sec)

相关文章

  • MySQL 解析单条查询

    读《高性能MySQL》第三版,笔记。官方文档:https://dev.mysql.com/doc/refman/5...

  • 【MySQL必知必会】第17章:组合查询

    多数SQL查询都只使用单条SELECT语句。但MySQL也允许执行多个查询(多条SELECT语句),并将结果作为单...

  • mysql查询缓存

    查询缓存工作原理 mysql手册地址一条查询语句在被解析之前,就会和查询缓存进行对比,查询的语句,必须完全相同,才...

  • 高性能MySQL之架构与历史

    MySQL逻辑架构 优化与执行 MySQL会解析查询,创建内部数据结构(解析树),对齐进行优化(重写查询、决定表的...

  • 高性能MySQL之架构与历史

    MySQL逻辑架构 优化与执行 MySQL会解析查询,创建内部数据结构(解析树),对齐进行优化(重写查询、决定表的...

  • MySQL 查询缓存

    开启查询缓存后,查询语句的解析过程: 在解析一个查询语句之前,如果查询缓存是打开的,那么MySQL会优先检查这个查...

  • MySQL学习笔记二之单表查询与多表查询

    title: MySQL学习笔记二之单表查询与多表查询tags: MySQL 数据库categories: MyS...

  • Python+MySQL数据库操作(PyMySQL)

    安装mysql驱动 连接数据库 建表 插入 查询 Python查询Mysql使用 fetchone() 方法获取单...

  • mysql-查询优化处理

    下面是mysql查询的路径 下面简单的理解一下mysql服务器的查询过程 查询缓存 在解析一个查询语句之前,如果查...

  • Mysql对于information_schema库的一些用法

    1) 查询mysql每一个数据库的大小 2) 查询mysql单个库中每张表的的大小 3) 查询mysql单个库中单...

网友评论

      本文标题:MySQL 解析单条查询

      本文链接:https://www.haomeiwen.com/subject/btwviktx.html