Redis | slow query

Manong up2u 2021-01-14 18:43:36
redis slow query

MySQL There are slow queries in ,Redis There are also slow queries in ,Redis My slow query is If the command execution exceeds the set threshold, it is a slow query . Let's sort it out .

The slow query

Redis It will record the command whose execution time exceeds the set threshold time , Slow query here means slow command execution , It's not I/O slow .

In general , We all connect through the client Redis The server , Then send the command to Redis The server ,Redis The server will cache the commands from each client into a queue , And then execute one by one , Finally, return the result to the client . And here we are Slow query means “ Carry out orders ” That part. . Not the Internet I/O perhaps The problem of command queuing .

About slow query configuration

There are two configurations for slow queries , They are as follows :

slowlog-log-slower-than: Slow query threshold , Command execution exceeds the value set by the configuration parameter , It is considered slow query ;

slowlog-max-len: The maximum number of slow query log records , That is to say Redis How many slow query records can be recorded at most . Slow query record is a list type , If the maximum allowable record 100 strip , So in record number 101 Stripe time , The first 1 Records will be removed , The number of records that always keep slow queries is 100 strip .

Slow query related commands

Here are a few commands , These commands are related to slow queries , A screenshot of the command and configuration is shown below :

config Commands are commands for configuration , Not just for slow queries , Because we will use these two commands , Therefore, I would also like to mention .config Commands can be obtained and set dynamically Redis Some parameters of the server , Usually we view and set the parameters to enter redis.conf In this file , But at run time, you can use config Command to quickly get and set parameters .

To use config get obtain slowlog-log-slower-than and slowlog-max-len Two parameters , The order is as follows .> config get slowlog-log-slower-than
1) "slowlog-log-slower-than"
2) "10000"> config get slowlog-max-len
1) "slowlog-max-len"
2) "128"

From the return result of the run, we can see that ,slowlog-log-slower-than The value of is 10000, This value is in microseconds , that 10000 Microseconds are 10 millisecond .slowlog-max-len The value of is 128, namely Redis Save only 128 Slow query records .

slowlog-log-slower-than If set to 0, Record all commands to the slow query queue , If slowlog-log-slower-than Set to Less than 0 Value , It means that no command is recorded in the slow query queue .

stay redis-cli Next , Use config The command has no syntax prompt !

The above two configurations are about slow query configuration , About slow query command Redis Provides slowlog The order of , This command can provide some parameters , Introduce the following .

slowlog get [n]: obtain [ Specify the number ] Slow query list of ;

slowlog len: Get the number of slow query records ;

slowlog reset: Clear the slow query list .

The above is about the configuration and command of slow query .

Command demonstration

There are not many configurations and commands about slow query , The command here demonstrates , Mainly to see slowlog get Return result of command .

View the configuration parameters of slow query

Look at slowlog-log-slower-than and slowlog-max-len The values of the two configuration parameters .> config get slowlog-log-slower-than
1) "slowlog-log-slower-than"
2) "10000"> config get slowlog-max-len
1) "slowlog-max-len"
2) "128"

slowlog-log-slower-than The default value of is 10000 Microsecond , That is to say 10 millisecond , This has been described before .slowlog-max-len The default value of is 128, That is to say, the slow query command queue can be saved 128 Slow query records .

Set slow query configuration parameters

Set the values of the two parameters to the values suitable for our learning .> config set slowlog-log-slower-than 0
OK> config set slowlog-max-len 5

slowlog-log-slower-than Is set to 0, So you can record all the orders , And then slowlog-max-len Is set to 5, That is to say, only save 5 Slow query records .

Just test a few commands

Just test a few commands , It is convenient for us to check the content of slow query later .> keys *
1) "k1"
2) "k2"> get k1
"v1"> hgetall k2
1) "k21"
2) "v1"
3) "k22"
4) "v2"

see Redis The number of slow queries recorded

adopt slowlog len Command to see how many slow query commands are recorded .> slowlog len
(integer) 5

Check slow query

adopt slowlog get Command to view the list of slow queries .> slowlog get
1) 1) (integer) 6
2) (integer) 1610156168
3) (integer) 2
4) 1) "slowlog"
2) "len"
5) ""
6) ""
2) 1) (integer) 5
2) (integer) 1610156086
3) (integer) 10
4) 1) "hgetall"
2) "k2"
5) ""
6) ""
3) 1) (integer) 4
2) (integer) 1610156073
3) (integer) 9
4) 1) "get"
2) "k1"
5) ""
6) ""
4) 1) (integer) 3
2) (integer) 1610156064
3) (integer) 6
4) 1) "keys"
2) "*"
5) ""
6) ""
5) 1) (integer) 2
2) (integer) 1610155960
3) (integer) 5
4) 1) "config"
2) "set"
3) "slowlog-max-len"
4) "5"
5) ""
6) ""

