如何在删除一些行后优化 MySQL 表?
使用 OPTIMIZE TABLE 命令优化 MySQL 表 −
optimize table yourTableName;
我们先创建一个表格 −
mysql> create table DemoTable( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(100) ); Query OK, 0 rows affected (1.38 sec)
使用 insert 命令在表格中插入一些记录 −
mysql> insert into DemoTable(Name) values('Chris'); Query OK, 1 row affected (0.27 sec) mysql> insert into DemoTable(Name) values('Robert'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Name) values('Bob'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(Name) values('David'); Query OK, 1 row affected (0.38 sec)
使用 select 语句显示表格中的所有记录 −
mysql> select *from DemoTable;
将产生以下输出 −
+----+--------+ | Id | Name | +----+--------+ | 1 | Chris | | 2 | Robert | | 3 | Bob | | 4 | David | +----+--------+ 4 rows in set (0.00 sec)
现在让我们从表格中删除行 −
mysql> delete from DemoTable where Id IN(1,3); Query OK, 2 rows affected (0.29 sec) mysql> select *from DemoTable; +----+--------+ | Id | Name | +----+--------+ | 2 | Robert | | 4 | David | +----+--------+ 2 rows in set (0.00 sec)
以下是优化在删除一些行之后创建的表格的查询 −
mysql> optimize table DemoTable;
将产生以下输出 −
+------------------+----------+----------+-------------------------------------------------------------------+ | Table | Op | Msg_type | Msg_text | +------------------+----------+----------+-------------------------------------------------------------------+ | web.DemoTable | optimize | note | Table does not support optimize, doing recreate + analyze instead | | web.DemoTable | optimize | status | OK | +------------------+----------+----------+-------------------------------------------------------------------+ 2 rows in set (4.32 sec)
广告