Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2344844
  • 博文数量: 276
  • 博客积分: 5998
  • 博客等级: 大校
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-24 14:43
文章分类

全部博文(276)

文章存档

2014年(25)

2013年(11)

2012年(69)

2011年(167)

2010年(4)

分类: Mysql/postgreSQL

2012-02-22 14:19:01



一、存储过程
1、代码

  1. DELIMITER $$
  2.     DROP PROCEDURE IF EXISTS `mysql`.`sp_optimize_tables`$$
  3.     CREATE PROCEDURE `mysql`.`sp_optimize_tables`(
  4.      IN db_name varchar(255))
  5.     BEGIN
  6.       -- Created by david yeung 20080128.
  7.       -- To optimize all the tables in exact database.
  8.       declare cnt int default 0;
  9.       declare i int default 0;
  10.       select count(*) as total from information_schema.tables where table_schema = db_name into cnt;
  11.       while i < cnt
  12.       do
  13.         -- Get the table's exact name.
  14.         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');
  15.         prepare s1 from @stmt;
  16.         execute s1;
  17.         drop prepare s1;
  18.         set @stmt = '';
  19.         set @stmt = concat('optimize table ',db_name,'.',@tb_name);
  20.         prepare s1 from @stmt;
  21.         execute s1;
  22.         drop prepare s1;
  23.         set @stmt = '';
  24.         set i = i + 1;
  25.       end while;
  26.       -- Refresh tables.
  27.       flush tables;
  28.     END$$
  29.     DELIMITER ;
2、调用示例:
  1. mysql> use mysql
  2. Database changed
  3. mysql> call sp_optimize_tables('david_test');
  4. +------------------------------+----------+----------+----------+
  5. | Table | Op | Msg_type | Msg_text |
  6. +------------------------------+----------+----------+----------+
  7. | david_test.test1 | optimize | status | OK |
  8. +------------------------------+----------+----------+----------+
  9. 1 row in set (0.26 sec)
3、在SHELL中调用
  1. #!/bin/sh
  2. #
  3. # Created by david yeung.
  4. # Optimize all the table one by one.
  5. #
  6. cd /usr/local/mysql/bin
  7. ./mysql -uoptimize_user -poptimize -e "use \"$1\";call mysql.sp_optimize_tables(\"$1\")"
  8. 不过要注意给optimize_user的select,insert权限。
二、碎片整理的shell脚本
  1. #!/bin/sh
  2.     mysql_user=root
  3.     mysql_pass=123123
  4.     time_log=/opt/time
  5.     databases=/opt/databases
  6.     /usr/bin/mysql -u$mysql_user -p$mysql_pass -e "show databases" | grep -v "Database" > /opt/databases
  7.     sed -i "s/information_schema//" $databases
  8.     sed -i "s/mysql//" $databases
  9.     sed -i "s/test//" $databases
  10.     databases1=$(cat /opt/databases)
  11.     for i in $databases1
  12.     do
  13.     echo "database $i starting"
  14.     tables=$(/usr/bin/mysql $i -u$mysql_user -p$mysql_pass -e "show tables" | grep -v "Tables" > /opt/$i)
  15.     tablelist=$(cat /opt/$i)
  16.     echo "optimize database $i starting" >> $time_log
  17.     echo "$i start at $(date +[%Y/%m/%d/%H:%M:%S])" >> $time_log
  18.     for list in $tablelist
  19.     do
  20.     echo $list
  21.     /usr/bin/mysql $i -u$mysql_user -p$mysql_pass -e "optimize table $list"
  22.     done
  23.     echo "$i end at $(date +[%Y/%m/%d/%H:%M:%S])" >> $time_log
  24.     echo >> $time_log
  25.     done


阅读(3820) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~