How much do you know about the slow query of MySQL in PHP?

1 Introduction

Turning on the slow query log allows MySQL to record queries that exceed the specified time. By locating and analyzing performance bottlenecks, the performance of the database system can be better optimized.

2. Parameter introduction

slow_query_log slow query open status

slow_query_log_file The location where the slow query log is stored (this directory needs the writable permission of the MySQL running account, and is generally set as the MySQL data storage directory)

long_query_time How many seconds will the query be recorded before, the default is 10 seconds

3. Turn on slow query

(1) View related parameters of slow query

mysql> show variables like 'slow_query%';
+---------------------------+-----------------------------------+
| Variable_name             | Value                              |
+---------------------------+-----------------------------------+
| slow_query_log            | OFF                                |
| slow_query_log_file       | /usr/local/var/mysql/slow.log          |
+---------------------------+-----------------------------------+

mysql> show variables like 'long_query_time';
+-----------------+-----------+
| Variable_name   | Value     |
+-----------------+-----------+
| long_query_time | 10.000000 |
+-----------------+-----------+

(2) Setting method

Method 1: Global Variable Setting

Set slow_query_log global variable to "ON" state

mysql> set global slow_query_log='ON'; 

Set the storage location of slow query logs

mysql> set global slow_query_log_file='/usr/local/var/mysql/slow.log ';

Set the slow query time, the query will be recorded if it exceeds 1 second

mysql> set global long_query_time=1;

Method two: configuration file settings

Modify the configuration file my.cnf and add it under [mysqld]

[mysqld]
slow_query_log = ON
slow_query_log_file = /usr/local/var/mysql/slow.log 
long_query_time = 1

(3) Restart the MySQL service

service mysqld restart

(4) Slow query log analysis

  • Intercept a piece of slow query log:
# Time: 180918 19:06:21
# User@Host: proxy[proxy] @  [192.168.0.16]  Id: 6707197
# Query_time: 1.015429  Lock_time: 0.000116 Rows_sent: 1  Rows_examined: 44438
SET timestamp=1537268781;
select
        id, user_id, device_uuid, bd_client_id, bd_user_id, bd_tag,
        nodisturb_mode, nodisturb_start_time,
        nodisturb_end_time, binding_time, device_os_type, app_type, state
        from app_mobile_device
        where user_id = '78436'
            and app_type = 'YGY'
        order by binding_time desc;
# User@Host: proxy[proxy] @  [192.168.0.16]  Id: 6707236
# Query_time: 1.021662  Lock_time: 0.000083 Rows_sent: 1  Rows_examined: 44438
SET timestamp=1537268781;
select
        id, user_id, device_uuid, bd_client_id, bd_user_id, bd_tag,
        nodisturb_mode, nodisturb_start_time,
        nodisturb_end_time, binding_time, device_os_type, app_type, state
        from app_mobile_device
        where user_id = '14433'
            and app_type = 'YGY'
        order by binding_time desc;

You can see here:

Query_time (the query time of the slow query statement) exceeds the set 1s,

Rows_sent (Slow query returns records) Here only 1 is returned

Rows_examined (the number of rows scanned by the slow query) 44438 -> From here, you can probably see that the problem is big

  • Now put this SQL statement in the database for execution, and use EXPLAIN to analyze the execution plan
EXPLAIN                                
select                                 
        id, user_id, device_uuid, bd_client_id, bd_user_id, bd_tag,                        
        nodisturb_mode, nodisturb_start_time,                          
        nodisturb_end_time, binding_time, device_os_type, app_type, state                          
        from app_mobile_device                         
        where user_id = '78436'                            
            and app_type = 'YGY'                       
        order by binding_time desc;

The query result is:

Insert picture description here

Explain the following parameters:

Insert picture description here

It can be found here that rows is the number of rows to be queried. If more than 4w rows are queried, the slowness is certain.

Because there are several conditions, and no index is used, you can only add an index.

Add a common multi-column index to the selection here, because this table was designed with a problem at the beginning, resulting in duplicate data and no unique index can be set.

ALTER  TABLE  app_mobile_device  ADD  INDEX user_app_type_only (  `user_id` ,`app_type` )

The index is set, and then look at the execution plan of the SQL just now.

Insert picture description here

You can find that the number of rows checked by rows has dropped significantly.

At this point, the use and optimization of slow queries are basically completed.

Reference article: https://www.cnblogs.com/sunxun/p/9673788.html

https://www.cnblogs.com/gxj521test/p/10964795.html

Pay attention, don't get lost

Alright, everyone, the above is the entire content of this article. The people who can see here are all talents . As I said before, there are a lot of technical points in PHP, because there are too many, it is really impossible to write, and you will not read too much after writing it, so I will organize it into PDF and documents here, if necessary Can

Click to enter the secret code: PHP+「Platform」

Insert picture description here

Insert picture description here


For more learning content, please visit the [Comparative Standard Factory] excellent PHP architect tutorial catalog, as long as you can read it to ensure that the salary will rise a step (continuous update)

The above content hopes to help everyone . Many PHPers always encounter some problems and bottlenecks when they are advanced. There is no sense of direction when writing too much business code. I don’t know where to start to improve. I have compiled some information about this, including But not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripts, Docker, microservices, Nginx, etc. Many knowledge points, advanced advanced dry goods, can be shared with everyone for free, and those who need can join my PHP technology exchange group

Guess you like

Origin blog.csdn.net/weixin_49163826/article/details/109061397