我有兴趣创建一个周期性事件,以每晚优化我的数据库表。

我去了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();

10-05 19:24