Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure Redis slow query log

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to configure the Redis slow query log". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Slow query log

What is SLOW LOG?

Slow log is a logging system used by Redis to record command requests that take longer than a given length of time to execute a query. Query execution time refers to the time it takes to execute a query command, excluding IO operations such as client response (talking) and sending replies. In addition, slow log is stored in memory and reads and writes very fast, so you can safely use it without having to worry about harming the speed of Redis by turning on slow log.

Set up and view SLOWLOG

The server configuration has two options related to slow query logs:

Slowlog-log-slower-than: the option specifies how many microseconds (1 second equals 1000000 microseconds) command requests that take longer to execute will be logged. For example, if the value of this option is 100, then commands that take longer than 100 microseconds will be logged to the slow query log; if the value of this option is 500, then commands that take more than 500 microseconds will be logged to the slow query log, and so on.

Slowlog-max-len: the option specifies the maximum number of slow query logs that the server can save. The server saves multiple slow logs on a first-in-first-out basis: when the number of slow logs stored by the server is equal to the value of the slowlog-max-len option, the server will delete the oldest slow log before adding a new one. For example, if the server slowlog-max-len has a value of 100 and assumes that the server has stored 100 slow query logs, then if the server is going to add a new log, it must first delete the oldest log currently saved and then add the new log.

Let's take a look at an example of the slow query log feature. First, use the CONFIG_SET command to set the value of the slowlog-log-slower-than option to 0 microseconds, so that any commands executed by the Redis server will be recorded in the slow log, and then set the value of the slowlog-max-len option to 5, so that the server can only save a maximum of 5 slow logs:

Redis > CONFIG SET slowlog-log-slower-than 0OKredis > CONFIG SET slowlog-max-len 5OK

Next, we use the client to send several command requests:

Redis > SET msg "hello world" OKredis > SET number 10086OKredis > SET database "Redis" OK

Then use the SLOWLOG GET command to view the slow query log saved by the server:

Redis > SLOWLOG GET1) 1) (integer) 4 # log unique identifier (uid); 2) (integer) 1378781447 # command execution UNIX timest 3) (integer) 13 # command execution time in microseconds; 4) 1) "SET" # command and command parameters 2) "database" 3) "Redis" 2) 1) (integer) 3 2) (integer) 1378781439 3) (integer) 10 4) 1) "SET" 2) "number" 3) 1) (integer) 2 2) (integer) 1378781436 3) (integer) 18 4) 1) "SET" 2) "msg" 3) " Hello world "4) 1) (integer) 1 2) (integer) 1378781425 3) (integer) 11 4) 1)" CONFIG "2)" SET "3)" slowlog-max-len "4)" 5 "5) 1) (integer) 02) (integer) 1378781415 3) (integer) 53 4) 1)" CONFIG "2)" SET "3)" slowlog-log-slower-than "4)" 0 "

If we execute another SLOWLOG GET 1 command at this time, we will see that the last SLOWLOG GET command has been recorded in the slow query log, while the oldest slow query log numbered 0 has been deleted, and the number of slow query logs on the server is still 5:

Redis > SLOWLOG GET 11) 1) (integer) 52) (integer) 1378781521 3) (integer) 61 4) 1) "SLOWLOG" 2) "GET"

The unique id of the log is reset only when the Redis server is restarted, which avoids repetitive processing of the log (for example, you may want to email you every time you find a new slow query).

Use the command SLOWLOG LEN to view the number of current logs.

Note the difference between this value and slower-max-len, one is the number of current logs and the other is the maximum number of logs allowed to be logged.

Redis > SLOWLOG LEN (integer) 5

Clear the log

Use the command SLOWLOG RESET to empty the slow log.

This is the end of redis > SLOWLOG RESETOK "how to configure Redis slow query logs". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report