本站电脑知识提供应用软件知识,计算机网络软件知识,计算机系统工具知识,电脑配置知识,电脑故障排除和电脑常识大全,帮助您更好的学习电脑!不为别的,只因有共同的爱好,为软件和互联网发展出一分力! 起因:查看线上数据库中Table Information时发现有一个日志表数据大小和索引大小有915M,但实际行数只有92行。该表需要频繁插入并且会定时去删掉旧的记录。表类型为Myisam,已建立一个索引,所以应该是产生了大量碎片,使用 Optimize table 表名 优化后大小变为2.19M,少了很多, 同时可以看出该表上的索引建的多余,因为插入操作比查询操作要多很多,而且查询不多,查询的数据量也一般比较小。 ![]() 借此延伸下MYSQL中Myisam、InnoDB碎片优化方式: Myisam清理碎片 InnoDB碎片优化 If you’re not using the innodb_file_per_table option, the only thing you can do about it is export and import the database, a time-and-disk-intensive procedure. But if you are using innodb_file_per_table, you can identify and reclaim this space! Prior to 5.1.21, the free space counter is available from the table_comment column of information_schema.tables. Here is some SQL to identify tables with at least 100M (actually 97.65M) of free space: SELECT table_schema, table_name, table_comment FROM information_schema.tables WHERE engine LIKE ‘InnoDB’ AND table_comment RLIKE ‘InnoDB free: ([0-9]{6,}).*‘; Starting with 5.1.21, this was moved to the data_free column (a much more appropriate place): SELECT table_schema, table_name, data_free/1024/1024 AS data_free_MB FROM information_schema.tables WHERE engine LIKE ‘InnoDB’ AND data_free > 100*1024*1024; You can reclaim the lost space by rebuilding the table. The best way to do this is using ‘alter table’ without actually changing anything: ALTER TABLE foo ENGINE=InnoDB; This is what MySQL does behind the scenes if you run ‘optimize table’ on an InnoDB table. It will result in a read lock, but not a full table lock. How long it takes is completely dependent on the amount of data in the table (but not the size of the data file). If you have a table with a high volume of deletes or updates, you may want to run this monthly, or even weekly. 什么是mysql碎片?怎样知道表的碎片有多大呢? OPTIMIZE 操作会暂时锁住表,而且数据量越大,耗费的时间也越长,它毕竟不是简单查询操作.所以把 Optimize 命令放在程序中是不妥当的,不管设置的命中率多低,当访问量增大的时候,整体命中率也会上升,这样肯定会对程序的运行效率造成很大影响.比较好的方式就是做个 Script,定期检查mysql中 information_schema.TABLES字段,查看 DATA_FREE 字段,大于0话,就表示有碎片.脚本多长时间运行一次,可以根据实际情况来定,比如每周跑一次. 我们在使用MySQL的时候,每当我们从表中删除一个数据的时候,都会让这段空间滞空。如果在短时间内删除大量的数据,那么这些留出来的空间比数据库中保留的数据所使用的空间还要大。虽然在mysql插入新的数据时候,mysql会尽最大的可能使用这些空间,但是依然是无法全部重新利用的,所以学会mysql碎片清理是很有用处的 mysql> select table_schema, table_name, data_free, engine ? ? from information_schema.tables where table_schema not in ('information_schema', 'mysql') and data_free > 0; ? 学习教程快速掌握从入门到精通的电脑知识。 |
温馨提示:喜欢本站的话,请收藏一下本站!