批量给表整理碎片

这个存储过程目的是给一个库的所有表来整理碎片的。一个表随着插入很频繁,或者一直更新不停的,就会积累好多碎片。如果及时整理一下,查询效率会高出好多。

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 ;



调用示例:

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.test2| optimize | status   | OK       |
+--------------------------+----------+----------+----------+
1 row in set (0.35 sec)

+---------------------------------------+----------+----------+----------+
| Table                                 | Op       | Msg_type | Msg_text |
+---------------------------------------+----------+----------+----------+
| david_test.test3 | optimize | status   | OK       |
+---------------------------------------+----------+----------+----------+
1 row in set (0.45 sec)

+--------------------------+----------+----------+----------+
| Table                    | Op       | Msg_type | Msg_text |
+--------------------------+----------+----------+----------+
| david_test.test_article | optimize | status   | OK       |
+--------------------------+----------+----------+----------+
1 row in set (4.13 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.members | optimize | status   | OK       |
+-----------------------+----------+----------+----------+
1 row in set (40.02 sec)

+--------------------+----------+----------+----------+
| Table              | Op       | Msg_type | Msg_text |
+--------------------+----------+----------+----------+
| david_test.test_site | optimize | status   | OK       |
+--------------------+----------+----------+----------+
1 row in set (40.31 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)

转自http://blog.chinaunix.net/u/29134/showart_472490.html 

引用通告地址: 点击获取引用地址
标签: mysql 性能优化 数据库
评论: 21 | 引用: 0 | 阅读: 7522
更多




ernestine [ 2021-01-31 16:36 网址 | 回复 | 编辑 删除 ]
ernestine [ 2020-12-24 02:31 网址 | 回复 | 编辑 删除 ]
frank [ 2020-12-23 02:58 网址 | 回复 | 编辑 删除 ]
wilma [ 2020-07-02 21:09 网址 | 回复 | 编辑 删除 ]
ronald [ 2020-05-15 08:00 网址 | 回复 | 编辑 删除 ]
karina [ 2020-05-10 13:38 网址 | 回复 | 编辑 删除 ]
james [ 2020-04-04 11:28 网址 | 回复 | 编辑 删除 ]
david [ 2020-03-07 14:27 网址 | 回复 | 编辑 删除 ]
marianne [ 2020-03-02 01:39 网址 | 回复 | 编辑 删除 ]
ernestine [ 2020-02-18 18:07 网址 | 回复 | 编辑 删除 ]
发表评论
昵 称: 密 码:
网 址: 邮 箱:
验证码: 验证码图片 选 项:
头 像:
内 容:
  • 粗体
  • 斜体
  • 下划线
  • 插入图像
  • 超链接
  • 电子邮件
  • 插入引用