You can see that the above list is all the slow query commands , Because we only keep 5 strip . For convenience, we only look at one , The order is as follows .> slowlog get 1
1) 1) (integer) 7
2) (integer) 1610156232
3) (integer) 24
4) 1) "slowlog"
2) "get"
5) ""
6) ""

In the output above , There are six lines , Explain separately :

1. Slow query label of record , Inverted display

2. Record the timestamp of the command

3. The time it takes to execute a command , In microseconds

4. Specific orders to execute

5. Execute the client's IP Address and port number

With the above information, we can easily locate which client executed which command to cause slow query .


Redis Slow down may be due to slow operation on some data structures , It may also be that improper data structure is used . Yes, of course , Lead to Redis There's a lot of slowing down , It's not just the execution part that leads to , But slow queries can only help us record slow commands , As for the cause Redis The reason why it's so slow , It's a multi-faceted search .

This article is from WeChat official account. - Code the agriculture UP2U(gh_3c91b47a82e0) , author : Code the agriculture UP2U

The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the Delete .

Original publication time : 2021-01-11

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

本文为[Manong up2u]所创,转载请带上原文链接,感谢

  1. Learn about RPC, why RPC was born, and what's the difference between RPC and HTTP?
  2. Learn about RPC, why RPC was born, and what's the difference between RPC and HTTP?
  3. Learn java base conversion supplementary learning
  4. JDBC测试连接数据库
  5. JDBC test connection database
  6. 大厂面试官竟然这么爱问Kafka,一连八个Kafka问题把我问蒙了?
  7. The interviewers of big factories love to ask Kafka so much. I'm blinded by eight Kafka questions in a row?
  8. 安卓开发和java开发有什么区别!2021年BATJ30套大厂Android经典高频面试题,面试必问
  9. Spring Security OAuth2.0認證授權四:分散式系統認證授權
  10. What's the difference between Android development and java development! 2021 batj30 Android classic high frequency interview questions
  11. Spring security oauth2.0 authentication and authorization 4: distributed system authentication and authorization
  12. Java微服务 vs Go微服务,究竟谁更强!?
  13. 大厂面试官竟然这么爱问Kafka,一连八个Kafka问题把我问蒙了?
  14. Who is stronger, Java microservice vs go microservice!?
  15. Java微服务 vs Go微服务,究竟谁更强!?
  16. The interviewers of big factories love to ask Kafka so much. I'm blinded by eight Kafka questions in a row?
  17. Who is stronger, Java microservice vs go microservice!?
  18. springboot异常处理之404
  19. Spring boot exception handling 404
  20. Spring Boot Security 国际化 多语言 i18n 趟过巨坑
  21. springboot异常处理之404
  22. Spring boot security international multilingual I18N
  23. Spring boot exception handling 404
  24. Netty系列化之Google Protobuf编解码
  25. Netty之编解码
  26. Java编解码
  27. Netty解码器
  28. Netty与TCP粘包拆包
  29. Netty开发入门
  30. Java集合遍历时遇到的坑
  31. Spring IOC 源码解析(下)
  32. Spring IoC源码解析(上)
  33. Google protobuf codec of netty serialization
  34. Encoding and decoding of netty
  35. Java codec
  36. Netty decoder
  37. Netty and TCP packet sticking and unpacking
  38. Introduction to netty development
  39. Problems encountered in Java collection traversal
  40. Spring IOC source code analysis (2)
  41. Spring IOC source code analysis (Part one)
  42. 半小时用Spring Boot注解实现Redis分布式锁
  43. Implementing redis distributed lock with spring boot annotation in half an hour
  44. What should we do if we can't get tickets for Spring Festival transportation? You can solve this problem by using these ticket grabbing apps!
  45. 百度智能(文本识别),API传图OC代码与SDK使用
  46. springboot源码解析-管中窥豹系列之aware(六)
  47. Baidu intelligent (text recognition), API map, OC code and SDK
  48. Spring boot source code analysis
  49. springboot源码解析-管中窥豹系列之aware(六)
  50. 百度智能(文本识别),API传图OC代码与SDK使用
  51. Spring boot source code analysis
  52. Baidu intelligent (text recognition), API map, OC code and SDK
  53. Java学习笔记
  54. Java learning notes
  55. Sentry(v20.12.1) K8S 雲原生架構探索, SENTRY FOR JAVASCRIPT 手動捕獲事件基本用法
  56. 我的程式設計師之路:自學Java篇
  57. SpringBoot專案,如何優雅的把介面引數中的空白值替換為null值?
  58. Sentry (v20.12.1) k8s cloud native architecture exploration, sentry for JavaScript manual capture event basic usage
  59. My way of programmer: self study java
  60. Spring boot project, how to gracefully replace the blank value in the interface argument with null value?