Reclaim the space of a database table

Source: Internet
Author: User

1. Create a stored procedure

2. Get all the table names in the database first

3. Reclaim space using optimize table

4. Execute the stored procedure (call Recycle_interspace)


DELIMITER $$
CREATE PROCEDURE ' Recycle_interspace ' ()
BEGIN
DECLARE row_table_name VARCHAR (90);
DECLARE ergodic INT DEFAULT 1;
DECLARE getcategory CURSOR for SELECT table_name from information_schema.tables WHERE table_schema= ' database name ';
DECLARE CONTINUE HANDLER for not FOUND SET ergodic:=0;
OPEN getcategory;

/**
The space to begin reclaiming the table.
Use optimize table to reclaim space, but this operation will lock the table to affect the business, if the recovery is confirmed, it is recommended to operate during the business low peak period.
*/
REPEAT
FETCH getcategory into Row_table_name;
SET @csql =concat (' OPTIMIZE TABLE ', Row_table_name, '; ');
PREPARE create_stmt from @csql;
EXECUTE create_stmt;
UNTIL ergodic=0 END REPEAT;
CLOSE getcategory;
end$$

DELIMITER;

Reclaim the space of a database table

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.