DROP PROCEDURE IFEXISTS `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;
selectcount(*)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');