[转载]批量给表整理碎片
时间:2009-07-20 来源:hb_li_520
个存储过程目的是给一个库的所有表来整理碎片的。一个表随着插入很频繁,或者一直更新不停的,就会积累好多碎片。如果及时整理一下,查询效率会高出好多。
不过要注意对数据巨大的表整理的时间可能非常的长。
1、原存储过程
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 ;
2、调用示例:
mysql> use mysql
Database changed
mysql> call sp_optimize_tables('david_test');
+------------------------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------------------+----------+----------+----------+
| david_test.test1 | optimize | status | OK |
+------------------------------+----------+----------+----------+
1 row in set (0.26 sec)
...
+----------------------------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+----------------------------------+----------+----------+----------+
| david_test.test_article_content | optimize | status | OK |
+----------------------------------+----------+----------+----------+
1 row in set (37.81 sec)
+--------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+--------------+----------+----------+----------+
| david_test.t | optimize | status | OK |
+--------------+----------+----------+----------+
1 row in set (41.10 sec)
Query OK, 0 rows affected (41.13 sec)
3、在SHELL中调用
#!/bin/sh
#
# Created by david yeung.
# Optimize all the table one by one.
#
cd /usr/local/mysql/bin
./mysql -uoptimize_user -poptimize -e "use \"$1\";call mysql.sp_optimize_tables(\"$1\")"
不过要注意给optimize_user的select,insert权限。
不过要注意对数据巨大的表整理的时间可能非常的长。
1、原存储过程
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 ;
2、调用示例:
mysql> use mysql
Database changed
mysql> call sp_optimize_tables('david_test');
+------------------------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------------------------+----------+----------+----------+
| david_test.test1 | optimize | status | OK |
+------------------------------+----------+----------+----------+
1 row in set (0.26 sec)
...
+----------------------------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+----------------------------------+----------+----------+----------+
| david_test.test_article_content | optimize | status | OK |
+----------------------------------+----------+----------+----------+
1 row in set (37.81 sec)
+--------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+--------------+----------+----------+----------+
| david_test.t | optimize | status | OK |
+--------------+----------+----------+----------+
1 row in set (41.10 sec)
Query OK, 0 rows affected (41.13 sec)
3、在SHELL中调用
#!/bin/sh
#
# Created by david yeung.
# Optimize all the table one by one.
#
cd /usr/local/mysql/bin
./mysql -uoptimize_user -poptimize -e "use \"$1\";call mysql.sp_optimize_tables(\"$1\")"
不过要注意给optimize_user的select,insert权限。
相关阅读 更多 +