Using the Optimize table command to defragment a table fragment practice

Source: Internet
Author: User
Operating environment: Ubuntu 14.10 MySQL 5.6.25

For tables that contain BLOBs or text fields, if you frequently modify or delete classes, you will need to perform regular optimize table commands to defragment them.

1.create table T1 (id varchar (), content text) Engine=myisam, default Charset=utf8;

Storage Engine uses MyISAM

2. Inserting data

INSERT into T1 VALUES (1, repeat (' Tony ', 100)); --repeat (' Tony ', 100), return Tony Repeat 100 times after the data

INSERT into T1 values (2, repeat (' Tony ', 100));

INSERT into T1 VALUES (3, repeat (' Tony ', 100));

A good way to quickly insert large amounts of data into a table, repeat the following statement

INSERT INTO T1 select * from T1; --From T1 query out all the data, and then insert the T1 table, the data multiplied

3. View File Size

root@ubuntu2:/var/lib/mysql/tsetest# Du-sh t1.*
12K t1.frm
310M t1. MyD
4.0K t1. Myi

4. Delete data and view file size

mysql> Delete from t1 where id = 2;
Query OK, 262144 rows affected (2.04 sec)

root@ubuntu2:/var/lib/mysql/tsetest# Du-sh t1.*
12K t1.frm
310M t1. MyD
4.0K t1. Myi

The physical size of the table file is not reduced after a large amount of data has been deleted.

5. Defragment the table using the Optimize table command

mysql> optimize table T1;
+------------+----------+----------+----------+
| Table | Op | Msg_type | Msg_text |
+------------+----------+----------+----------+
| Tsetest.t1 | Optimize | Status | OK |
+------------+----------+----------+----------+
1 row in Set (2.34 sec)

root@ubuntu2:/var/lib/mysql/tsetest# Du-sh t1.*
12K t1.frm
206M t1. MyD
4.0K t1. Myi

After you use the Optimize table command to defragment the table, the table file size is reduced by nearly one-third.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.