OPTIMIZE TABLE linked list 表优化原理 链表数据结构 空间再利用
小结:
1、加快读写;
2、对于InnoDB表,在一定条件下,通过复制旧表重建;
3、实践中,
3.1、show processlist;查看线程,发现,认为堵塞读请求;
3.2、数据长度空间不变,索引长度空间减小;历史的写操作是:url访问统计表,当天多次更新当天聚合结果数据的逻辑是“当天数据删除,再插入”(url-pv);
MySQL :: MySQL 8.0 Reference Manual :: 13.7.3.4 OPTIMIZE TABLE Syntax https://dev.mysql.com/doc/refman/8.0/en/optimize-table.html
13.7.3.4 OPTIMIZE TABLE Syntax
OPTIMIZE [NO_WRITE_TO_BINLOG | LOCAL]
TABLE tbl_name [, tbl_name] ...
OPTIMIZE TABLE
reorganizes the physical storage of table data and associated index data, to reduce storage space and improve I/O efficiency when accessing the table. The exact changes made to each table depend on the storage engine used by that table.
Use OPTIMIZE TABLE
in these cases, depending on the type of table:
After doing substantial insert, update, or delete operations on an
InnoDB
table that has its own .ibd file because it was created with theinnodb_file_per_table
option enabled. The table and indexes are reorganized, and disk space can be reclaimed for use by the operating system.After doing substantial insert, update, or delete operations on columns that are part of a
FULLTEXT
index in anInnoDB
table. Set the configuration optioninnodb_optimize_fulltext_only=1
first. To keep the index maintenance period to a reasonable time, set theinnodb_ft_num_word_optimize
option to specify how many words to update in the search index, and run a sequence ofOPTIMIZE TABLE
statements until the search index is fully updated.After deleting a large part of a
MyISAM
orARCHIVE
table, or making many changes to aMyISAM
orARCHIVE
table with variable-length rows (tables that haveVARCHAR
,VARBINARY
,BLOB
, orTEXT
columns). Deleted rows are maintained in a linked list and subsequentINSERT
operations reuse old row positions. You can useOPTIMIZE TABLE
to reclaim the unused space and to defragment the data file. After extensive changes to a table, this statement may also improve performance of statements that use the table, sometimes significantly.
This statement requires SELECT
and INSERT
privileges for the table.
OPTIMIZE TABLE
works for InnoDB
, MyISAM
, and ARCHIVE
tables. OPTIMIZE TABLE
is also supported for dynamic columns of in-memory NDB
tables. It does not work for fixed-width columns of in-memory tables, nor does it work for Disk Data tables. The performance of OPTIMIZE
on NDB Cluster tables can be tuned using --ndb_optimization_delay
, which controls the length of time to wait between processing batches of rows by OPTIMIZE TABLE
. For more information, see Previous NDB Cluster Issues Resolved in NDB Cluster 7.3.
For NDB Cluster tables, OPTIMIZE TABLE
can be interrupted by (for example) killing the SQL thread performing the OPTIMIZE
operation.
By default, OPTIMIZE TABLE
does not work for tables created using any other storage engine and returns a result indicating this lack of support. You can make OPTIMIZE TABLE
work for other storage engines by starting mysqld with the --skip-new
option. In this case, OPTIMIZE TABLE
is just mapped to ALTER TABLE
.
This statement does not work with views.
OPTIMIZE TABLE
is supported for partitioned tables. For information about using this statement with partitioned tables and table partitions, see Section 23.3.4, “Maintenance of Partitions”.
By default, the server writes OPTIMIZE TABLE
statements to the binary log so that they replicate to replication slaves. To suppress logging, specify the optional NO_WRITE_TO_BINLOG
keyword or its alias LOCAL
.
OPTIMIZE TABLE Output
OPTIMIZE TABLE
returns a result set with the columns shown in the following table.
Column | Value |
---|---|
Table |
The table name |
Op |
Always optimize |
Msg_type |
status , error , info , note , or warning |
Msg_text |
An informational message |
OPTIMIZE TABLE
table catches and throws any errors that occur while copying table statistics from the old file to the newly created file. For example. if the user ID of the owner of the .MYD
or .MYI
file is different from the user ID of the mysqld process, OPTIMIZE TABLE
generates a "cannot change ownership of the file" error unless mysqld is started by the root
user.
InnoDB Details
For InnoDB
tables, OPTIMIZE TABLE
is mapped to ALTER TABLE ... FORCE
, which rebuilds the table to update index statistics and free unused space in the clustered index. This is displayed in the output of OPTIMIZE TABLE
when you run it on an InnoDB
table, as shown here:
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 |
+----------+----------+----------+-------------------------------------------------------------------+
OPTIMIZE TABLE
uses online DDL for regular and partitioned InnoDB
tables, which reduces downtime for concurrent DML operations. The table rebuild triggered by OPTIMIZE TABLE
and performed under the cover by ALTER TABLE ... FORCE
is completed in place. An exclusive table lock is only taken briefly during the prepare phase and the commit phase of the operation. During the prepare phase, metadata is updated and an intermediate table is created. During the commit phase, table metadata changes are committed.
OPTIMIZE TABLE
rebuilds the table using the table copy method under the following conditions:
When the
old_alter_table
system variable is enabled.When the mysqld
--skip-new
option is enabled.
OPTIMIZE TABLE
using online DDL is not supported for InnoDB
tables that contain FULLTEXT
indexes. The table copy method is used instead.
InnoDB
stores data using a page-allocation method and does not suffer from fragmentation in the same way that legacy storage engines (such as MyISAM
) will. When considering whether or not to run optimize, consider the workload of transactions that your server will process:
Some level of fragmentation is expected.
InnoDB
only fills pages 93% full, to leave room for updates without having to split pages.Delete operations might leave gaps that leave pages less filled than desired, which could make it worthwhile to optimize the table.
Updates to rows usually rewrite the data within the same page, depending on the data type and row format, when sufficient space is available. See Section 15.9.1.5, “How Compression Works for InnoDB Tables” and Section 15.10, “InnoDB Row Formats”.
High-concurrency workloads might leave gaps in indexes over time, as
InnoDB
retains multiple versions of the same data due through itsMVCC mechanism. See Section 15.3, “InnoDB Multi-Versioning”.
MyISAM Details
For MyISAM
tables, OPTIMIZE TABLE
works as follows:
If the table has deleted or split rows, repair the table.
If the index pages are not sorted, sort them.
If the table's statistics are not up to date (and the repair could not be accomplished by sorting the index), update them.
Other Considerations
OPTIMIZE TABLE
is performed online for regular and partitioned InnoDB
tables. Otherwise, MySQL locks the table during the time OPTIMIZE TABLE
is running.
OPTIMIZE TABLE
does not sort R-tree indexes, such as spatial indexes on POINT
columns. (Bug #23578)
实践
show processlist;
statistics_visit Query 1238 altering table OPTIMIZE TABLE `test`
community Sleep 56
community Sleep 20
statistics_visit Sleep 158
statistics_visit Sleep 155
statistics_visit Query 0 init show processlist
statistics_visit Execute 104 Creating sort index SELECT address,pv,modify_time,ip FROM test WHERE uid=50016942 ORDER BY modify_time DESC LIMIT
statistics_visit Execute 104 Sending data SELECT SUM(pv) AS c FROM test WHERE uid=50016942 UNION ALL SELECT SUM(pv) AS c FROM st
statistics_visit Execute 96 Sending data SELECT SUM(pv) AS c FROM test WHERE uid=50016942
statistics_visit Execute 96 Creating sort index SELECT address,pv,modify_time,ip FROM test WHERE uid=50016942 ORDER BY modify_time DESC LIMIT
statistics_visit Execute 41 Sending data SELECT SUM(pv) AS c FROM test WHERE uid=50015592
statistics_visit Execute 40 Creating sort index SELECT address,pv,modify_time,ip FROM test WHERE uid=50015592 ORDER BY modify_time DESC LIMIT
statistics_visit Execute 35 Creating sort index SELECT address,pv,modify_time,ip FROM test WHERE uid=50017425 ORDER BY modify_time DESC LIMIT
statistics_visit Execute 35 Sending data SELECT SUM(pv) AS c FROM test WHERE uid=50017425 UNION ALL SELECT SUM(pv) AS c FROM st
statistics_visit Execute 34 Creating sort index SELECT address,pv,modify_time,ip FROM test WHERE uid=50015592 ORDER BY modify_time DESC LIMIT
statistics_visit Execute 33 Sending data SELECT SUM(pv) AS c FROM test WHERE uid=50015592 UNION ALL SELECT SUM(pv) AS c FROM st
statistics_visit Execute 18 Sending data SELECT SUM(pv) AS c FROM test WHERE uid=50017828
statistics_visit Execute 18 Creating sort index SELECT address,pv,modify_time,ip FROM test WHERE uid=50017828 ORDER BY modify_time DESC LIMIT
优化前
优化后
优化结果语句
Table does not support optimize, doing recreate + analyze instead
在优化过程中,只有读查询;
数据长度空间不变,索引长度空间由7.63GB减少至5.25GB;
OPTIMIZE TABLE linked list 表优化原理 链表数据结构 空间再利用的更多相关文章
- 使用OPTIMIZE TABLE命令来整理表碎片实践
操作环境:ubuntu 14.10 mysql 5.6.25 对含有BLOB或TEXT字段的表,若经常做修改或删除类的操作,需要定期执行OPTIMIZE TABLE命令来整理碎片. 1.creat ...
- OPTIMIZE TABLE 小解
首先看一下语法: OPTIMIZE [NO_WRITE_TO_BINLOG | LOCAL] TABLE tbl_name [, tbl_name] ... 我们知道mysql存储引擎里面的数据和索 ...
- OPTIMIZE TABLE的作用--转载
当您的库中删除了大量的数据后,您可能会发现数据文件尺寸并没有减小.这是因为删 除操作后在数据文件中留下碎片所致.Discuz! 在系统数设置界面提供了数据表优化的功能,可以去除删除操作后留下的数据文件 ...
- mysql optimize table
mysql 数据文件的使用是只扩展,不回收.对表执行delete之后,磁盘上数据文件是不会缩小的. 通常的做法,是先逻辑导出,然后truncate 原表(或者删除重建),再导入. 另外还有一种方法是o ...
- 【巨杉数据库SequoiaDB】巨杉Tech | 分布式数据库千亿级超大表优化实践
01 引言 随着用户的增长.业务的发展,大型企业用户的业务系统的数据量越来越大,超大数据表的性能问题成为阻碍业务功能实现的一大障碍.其中,流水表作为最常见的一类超大表,是企业级用户经常碰到的性能瓶颈. ...
- MySQL表的碎片整理和空间回收小结
MySQL表碎片化(Table Fragmentation)的原因 关于MySQL中表碎片化(Table Fragmentation)产生的原因,简单总结一下,MySQL Engine不同,碎片化的原 ...
- 表优化 altering table OPTIMIZE TABLE `sta_addr_copy`
表优化 altering table OPTIMIZE TABLE `sta_addr_copy` [总结] 1.实际测试的结果是,在state sqlaltering table OPTIMIZE ...
- mysql下优化表和修复表命令使用说明(REPAIR TABLE和OPTIMIZE TABLE)
随着mysql的长期使用,可以修复表来优化,优化时减少磁盘占用空间.方便备份. REPAIR TABLE `table_name` 修复表 OPTIMIZE TABLE `table_name` 优化 ...
- optimize table 表优化问题
语法: optimize table '表名' 一,原始数据 1,数据量 2,存放在硬盘中的表文件大小 3,查看一下索引信息 索引信息中的列的信息说明. Table :表的名称.Non_unique: ...
随机推荐
- o2o的关健在于线下!
其实,说到底,O2O不是那么简单的.一堆空降兵和一群自以为是的风投都把所有的线下产业想简单了.没有真正的去了解和体验这个产业,就动不动一个小点子,创新一个小平台就妄想能改变或替代中国某某产业.看看这几 ...
- css自适应浏览器大小
css自适应浏览器大小 1.屏幕 > 900px :显示3列 2.450px < 屏幕 < 900px :显示2列 3.屏幕 < 450px :显示1列 <!DOCTYP ...
- [k8s]通过openssl生成证书
证书认证原理: http://www.cnblogs.com/iiiiher/p/7873737.html [root@m1 ssl]# cat master_ssl.cnf [req] req_ex ...
- IOS开发之Storyboard应用
制作一个Tab类型的应用 制作一个表格视图 原型表格单元 设计自定义的原型单元格 为原型单元格设置子类 故事版(Storyboard)是一个能够节省你很多设计手机App界面时间的新特性,下面,为了简明 ...
- TP支持菜单动态生成RBAC权限系统数据库结构设计方案
最简单基于RBAC权限系统数据库结构设计 包括如下几个表 1. 用户表 -- Table "t_user" DDL CREATE TABLE `t_user` ( `id` int ...
- 【Unity】ShareSDK、SMSSDK的基本使用与常见问题
概要 测试使用ShareSDK的一些常用功能.包括: 用微博帐号做第三方登录 获取用户的帐号详细信息 获取好友列表 分享功能 测试使用SMSSDK插件,包括: 导入插件,解决包冲突 短信登录功能:发验 ...
- java 中使用log4j
一.控制台使用 1.导入log4j包到工程中 2.配置: log4j.rootLogger=DEBUG,console,R log4j.appender.console=org.apache.log4 ...
- Java知多少(65)线程的挂起、恢复和终止
有时,线程的挂起是很有用的.例如,一个独立的线程可以用来显示当日的时间.如果用户不希望用时钟,线程被挂起.在任何情形下,挂起线程是很简单的,一旦挂起,重新启动线程也是一件简单的事. 挂起,终止和恢复线 ...
- Redis数据库高级实用特性:持久化机制
Redis数据库高级实用特性:持久化机制 大型web系统数据缓存设计 Redis高级特性:虚拟内存的使用技巧 Redis高级实用特性:安全性与主从复制 Memcached.Redis OR Tair
- HashMap,Hashtable,ConcurrentHashMap 和 synchronized Map 的原理和区别
HashMap 是否是线程安全的,如何在线程安全的前提下使用 HashMap,其实也就是HashMap,Hashtable,ConcurrentHashMap 和 synchronized Map 的 ...