
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to optimize a MySQL table after deleting some rows?
Use the OPTIMIZE TABLE command to optimize a MySQL table −
optimize table yourTableName;
Let us first create a table −
mysql> create table DemoTable( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(100) ); Query OK, 0 rows affected (1.38 sec)
Insert some records in the table using insert command −
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)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+----+--------+ | Id | Name | +----+--------+ | 1 | Chris | | 2 | Robert | | 3 | Bob | | 4 | David | +----+--------+ 4 rows in set (0.00 sec)
Now let us delete rows from the table −
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)
Following is the query to optimize the table created above and after deleting some rows −
mysql> optimize table DemoTable;
This will produce the following output −
+------------------+----------+----------+-------------------------------------------------------------------+ | 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)
- Related Articles
- Reorder keys after deleting a record from MySQL table?
- How to auto increment with 1 after deleting data from a MySQL table?
- How to optimize many SELECTs in a single table in MySQL?
- How to add some rows to a table through DefaultTableModel in Java
- Selecting and displaying only some rows from a column in a MySQL table
- Delete only some rows from a table based on a condition in MySQL
- How to delete all rows except some in MySQL?
- How to replace rows in a MySQL table with conditions?
- Deleting all rows older than 5 days in MySQL
- How to select only 3 ordered rows on a MySQL table?
- Copy all rows of a table to another table in MySQL?
- Need help in deleting duplicate columns from a table in MySQL?
- How can we delete multiple rows from a MySQL table?
- How can we delete all rows from a MySQL table?
- Check how many rows are in a MySQL database table?

Advertisements