`
LJ你是唯一LT
  • 浏览: 238705 次
社区版块
存档分类
最新评论

mysql optimize 清理碎片

阅读更多
---定期清理脚本
0 1  * * 4 root /root/qingli_mysql.sh
[root@newmysql5 ~]# cat qingli_mysql.sh
#!/bin/bash
date=`date +"%Y-%m-%d %H:%M:%S"`
echo $date >>/root/qingli.log
tables=$(mysql -u root -p"tina" 2>/dev/null -e "select concat(table_schema,'.',table_name) from information_schema.tables where data_free>0 and engine !='MEMORY';" |grep -v "concat")

for table in $tables
do
  mysql -u root -p"****" 2>/dev/null -e "optimize table $table;" >>/root/qingli.log
done
----


1、清理mysql碎片
查询存在碎片的表和碎片的大小:
mysql>select concat('optimize table ',table_schema,'.',table_name,';'),data_free,engine from information_schema.tables where data_free>0 and engine !='MEMORY';
+-----------------------------------------------------------+-----------+--------+
| concat('optimize table ',table_schema,'.',table_name,';') | data_free | engine |
+-----------------------------------------------------------+-----------+--------+
| optimize table 117demo.scan_url;                          |   5242880 | InnoDB |
| optimize table antiyfeature.basic_csv_file;               |   4194304 | InnoDB |
| optimize table antiyfeature.gen_avl_entry;                |   4194304 | InnoDB |
| optimize table antiyfeature.sample_info;                  |   4194304 | InnoDB |
| optimize table avlyun_googleplay.app_package;             |   7340032 | InnoDB |
| optimize table avlyun_googleplay.app_update;              | 376438784 | InnoDB |
| optimize table enginedn.ads_opc_avl;                      |   4194304 | InnoDB |
| optimize table enginedn.avl_info;                         |   4194304 | InnoDB |
| optimize table enginedn.basic_avl_info;                   |   4194304 | InnoDB |
| optimize table enginedn.basic_csv_file;                   |   4194304 | InnoDB |
| optimize table enginedn.gen_change_record;                |   4194304 | InnoDB |
| optimize table enginedn.opc_avl_info;                     |   4194304 | InnoDB |
| optimize table enginedn.package_channel;                  |   4194304 | InnoDB |
| optimize table enginedn.package_info;                     |   4194304 | InnoDB |
| optimize table enginedn.scdf_avl_info;                    |   4194304 | InnoDB |
| optimize table enginedn.sign_avl_info;                    |   4194304 | InnoDB |
| optimize table mobile_event.program_url;                  |   4194304 | InnoDB |
| optimize table mobile_event.sample;                       |   4194304 | InnoDB |
| optimize table mobile_event.sample_url;                   |   4194304 | InnoDB |
| optimize table mobile_event.virus_url;                    |   4194304 | InnoDB |
| optimize table mysql.innodb_index_stats;                  |   4194304 | InnoDB |
| optimize table sohu.detail_sohu;                          |   7340032 | InnoDB |
+-----------------------------------------------------------+-----------+--------+
23 rows in set (0.13 sec)   --共有39个表有碎片,较小的已经提前清理,剩下的需要在空闲时间清理,预计耗时1h30min,可放在凌晨执行~
清理步骤:
执行命令optimize table  table_name;

注意:OPTIMIZE TABLE只对MyISAM, BDB和InnoDB表起作用。在OPTIMIZE TABLE运行过程中,MySQL会锁定表。
即使您对可变长度的行进行了大量的更新,您也不需要经常运行,每周一次或每月一次即可,只对特定的表运行。

| optimize table antiy_bbs.bbs_common_session;              |      2492 | MEMORY |    --不支持这种格式
mysql> optimize table antiy_bbs.bbs_common_session;
+------------------------------+----------+----------+-----------------------------------------------------------+
| Table                        | Op       | Msg_type | Msg_text                                                  |
+------------------------------+----------+----------+-----------------------------------------------------------+
| antiy_bbs.bbs_common_session | optimize | note     | The storage engine for the table doesn't support optimize |
+------------------------------+----------+----------+-----------------------------------------------------------+
| optimize table sohu.basic_sohu;                           |   3145728 | InnoDB |   --3M耗时2min47s

--出现这种,就表示已经清理了
mysql> optimize table antiy_bbs.bbs_ucenter_newpm;
+-----------------------------+----------+----------+-----------------------------+
| Table                       | Op       | Msg_type | Msg_text                    |
+-----------------------------+----------+----------+-----------------------------+
| antiy_bbs.bbs_ucenter_newpm | optimize | status   | Table is already up to date |
+-----------------------------+----------+----------+-----------------------------+
1 row in set (0.25 sec)


MYSQL的文档说明了,当INNODB时,MYSQL会以ALTER TABLE去执行这个命令。 所以最终还是会看到 OK 的状态。
mysql> OPTIMIZE TABLE foo;
+----------+----------+----------+-------------------------------------------------------------------+
| Table    | Op       | Msg_type | Msg_text                                                          |
+----------+----------+----------+-------------------------------------------------------------------+
| test.foo | optimize | note     | Table does not support optimize, doing recreate + analyze instead |
| test.foo | optimize | status   | OK                                                                |
+----------+----------+----------+-------------------------------------------------------------------+






////////////////////////////////////
注意:生产上不要随便操作,因为会锁表。

mysql> show index from basic_sohu from sohu;
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table      | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| basic_sohu |          0 | PRIMARY  |            1 | id          | A         |        8764 |     NULL | NULL   |      | BTREE      |         |               |
+------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)



mysql中OPTIMIZE TABLE的作用 (2009-04-01 17:44:39)转载▼
标签: 杂谈 分类: 工作
1、先来看看多次删除插入操作后的表索引情况
mysql> SHOW INDEX FROM `tbl_name`;
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| tbl_name | 0 | PRIMARY | 1 | StepID | A | 1 | NULL | NULL | | BTREE | |
| tbl_name | 1 | FlowID | 1 | FlowID | A | 1 | NULL | NULL | | BTREE | |
| tbl_name | 1 | WagerCount | 1 | WagerCount | A | 1 | NULL | NULL | | BTREE | |
| tbl_name | 1 | WagerID_3 | 1 | WagerID | A | 1 | NULL | NULL | | BTREE | |
| tbl_name | 1 | WagerID_3 | 2 | StepType | A | 1 | NULL | NULL | | BTREE | |
| tbl_name | 1 | WagerID_3 | 3 | ParamResult | A | 1 | 255 | NULL | | BTREE | |
| tbl_name | 1 | StepType_2 | 1 | StepType | A | 1 | NULL | NULL | | BTREE | |
| tbl_name | 1 | StepType_2 | 2 | ParamResult | A | 1 | 255 | NULL | | BTREE | |
| tbl_name | 1 | WagerID_2 | 1 | WagerID | A | 1 | NULL | NULL | | BTREE | |
| tbl_name | 1 | WagerID_2 | 2 | StepType | A | 1 | NULL | NULL | | BTREE | |
| tbl_name | 1 | WagerID_2 | 3 | ParamResult | A | 1 | 255 | NULL | | BTREE | |
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
11 rows in set (0.01 sec)
2、优化表
mysql> optimize table tbl_name;
+---------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+---------------+----------+----------+----------+
| test.tbl_name | optimize | status | OK |
+---------------+----------+----------+----------+
1 row in set (40.60 sec)
3、再来看看优化后的效果
mysql> SHOW INDEX FROM `tbl_name`;
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| tbl_name | 0 | PRIMARY | 1 | StepID | A | 172462 | NULL | NULL | | BTREE | |
| tbl_name | 1 | FlowID | 1 | FlowID | A | 86231 | NULL | NULL | | BTREE | |
| tbl_name | 1 | WagerCount | 1 | WagerCount | A | 4311 | NULL | NULL | | BTREE | |
| tbl_name | 1 | WagerID_3 | 1 | WagerID | A | 86231 | NULL | NULL | | BTREE | |
| tbl_name | 1 | WagerID_3 | 2 | StepType | A | 172462 | NULL | NULL | | BTREE | |
| tbl_name | 1 | WagerID_3 | 3 | ParamResult | A | 172462 | 255 | NULL | | BTREE | |
| tbl_name | 1 | StepType_2 | 1 | StepType | A | 9 | NULL | NULL | | BTREE | |
| tbl_name | 1 | StepType_2 | 2 | ParamResult | A | 86231 | 255 | NULL | | BTREE | |
| tbl_name | 1 | WagerID_2 | 1 | WagerID | A | 86231 | NULL | NULL | | BTREE | |
| tbl_name | 1 | WagerID_2 | 2 | StepType | A | 172462 | NULL | NULL | | BTREE | |
| tbl_name | 1 | WagerID_2 | 3 | ParamResult | A | 172462 | 255 | NULL | | BTREE | |
+----------+------------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
最后,来看看手册中关于 OPTIMIZE 的描述:
OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_name [, tbl_name] ...

如果您已经删除了表的一大部分,或者如果您已经对含有可变长度行的表(含有VARCHAR, BLOB或TEXT列的表)进行了很多更改,则应使用
OPTIMIZE TABLE。被删除的记录被保持在链接清单中,后续的INSERT操作会重新使用旧的记录位置。您可以使用OPTIMIZE TABLE来重新
利用未使用的空间,并整理数据文件的碎片。

在多数的设置中,您根本不需要运行OPTIMIZE TABLE。即使您对可变长度的行进行了大量的更新,您也不需要经常运行,每周一次或每月一次
即可,只对特定的表运行。

OPTIMIZE TABLE只对MyISAM, BDB和InnoDB表起作用。

注意,在OPTIMIZE TABLE运行过程中,MySQL会锁定表。
分享到:
评论

相关推荐

    解析mysql 表中的碎片产生原因以及清理

    大量删除数据必然会在数据文件中造成不连续的空白空间,而当插入数据时,这些空白空间则会被利用起来 。对于不同的存储引擎整理碎片的方式不一样。...因为在中间删除,所以留下了空白mysql> optimize table

    探讨Mysql中OPTIMIZE TABLE的作用详解

    本篇文章是对Mysql中OPTIMIZE TABLE的作用进行了详细的分析介绍,需要的朋友参考下

    mysql-optimize

    mysql-optimize 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引。 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描, ...

    WP-Optimize插件

    WordPress上每次新建页面或文章时,有时免不了要修改这些页面的内容,假如你多次修改文章,那么,WordPress就会产生一些冗余的数据,就会占据你的MySQL空间,例如:你的一篇文章占用100KB数据库空间,你修改5次的话...

    MYSQL

    7.6 DROP DATABASE (抛弃数据库)句法 7.7 CREATE TABLE (创建表)句法 7.7.1 隐含(silent)的列指定变化 7.8 ALTER TABLE (改变表)句法 7.9 OPTIMIZE TABLE (优化表) 句法 7.10 DROP TABLE ...

    MySQL中文参考手册.chm

    7.9 OPTIMIZE TABLE (优化表) 句法 7.10 DROP TABLE (抛弃表)句法 7.11 DELETE (删除)句法 7.12 SELECT (精选)句法 7.13 JOIN (联接)句法 7.14 INSERT (插入)句法 7.15 REPLACE ...

    System Optimize Tools V1.0.1

    System Optimize Tools V1.0.1 System Optimize Tools V1.0.1 System Optimize Tools V1.0.1 System Optimize Tools V1.0.1 System Optimize Tools V1.0.1 System Optimize Tools V1.0.1 System Optimize Tools V...

    实现MySQL定时批量检查表repair和优化表optimize table的shell脚本

    本文介绍mysql定时批量检查表repair和优化表optimize table的shell脚本,对于MySQL数据库的定期维护相当有用!如下所示: #!/bin/bash host_name=192.168.0.123 user_name=jincon.com user_pwd=jincon.com database...

    Guide to Optimizing Performance of the MySQL Cluster Database

    This guide explores how to tune and optimize the MySQL Cluster database to handle diverse workload requirements. It discusses data access patterns and how to build distribution awareness into ...

    MySQL中文参考手册

    * 1 MySQL的一般的信息 o 1.1 什么是MySQL? o 1.2 关于本手册 + 1.2.1 本手册中使用的约定 o 1.3 MySQL的历史 o 1.4 MySQL的主要特征 o 1.5 MySQL稳定性? o 1.6 顺应2000年 o 1.7 SQL一般信息和教程 o ...

    mysql下优化表和修复表命令使用说明(REPAIR TABLE和OPTIMIZE TABLE)

    随着mysql的长期使用,肯定会出现一些问题,一般情况下mysql表无法访问,就可以修复表了,优化时减少磁盘占用空间。方便备份。

    Devart dbForge Studio for MySQL Professional Edition v7.1.13

    The tool helps you detect problems and optimize SQL queries via GUI. Profiler: Offers visual query profiling Compares profiling results More on query profiling Report and Analysis Data ...

    Pro.MySQL.NDB.Cluster

    Along the way, you will learn to monitor your cluster, make decisions about schema design, implement geographic replication, troubleshoot and optimize performance, and much more. This book covers ...

    SSD Optimize v1.1 Build 823

    SSD Optimize v1.1 Build 823. SSD盘优化工具

    High Performance MySQL_3rd_edition

    Optimize advanced querying features, such as full-text searches Take advantage of modern multi-core CPUs and solid-state disks Explore backup and recovery strategies—including new tools for hot on...

    Laravel开发-laravel-optimize-images

    Laravel开发-laravel-optimize-images Laravel 4的优化图像命令

    前端开源库-node-optimize

    前端开源库-node-optimize节点优化,将一个节点项目优化为一个JS文件,以供发布。

Global site tag (gtag.js) - Google Analytics