关于long_query_time参数的一个测试
admin
2023-05-01 22:04:32
0次

创建测试表,其建表语句如下:

mysql> show create table test1;

+-------+----------------------------------------------------------------------------------------------------------------------------------------------------+

| Table | Create Table                                                                                                                                       |

+-------+----------------------------------------------------------------------------------------------------------------------------------------------------+

| test1 | CREATE TABLE `test1` (

  `a` int(10) DEFAULT NULL,

  `b` varchar(10) DEFAULT NULL,

  KEY `test_index_a` (`a`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

+-------+----------------------------------------------------------------------------------------------------------------------------------------------------+

1 row in set (0.00 sec)


插入测试数据:

mysql> insert into test1 values (1,'a'),(2,'a'),(3,'a'),(4,'d'),(5,'e'),(6,'f'),(7,'g'),(8,'h'),(9,'i'),(10,'j'); 

Query OK, 10 rows affected (0.00 sec)

Records: 10  Duplicates: 0  Warnings: 0


mysql> select * from test1;

+------+------+

| a    | b    |

+------+------+

|    1 | a    |

|    2 | a    |

|    3 | a    |

|    4 | d    |

|    5 | e    |

|    6 | f    |

|    7 | g    |

|    8 | h    |

|    9 | i    |

|   10 | j    |

+------+------+

10 rows in set (0.00 sec)


数据库slow日志相关参数配置:

slow_query_log = 1

slow_query_log_file = /data/mysql/mysql3306/slow_statement.log

long_query_time = 0

log_queries_not_using_indexes = 0



session A:


mysql> set autocommit=0;

Query OK, 0 rows affected (0.00 sec)


mysql> update test1 set b='xx' where b='a';

Query OK, 3 rows affected (0.00 sec)

Rows matched: 3  Changed: 3  Warnings: 0


mysql> commit;

Query OK, 0 rows affected (0.01 sec)



session B:


mysql> update test1 set b='yy' where b='a';


Query OK, 0 rows affected (9.38 sec)

Rows matched: 0  Changed: 0  Warnings: 0


mysql> commit;

Query OK, 0 rows affected (0.00 sec)


慢日志记录:

# Time: 2018-03-30T02:42:16.027553Z

# User@Host: root[root] @ localhost []  Id:    47

# Query_time: 0.001280  Lock_time: 0.000310 Rows_sent: 0  Rows_examined: 10

SET timestamp=1522377736;

update test1 set b='xx' where b='a';

# Time: 2018-03-30T02:42:29.785509Z

# User@Host: root[root] @ localhost []  Id:    47

# Query_time: 0.008619  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0

SET timestamp=1522377749;

commit;

# Time: 2018-03-30T02:42:29.785817Z

# User@Host: root[root] @ localhost []  Id:    48

# Query_time: 9.375238  Lock_time: 9.374875 Rows_sent: 0  Rows_examined: 11

SET timestamp=1522377749;

update test1 set b='yy' where b='a';


小结1:当参数long_query_time设置为0,则执行时长大于0s的语句都会记录到slowlog里面。


============================================================================



数据库slow日志相关参数配置:

slow_query_log = 1

slow_query_log_file = /data/mysql/mysql3306/slow_statement.log

long_query_time = 0.0001

log_queries_not_using_indexes = 0


session A:


mysql> set autocommit=0;

Query OK, 0 rows affected (0.00 sec)


mysql> update test1 set b='xx' where b='a';

Query OK, 3 rows affected (0.01 sec)

Rows matched: 3  Changed: 3  Warnings: 0


mysql> commit;

Query OK, 0 rows affected (0.01 sec)


session B;


mysql> set autocommit=0;

Query OK, 0 rows affected (0.00 sec)


mysql> update test1 set b='yy' where b='a';

Query OK, 0 rows affected (8.63 sec)

Rows matched: 0  Changed: 0  Warnings: 0


mysql> commit;

Query OK, 0 rows affected (0.00 sec)


慢日志记录:

# Time: 2018-03-30T02:52:11.214300Z

# User@Host: root[root] @ localhost []  Id:     3

# Query_time: 0.001435  Lock_time: 0.000561 Rows_sent: 0  Rows_examined: 10

SET timestamp=1522378331;

update test1 set b='xx' where b='a';

# Time: 2018-03-30T02:52:25.326360Z

# User@Host: root[root] @ localhost []  Id:     3

# Query_time: 0.007641  Lock_time: 0.000000 Rows_sent: 0  Rows_examined: 0

SET timestamp=1522378345;

commit;

# Time: 2018-03-30T02:52:25.327693Z

# User@Host: root[root] @ localhost []  Id:     4

# Query_time: 8.629981  Lock_time: 8.629332 Rows_sent: 0  Rows_examined: 11

SET timestamp=1522378345;

update test1 set b='yy' where b='a';


小结2:当参数long_query_time设置为0.0001,则执行时长大于0.0001s的语句都会记录到slowlog里面。


=============================================================================


数据库slow日志相关参数配置:

slow_query_log = 1

slow_query_log_file = /data/mysql/mysql3306/slow_statement.log

long_query_time = 1

log_queries_not_using_indexes = 0


session A:


mysql> set autocommit=0;

Query OK, 0 rows affected (0.00 sec)


mysql> update test1 set b='xx' where b='a';

Query OK, 3 rows affected (0.00 sec)

Rows matched: 3  Changed: 3  Warnings: 0


mysql> commit;

Query OK, 0 rows affected (0.00 sec)


session B:


mysql> set autocommit=0;

Query OK, 0 rows affected (0.00 sec)

mysql> update test1 set b='yy' where b='a';

Query OK, 0 rows affected (10.13 sec)

Rows matched: 0  Changed: 0  Warnings: 0


mysql> commit;

Query OK, 0 rows affected (0.00 sec)


慢日志记录:

# Time: 2018-03-30T02:56:32.433616Z

# User@Host: root[root] @ localhost []  Id:     5

# Query_time: 0.001227  Lock_time: 0.000149 Rows_sent: 1  Rows_examined: 1050

SET timestamp=1522378592;

SHOW VARIABLES LIKE 'pid_file';

/usr/local/mysql/bin/mysqld, Version: 5.7.20-log (MySQL Community Server (GPL)). started with:

Tcp port: 3306  Unix socket: /data/mysql/mysql3306/mysql3306.sock

Time                 Id Command    Argument


小结3:当参数long_query_time设置为1,则执行时长不大于1s的语句不会记录到slowlog里面。



总结:从上面3个实验可以发现,参数long_query_time影响了慢SQL在slowlog的记录。只有运行时长大于long_query_time参数的SQL,才会记录到slowlog。这个运行时长,是不包括由于事务锁等待消耗的时间的。

也就是说,exec_time= query_time - lock_time。 当exec_time >= long_query_time的SQL才会被记录到慢SQL里面。很多人会误认为只有query_time>= long_query_time就会记录到slowlog,需要纠正这个认知。


相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...