brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: set slow_query_log at runtime to catch slow database queries without a restart

Database
~/tips/tip-database-mysql-slow-query-log-runtime

SET GLOBAL slow_query_log = 'ON';

Starts logging slow statements on a running MySQL or MariaDB server and shows where the file lives.

I want the slow queries written down before I start guessing. MySQL and MariaDB can switch the log on while the server runs.

SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;
SHOW VARIABLES LIKE 'slow_query_log_file';

Every statement that takes longer than long_query_time seconds is appended to the file named by the last command. The shell tool mysqldumpslow on the server groups similar queries and sorts them by total time, which is easier than reading the log.

SET GLOBAL is lost at the next restart, so put slow_query_log = 1 and long_query_time = 1 in the server config once you are happy with them. A new long_query_time only applies to connections opened after the change.

Further reading: MySQL Reference Manual: The Slow Query Log

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link