Tutorial on how to use profile in MySQL

Tutorial on how to use profile in MySQL

What is a profile?

We can use it when we want to analyze the performance of a certain SQL.

Profiling is only available after MySQL 5.0.3.

After starting the profile, all queries including incorrect statements will be recorded.

Close the session or set profiling=0 to close it. (If you set the profiling_history_size parameter to 0, this also has the effect of turning off MySQL profiling.)

This tool can be used to query the SQL execution status, how much time is spent on System lock and Table lock, etc.

It is very important to locate the I/O consumption and CPU consumption of a statement. (The two largest resources consumed by SQL statement execution are IO and CPU)

--After MySQL 5.7, profile information will be gradually abandoned. MySQL recommends using performance schema

MySQL official website definition

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

Simply put, the current session resource consumption.

Note: show profile and show Profiles are not recommended and may be deleted in later versions of MySQL; the official website recommends using Performance Schema

How to use

The profile is disabled by default and is recommended for production environments.

View the profile settings of the current environment

mysql> show variables like '%profiling%';
+------------------------+-------+
| Variable_name | Value |
+------------------------+-------+
| have_profiling | YES |
| profiling | OFF |
| profiling_history_size | 15 |
+------------------------+-------+

profiling off means profiling is turned off, and profiling_history_size 15 means saving the resource consumption of the most recent 15 SQL statements.

To enable the profile function, you can use the command

set global profiling = 1;

Then you can use the following command

show profiles;

View the latest 15 SQL statements;

If you want to view the specific situation of a certain item, the SQL format is:

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 official website explains each field in type as follows:

ALL displays all information

BLOCK IO displays counts for block input and output operations

CONTEXT SWITCHES displays counts for voluntary and involuntary context switches

CPU displays user and system CPU usage times

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 is valid for each session. When the session ends, the current profiling information will be lost.

Use Cases

mysql> show profiles;
+----------+------------+----------------------------+
| Query_ID | Duration | Query |
+----------+------------+----------------------------+
| 1 | 0.00060275 | select * from customers |
| 2 | 0.00222450 | show tables |
| 3 | 0.00567425 | select * from offices |
| 4 | 0.00052050 | show tables |
| 5 | 0.01123300 | select * from payments |
| 6 | 0.00111675 | show tables |
| 7 | 0.02049625 | select * from productlines |
+----------+------------+----------------------------+

When troubleshooting SQL execution, or which SQL is executing very slowly and where it is slow, profiles are very useful tools.

Shows where a SQL statement is spent

mysql> show profile for query 7;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000043 |
| checking permissions | 0.000005 |
| Opening tables | 0.014552 |
| init | 0.000025 |
| System lock | 0.000009 |
| optimizing | 0.000004 |
| statistics | 0.000011 |
| preparing | 0.000010 |
| executing | 0.000003 |
| Sending data | 0.005653 |
| end | 0.000010 |
| query end | 0.000009 |
| closing tables | 0.000020 |
| freeing items | 0.000121 |
| cleaning up | 0.000023 |
+----------------------+----------+

The information is clear at a glance, so I can get a general understanding of the SQL execution.

Summarize

The above is the full content of this article. I hope that the content of this article will have certain reference learning value for your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support for 123WORDPRESS.COM.

You may also be interested in:
  • MySQL performance analysis tool profile usage tutorial
  • How to use SHOW PROFILE command to analyze performance in MySQL
  • Tutorial on using profile query performance in MySQL
  • Detailed explanation of MySQL using profile to analyze slow SQL (group left join is more efficient than subquery)

<<:  Vue Element UI custom description list component

>>:  How to set up ssh password-free login to Linux server

Recommend

18 Web Usability Principles You Need to Know

You can have the best visual design skills in the...

JavaScript implements select all and unselect all operations

This article shares the specific code for JavaScr...

Summary of problems encountered when installing docker on win10 home version

Docker download address: http://get.daocloud.io/#...

Linux configuration SSH password-free login "ssh-keygen" basic usage

Table of contents 1 What is SSH 2 Configure SSH p...

7 interview questions about JS this, how many can you answer correctly

Preface In JavaScript, this is the function calli...

Let you understand the working principle of JavaScript

Table of contents Browser kernel JavaScript Engin...

Introduction to the steps of deploying redis in docker container

Table of contents 1 redis configuration file 2 Do...

Detailed explanation of MySQL remote connection permission

1. Log in to MySQL database mysql -u root -p View...

About the problem of no virtual network card after VMware installation

1 Problem description: 1.1 When VMware is install...

Basic HTML directory problem (difference between relative path and absolute path)

Relative path - a directory path established based...

MySQL full backup and quick recovery methods

A simple MySQL full backup script that backs up t...

How to hide the border/separation line between cells in a table

Only show the top border <table frame=above>...

HTML input file control limits the type of uploaded files

Add an input file HTML control to the web page: &...

Vue3 navigation bar component encapsulation implementation method

Encapsulate a navigation bar component in Vue3, a...