分类: Mysql/postgreSQL
2008-01-28 13:40:44
DELIMITER $$
DROP PROCEDURE IF EXISTS `mysql`.`sp_optimize_tables`$$
CREATE PROCEDURE `mysql`.`sp_optimize_tables`(
IN db_name varchar(255))
BEGIN
-- Created by david yeung 20080128.
-- To optimize all the tables in exact database.
declare cnt int default 0;
declare i int default 0;
select count(*) as total from information_schema.tables where table_schema = db_name into cnt;
while i < cnt
do
-- Get the table's exact name.
set @stmt = concat('select table_name from information_schema.tables where table_schema = ''',db_name,''' order by table_name asc limit ',i,',1 into @tb_name');
prepare s1 from @stmt;
execute s1;
drop prepare s1;
set @stmt = '';
set @stmt = concat('optimize table ',db_name,'.',@tb_name);
prepare s1 from @stmt;
execute s1;
drop prepare s1;
set @stmt = '';
set i = i + 1;
end while;
-- Refresh tables.
flush tables;
END$$
DELIMITER ;
chinaunix网友2008-06-12 10:59:21
你个是不是重复优化就会有问题? 第一次 -------------+----------+----------+-----------------------------+ | Table | Op | Msg_type | Msg_text +-------------+----------+----------+-----------------------------+ | bbs.uc_tags | optimize | status | ok 第二次Msg_text显示 Table is already up to date 第三次显示 The handler for the table doesn't support repair