当前位置:网站首页>Show profile analysis of SQL statement performance overhead

Show profile analysis of SQL statement performance overhead

2020-11-09 10:51:36 osc_jrhexi1r

Preface

In the project , We use sql sentence , does CRUD, He counted hundreds of database servers ( Nonsense ), Come to the conclusion , Eighty percent of their performance goes to queries , modify , newly added , The deletion of three modules together accounts for 20% .
therefore , We are right. SQL The optimization of the , It's basically reflected in select On .

Step one : Illustrate with examples SQL The concept of time

I'll give a few examples to illustrate , Let's talk about sql Execution time has a simple concept .
scene :
I have two tables now , A watch has 5000 Data , The other one has 50000 Data .
audit_legal :5000 Data .
resource:50000 Data .



Case a : Inquire about 1000 Data , Is the speed of the two watches the same .

 Insert picture description here
 Insert picture description here
One is used 21ms, One is used 243ms, The results of .
Even if you query the same data , If the base number is different , So the query time is different .
Here's why :
Without index , We implement a rule sql sentence , Table is global traversal , Disk addressing , Even if logically adjacent records are on disk, they are not necessarily physically adjacent , The larger the base , The longer the query time .




The request response time of the access interface in the project is usually in 200ms within , higher than 200ms The interface of , Hey , No problem if you don't ask too much .
Be careful. :
Directly in MySQL Visual interface execution sql It takes much longer to access this interface to get a response , Because the bullet's going to fly for a while .

Case 2 : Add index , How many times has the query efficiency been improved .
stay resource This table is tested ,
Two times :
1、 Query through the primary key .


 Insert picture description here
 Insert picture description here

2、 Query through ordinary fields .

 Insert picture description here
With a watch , The same data :
 Insert picture description here
One is almost time-consuming ,0.00025s, The other used 1.378s, So the index is very strong , If someone asks you , Add index , How many times has the query efficiency been improved . Then he must be a hammer , Different bases , The multiple of query efficiency is different .
One more question ,MySQL When the amount of data in a single table of the database is , Efficiency has basically reached its limit .
Ten million pieces of data , The answer is here :
https://blog.csdn.net/numbbe/article/details/109300087





Step two : Open the database analysis function

Before opening , Let's first introduce an English word :profiling
Its Chinese is analysis . We'll use it all the time .
Enter the command line :
SHOW VARIABLES LIKE ‘%pro%’;
 Insert picture description here
We see two parameters :
profiling:
Determine whether the parsing function is on ,mysql Default off/0 close ,on/1 For opening .
After opening , After that SQL,mysql The server will record this sql System overhead of , such as cpu,io, Memory consumption, etc .
profiling_history_size:
Retention analysis profiling Number , The scope is 0-100, by 0 when , Turn off the function .









Step three :MySQL Teach you how to play profiling

I certainly didn't say mysql Teaching major , So let's see MySQL How do you say .
Enter the command line :
HELP PROFILE;
 Insert picture description here


I will mysql Of example, Copy here .
wow , How long , Don't look at , Flip down , I'll translate the urine .

NAME	description	EXAMPLE
SHOW PROFILE	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*:

The SHOW PROFILE AND SHOW PROFILES statements are deprecated AND will
be removed IN a future MySQL release. USE the Performance SCHEMA
instead; see
https://dev.mysql.com/doc/refman/5.7/en/performance-SCHEMA-QUERY-profil
ing.html.

TO control profiling, USE the profiling SESSION variable, which has a
DEFAULT VALUE of 0 (OFF). ENABLE profiling 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
https://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: https://dev.mysql.com/doc/refman/5.7/en/SHOW-profile.html

	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)

 Here's how to use profile command , Its grammar is introduced , Its parameters are introduced .

SHOW PROFILE	Syntax:

 grammar 
SHOW PROFILE [TYPE [, TYPE] ... ]	type It's the following type
    [FOR QUERY n]					 there n yes sql Statement code .
    [LIMIT ROW_COUNT [OFFSET OFFSET]]

 Parameters 
