mysql> SHOW CREATE TABLE mysql.slow_log;
+------------------+------------------------------------------------------------------+
| Table | Create Table |
+------------------+------------------------------------------------------------------+
| slow_log_backup2 | CREATE TABLE `slow_log` (
`start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
`user_host` mediumtext NOT NULL,
`query_time` time(6) NOT NULL,
`lock_time` time(6) NOT NULL,
`rows_sent` int NOT NULL,
`rows_examined` int NOT NULL,
`db` varchar(512) NOT NULL,
`last_insert_id` int NOT NULL,
`insert_id` int NOT NULL,
`server_id` int unsigned NOT NULL,
`sql_text` mediumblob NOT NULL,
`thread_id` bigint unsigned NOT NULL
) ENGINE=CSV DEFAULT CHARSET=utf8mb3 COMMENT='Slow log' |
+------------------+------------------------------------------------------------------+
1 row in set (0.00 sec)
#ON状态下
mysql> SET GLOBAL slow_query_log=ON;
mysql> DROP TABLE slow_log;
ERROR 1580 (HY000): You cannot DROP a log table if logging is enabled
'
#OFF状态下
mysql> SET GLOBAL slow_query_log=OFF;
mysql> DROP TABLE slow_log;
Query OK, 0 rows affected (0.00 sec)
#再次开启慢日志,因为表不存在,所以报错,需要手动重新创建。
mysql> SET GLOBAL slow_query_log=ON;
ERROR 1146 (42S02): Table 'mysql.slow_log' doesn't exist
目前表支持MyISAM和CSV引擎的转换,不能改成Innodb引擎。
#关闭慢日志
mysql> SET GLOBAL slow_query_log=OFF;
Query OK, 0 rows affected (0.00 sec)
#在MyISAM引擎下添加索引
mysql> ALTER TABLE slow_log engine=MyISAM;
mysql> ALTER TABLE slow_log add index idx_id(insert_id);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
#更改引擎InnoDB,失败
mysql> ALTER TABLE slow_log engine=InnoDB;
ERROR 1579 (HY000): This storage engine cannot be used for log tables
官方说明中,可以RENAME重命名方式替换表同时改成InnoDB引擎.如下新的记录表采用InnoDB引擎,为了查询方便在start_time创建索引。当然也可以关闭日志DROP之后手动创建。
#slow_log_tmp新表
USE mysql;
CREATE TABLE `slow_log_tmp` (
`start_time` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
`user_host` mediumtext NOT NULL,
`query_time` time(6) NOT NULL,
`lock_time` time(6) NOT NULL,
`rows_sent` int NOT NULL,
`rows_examined` int NOT NULL,
`db` varchar(512) NOT NULL,
`last_insert_id` int NOT NULL,
`insert_id` int NOT NULL,
`server_id` int unsigned NOT NULL,
`sql_text` mediumblob NOT NULL,
`thread_id` bigint unsigned NOT NULL,
KEY `idx_dt` (`start_time`)
) ENGINE=InnoDB;
#进行rename操作
mysql> RENAME TABLE slow_log TO slow_log_backup1, slow_log_tmp TO slow_log;
Query OK, 0 rows affected (0.02 sec)
对于output表,可以进行TRUNCATE方式清理,但不支持DELETE语句。