Slow Query LogRecords the most recent command with N execution time exceeding m milliseconds. The slow query log is saved in memory, not in the file, which guarantees the efficiency of the slow query log.
the entry definition for the slow query log/* this structure defines an entry inside the slow log list *//* * Slow Query Log */typedef struct slowlogentry { // Command and command parameters robj **argv; // number of commands and command parameters int argc; // Unique Identifiers long long id; /* Unique entry identifier. */ // the time taken to execute the command, in microseconds // note that nanoseconds is wrong long long duration; /* time spent by the query, in nanoseconds. */ // time when the command was executed, in the format UNIX timestamp time_t time; /* Unix time at which the query was executed. */} slowlogentry;
definitions related to server and slow queries /* slowlog */ // keeps a list of all slow query logs list *slowlog; /* SLOWLOG list of commands */ // id long long slowlog_entry_id; /* of the next slow query log SLOWLOG current entry ID */ // Server Configuration The value of the slowlog-log-slower-than option long long slowlog_log_slower_than; /* SLOWLOG time limit (to get logged) */ // server configuration Values for slowlog-max-len options unsigned long slowlog_max_len; Slow queries for /* slowlog max number of items logged */servers are stored in a list , each item in the list is a slow query log, and the newer log is always saved at the top of the queue. Slow query logThe execution parameters and execution time of the command are saved, and the parameters and logs may be truncated if the system limit is exceeded.
client operations supported by slow queriesGet: Get one or all slow query logs reset: Empty slow query Log len: Number of slow query logs
application of slow query logEach time Redis executes a command, it records the start and end times of the command, thus calculating the execution times of the command. Concurrent commands and the execution time of the command are passed to slowlogpushentryifneeded, and the slowlogpushentryifneeded determines whether to generate a slow query log. /* call () is the core of redis execution of a command * Invoke the command's implementation function, execute command void call (redisclient *c, int flags) { //Get command Execution time /* log the command into the slow log if needed, and populate the * per-command statistics that we show in INFO commandstats. */ // if necessary, place the command in slowlog Inside if (flags & redis_call_slowlog && C->cmd->proc != execcommand) Slowlogpushentryifneeded (c->argv,c->argc,duration);}
the realization of slowlogpushentryifneededJudge the system flag bit and add the slow query log to the server's slow query/* push a new entry into the slow log list. * * if the parameter duration exceeds the maximum time set by the server, * then a new entry is pushed into the slow query log in FIFO order. * * this function will make sure to trim the slow log accordingly to the * configured max length. * * Depending on the maximum log length set by the server, the log may be truncated (trim) */void slowlogpushentryifneeded (robj **argv, int argc, long long duration) { // Slow query function is not turned on, direct return if (server.slowlog_log_slower_than < 0) return; /* Slowlog disabled */ // if the execution time exceeds the limit set by the server, add the command to the slow query log if (Duration >= server.slowlog_log_slower_than) // New log added to the list header &nbsP; listaddnodehead (Server.slowlog,slowlogcreateentry (argv,argc,duration)); /* remove old entries if needed. */ // If the number of logs is too large, then delete while (Listlength (server.slowlog) > Server.slowlog_max_len) listdelnode (Server.slowlog,listLast ( Server.slowlog));}
Slow query log for Redis