TYPE: {
    ALL					 Show all the overhead information .
  | BLOCK IO			IO expenses .
  | CONTEXT SWITCHES	 Context overhead .
  | CPU					CPU expenses .
  | IPC					 Sending and receiving overhead .
  | MEMORY				 Memory overhead .
  | PAGE FAULTS			 Page error overhead .
  | SOURCE				source expenses .
  | SWAPS				 Exchange times overhead .
}

 Illustrate with examples :
SHOW PROFILE CPU FOR QUERY 2;	 Inquire about 2 Number SQL Of CPU Spending .
 The following is an English translation , use CSDN Translated ( stay CSDN I don't want to translate in the advertisement ), Capable students can read the above English .
 Bloggers suggest that , Copy this Chinese paragraph into your own notepad++ And so on , It doesn't look good here , Is too small .
 It's better to have a look at this passage , Don't stop watching just because I said it was a translator .
 It's not just translation , I have my own words in it , There is a lot of knowledge .
 Eleven sentences in all .


1SHOW PROFILE and SHOW PROFILES Statement displays the statement according to the configuration file session Information about resource usage performed during .

2SHOW PROFILE and SHOW PROFILES Statement is obsolete and will be in the future MySQL Delete... In version . Use Performance Schema replace .
#  what , There will be no more profile This thing , Let's go .

3、 To control the analysis , Please use the parse session variable , This variable has a default value of 0( close ). By setting the analysis to 1 Or enable analysis on4SHOW PROFILES Displays a list of the latest statements sent to , The size of the server list is determined by profile_history_size Session variables determine , The default value is 15 The maximum value is 100. Set the value to 0 Has the effect of disabling analysis .

5、 except SHOW PROFILE and SHOW PROFILES Outside , All statements will be analyzed , So you can't find these statements in the profile list .

6、profile Malformed statements . for example ,SHOW PROFILING It's an illegal statement , If you try to execute the statement , There will be grammatical errors , But it will appear in the analysis list .

7show profile Statement displays detailed information about a single statement . without for query n Clause , The output belongs to the most recently executed statement . If you include FOR QUERY n, Is displayed PROFILE, Show statement n Information about .n The value of corresponds to the query SHOW PROFILES According to the ID value .

8、 have access to LIMIT ROW_COUNT Clause limits the output to a number of lines . If a limit is given , You can add an offset at the beginning , Put the output offset rows into the complete row set .

9、 By default ,show profile Show status and duration Columns . Two fields , The state value is similar to SHOW PROCESSLIST The displayed status value , You can specify an optional type value to display the information type of a specific added value .

10show profile Statement parameter type interpretation , It corresponds to the nine parameters above .
ALL						 Show all information 

BLOCK IO				 block IO Displays the count of block input and output operations 

CONTEXT SWITCHES		 Context switch 

CPU						CPU Show users and systems CPU Use your time 

IPC						IPC Displays the count of messages sent and received 

MEMORY					 Memory is not currently implemented 

PAGE FAULTS				 Page errors displays the count of primary and secondary page errors 

SOURCE					SOURCE Displays the name of the function in the source code or the name and line number of the file where the function is located 

SWAPS					 Exchange shows the switch count 

11、 Enable analysis for each session , At the end of the conversation , Its analysis information will be lost .


Step four : analysis MySQL Teaching example.

Execute the command line , Turn on sql Analysis function :
SHOW VARIABLES LIKE ‘%pro%’;

SET profiling = 1;

SELECT @@profiling; To see if it's turned on

 Insert picture description here

Carry out orders :
SELECT COUNT(*) FROM laoben;
SHOW PROFILES;
You can see above select Ordered query_id by 5, The time consumed is 0.00078257s.
 Insert picture description here



Carry out orders :
SHOW PROFILE;
This command shows the previous one sql Statement execution , By default, there are two fields .
status,duration. State and duration .
 Insert picture description here



Carry out orders : Specified field , Appoint query_id, see cpu,io expenses .
SHOW PROFILE cpu,block io FOR QUERY 11;
 Insert picture description here

版权声明
本文为[osc_jrhexi1r]所创,转载请带上原文链接,感谢