Home > Database > Mysql Tutorial > body text

MySQL中optimize优化表_MySQL

WBOY
Release: 2016-06-08 08:50:34
Original
1068 people have browsed it

前言

当对MySQL进行大量的增删改操作的时候,很容易产生一些碎片,这些碎片占据着空间,所以可能会出现删除很多数据后,数据文件大小变化不大的现象。当然新插入的数据仍然会利用这些碎片。但过多的碎片,对数据的插入操作是有一定影响的,此时,我们可以通过optimize来对表的优化。

为了更加直观的看到数据碎片,Mysql可以使用如下命令查看

show table status [like table_name]

如下图

 

这里写图片描述

 

data_free选项代表数据碎片。

针对MySQL的不同数据库存储引擎,在optimize使用清除碎片,回收闲置的数据库空间,把分散存储(fragmented)的数据和索引重新挪到一起(defragmentation),对I/O速度有好处。

当然optimize在对表进行操作的时候,会加锁,所以不宜经常在程序中调用。

MyISAM存储引擎

针对MyISAM表,直接使用如下命令进行优化

optimize table table1[,table2][,table3]

如果同时优化多个表可以使用逗号分隔。

下面优化dede_member_vhistory表,可以看出,优化后data_free值为0。

 

这里写图片描述

 

#InnoDB存储引擎

InnoDB引擎的表分为独享表空间和同享表空间的表,我们可以通过show variables like ‘innodb_file_per_table’;来查看是否开启独享表空间。

我本地是开启了独享表空间的。此时是无法对表进行optimize操作的,如果操作,会返回如图信息,最后的一条Table does not support optimize, doing recreate + analyze instead。因为该结构下删除了大量的行,此时索引会重组并且会释放相应的空间因此不必优化。

 

这里写图片描述
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!