MySQL / MariaDB事件优化数据库表
MySQL / MariaDB event to optimize database tables
我有兴趣创建一个周期性事件来每晚优化我的数据库表。
我到了 SELECT Concat('OPTIMIZE TABLE ', TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES WHERE table_schema='database_name';
。如何在 MySQL 事件中执行每一行?
我更喜欢使用 MySQL / MariaDB 事件而不涉及 bash。谢谢!
所以,这是您的查询。您只需要更改 SELECT 语句,然后就可以调用它
ELIMITER //
CREATE PROCEDURE p1()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE val VARCHAR(2000);
DECLARE curs CURSOR FOR SELECT sqlstring FROM table_a;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs;
read_loop: LOOP
FETCH curs INTO val;
IF done THEN
LEAVE read_loop;
END IF;
SET @sql := val;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE curs;
END; //
DELIMITER ;
叫它
调用 p1();
设法 运行 通过:
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE val VARCHAR(2000);
DECLARE curs CURSOR FOR SELECT Concat('OPTIMIZE TABLE ', TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES WHERE table_schema='database_name';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs;
read_loop: LOOP
FETCH curs INTO val;
IF done THEN
LEAVE read_loop;
END IF;
SET @sql := val;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE curs;
END
我有兴趣创建一个周期性事件来每晚优化我的数据库表。
我到了 SELECT Concat('OPTIMIZE TABLE ', TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES WHERE table_schema='database_name';
。如何在 MySQL 事件中执行每一行?
我更喜欢使用 MySQL / MariaDB 事件而不涉及 bash。谢谢!
所以,这是您的查询。您只需要更改 SELECT 语句,然后就可以调用它
ELIMITER //
CREATE PROCEDURE p1()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE val VARCHAR(2000);
DECLARE curs CURSOR FOR SELECT sqlstring FROM table_a;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs;
read_loop: LOOP
FETCH curs INTO val;
IF done THEN
LEAVE read_loop;
END IF;
SET @sql := val;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE curs;
END; //
DELIMITER ;
叫它
调用 p1();
设法 运行 通过:
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE val VARCHAR(2000);
DECLARE curs CURSOR FOR SELECT Concat('OPTIMIZE TABLE ', TABLE_NAME, ';') FROM INFORMATION_SCHEMA.TABLES WHERE table_schema='database_name';
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN curs;
read_loop: LOOP
FETCH curs INTO val;
IF done THEN
LEAVE read_loop;
END IF;
SET @sql := val;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END LOOP;
CLOSE curs;
END