网站地图    收藏   

主页 > 后端 > mysql数据库 >

mysql中对数据库的每个表执行优化的存储过程 -

来源:自学PHP网    时间:2015-04-16 10:51 作者: 阅读:

[导读] mysql中对数据库的每个表执行优化的存储过程对数据库的每个表执行优化的存储过程CREATE PROCEDURE `inventory` `optimize_table` (db_name VARCHAR(64))BEGIN DECLARE t VARCHAR(64); DECLARE done IN...

mysql中对数据库的每个表执行优化的存储过程
 
对数据库的每个表执行优化的存储过程
 
CREATE PROCEDURE `inventory`.`optimize_table` (db_name VARCHAR(64))
BEGIN
 DECLARE t VARCHAR(64);
 DECLARE done INT DEFAULT 0;
 DECLARE c CURSOR FOR
  SELECT table_name FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_SCHEMA=db_name AND TABLE_TYPE='BASE TABLE';
 DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
 OPEN c;
 tables_loop:LOOP
  FETCH c INTO t;
  IF done THEN
   CLOSE c;
   LEAVE tables_loop;
  END IF;
  SET @stmt_text:=CONCAT("OPTIMIZE TABLE ",db_name,'.',t);
  PREPARE stmt FROM @stmt_text;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;
 END LOOP;
 CLOSE c;
END

语句2:

CREATE PROCEDURE `inventory`.`optimize_tables2` (db_name VARCHAR(64))
BEGIN
 DECLARE t VARCHAR(64);
 DECLARE done INT DEFAULT 0;
 DECLARE c CURSOR FOR
  SELECT table_name FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_SCHEMA=db_name AND TABLE_TYPE='BASE TABLE';
 DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
 OPEN c;
 REPEAT
  FETCH c INTO t;
  IF NOT done THEN
   SET @stmt_text:=CONCAT("OPTIMIZE TABLE ",db_name,'.',t);
   PREPARE stmt FROM @stmt_text;
   EXECUTE stmt;
   DEALLOCATE PREPARE stmt;
  END IF;
 UNTIL done END REPEAT;
 CLOSE c;
END

调用时为call optimize_tables2('库名');

或者

call optimize_tables('库名');

 


自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论