Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4180242
  • 博文数量: 240
  • 博客积分: 11504
  • 博客等级: 上将
  • 技术积分: 4277
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-28 14:24
文章分类

全部博文(240)

分类: Mysql/postgreSQL

2008-01-28 13:40:44

这个存储过程目的是给一个库的所有表来整理碎片的。一个表随着插入很频繁,或者一直更新不停的,就会积累好多碎片。如果及时整理一下,查询效率会高出好多。
不过要注意对数据巨大的表整理的时间可能非常的长。
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权限。

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

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