mysql recreate_does the optimize table of mysql really rebuild(recreate) table?

Demo:

I created a table on MySQL 5.6.37 and filled it with about 1GB of random data.

mysql> select version();

+-----------------+

| version() |

+-----------------+

| 5.6.37-82.2-log |

+-----------------+

mysql> show table statusG

*************************** 1. row ***************************

Name: mytable

Engine: InnoDB

Version: 10

Row_format: Compact

Rows: 61914

Avg_row_length: 20926

Data_length: 1295613952

Max_data_length: 0

Index_length: 1589248

Data_free: 4194304

Auto_increment: 131056

Create_time: 2019-02-20 15:54:23

Update_time: NULL

Check_time: NULL

Collation: utf8mb4_general_ci

Checksum: NULL

Create_options:

Comment:

I can observe the file in my datadir and note the inode number (the leftmost number).

/usr/local/var/mysql/test ls -li

total 2590752

8625784850 -rw-rw---- 1 bkarwin admin 67 Feb 20 07:54 db.opt

8625784853 -rw-rw---- 1 bkarwin admin 8580 Feb 20 07:54 mytable.frm

8625784854 -rw-rw---- 1 bkarwin admin 1321205760 Feb 20 07:56 mytable.ibd

I run optimize table.

mysql> optimize table mytable;

While it's running, I check progress by listing files in the datadir again. I see it has created a new temp file and it's filling it with a copy of the data.

Note the inode number of the temp file.

/usr/local/var/mysql/test ls -li

total 4395064

8625785082 -rw-rw---- 1 bkarwin admin 8580 Feb 20 07:57 #sql-337_16.frm

8625785083 -rw-rw---- 1 bkarwin admin 914358272 Feb 20 07:57 #sql-ib2021-3689447301.ibd

8625784850 -rw-rw---- 1 bkarwin admin 67 Feb 20 07:54 db.opt

8625784853 -rw-rw---- 1 bkarwin admin 8580 Feb 20 07:54 mytable.frm

8625784854 -rw-rw---- 1 bkarwin admin 1321205760 Feb 20 07:56 mytable.ibd

Finally my optimize table finishes. It took almost 2 minutes. If it wasn't doing a real table copy, why would it have taken so long?

+--------------+----------+----------+-------------------------------------------------------------------+

| Table | Op | Msg_type | Msg_text |

+--------------+----------+----------+-------------------------------------------------------------------+

| test.mytable | optimize | note | Table does not support optimize, doing recreate + analyze instead |

| test.mytable | optimize | status | OK |

+--------------+----------+----------+-------------------------------------------------------------------+

2 rows in set (1 min 5.52 sec)

Looking at the datadir again, I see the temp file has been renamed to be the new tablespace, and the old file has been removed. The new file has the inode number of the temp file, indicating it's the copy.

/usr/local/var/mysql/test ls -li

total 2557984

8625784850 -rw-rw---- 1 bkarwin admin 67 Feb 20 07:54 db.opt

8625785082 -rw-rw---- 1 bkarwin admin 8580 Feb 20 07:57 mytable.frm

8625785083 -rw-rw---- 1 bkarwin admin 1304428544 Feb 20 07:58 mytable.ibd

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值