Everyone is used to this one, the good old text file.
Just run the following to flush a slow log everyday
STEP 01) Turn off the slow query log
SET GLOBAL slow_query_log = 'OFF';
STEP 02) Copy the text file
cat slow-query.log | gzip > /logs/slow-query-`date +"%Y%m%d-%H%M"`.log.gz
STEP 03) Truncate the file to zero bytes
echo -n > slow-query.log
STEP 04) Turn on the slow query log
SET GLOBAL slow_query_log = 'ON';
You could switch to log-output=TABLE
and deal with it as a Table to Query.
STEP 01) Convert mysql.slow_log from CSV to MyISAM
ALTER TABLE mysql.slow_log ENGINE = MyISAM;
STEP 02) Index the table
ALTER TABLE mysql.slow_log ADD INDEX (start_time);
STEP 03) Activate log format to be TABLE
[mysqld]
log-output=TABLE
STEP 04) service mysql restart
Once you startup mysqld, the slow log entries are recorded in the MyISAM table mysql.slow_log;
To rotate out the entries before midnight, you could something like this:
SET GLOBAL slow_query_log = 'OFF';
SET @dt = NOW();
SET @dtstamp = DATE_FORMAT(@dt,'%Y%m%d_%H%i%S');
SET @midnight = DATE(@dt) + INTERVAL 0 SECOND;
ALTER TABLE mysql.slow_log RENAME mysql.slow_log_old;
CREATE TABLE mysql.slow_log LIKE mysql.slow_log_old;
INSERT INTO mysql.slow_log SELECT * FROM mysql.slow_log_old WHERE start_time >= @midnight;
DELETE FROM mysql.slow_log_old WHERE start_time >= @midnight;
SET @sql = CONCAT('ALTER TABLE mysql.slow_log_old RENAME mysql.slow_log_',@dtstamp);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
SET GLOBAL slow_query_log = 'ON';
and that's all for slow logs...