Optimize the stored procedure for each table in the database in mysql _ MySQL

Source: Internet
Author: User
The stored procedure bitsCN.com optimized for each table in the database in mysql

Mysql optimizes the storage process for each table in the database.

Optimize the storage process for each table in the database

Create procedure 'inventory '. 'optimize _ table' (db_name VARCHAR (64) begin declare t VARCHAR (64); DECLARE done int default 0; DECLARE c cursor for select table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = db_name AND TABLE_TYPE = 'base table'; declare continue handler for sqlstate '100' SET done = 1; OPEN c; tables_loop: loop fetch c INTO t; IF done then close c; LEAVE tables_loop; end if; SET @ stmt_text: = CONCAT ("optimize table", db_name ,'. ', t); PREPARE stmt FROM @ stmt_text; EXECUTE stmt; deallocate prepare stmt; end loop; CLOSE c; END Statement 2: create procedure 'inventory '. 'optimize _ tables2 '(db_name VARCHAR (64) begin declare t VARCHAR (64); DECLARE done int default 0; DECLARE c cursor for select table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = db_name AND TABLE_TYPE = 'base table'; declare continue handler for sqlstate '2013' SET done = 1; OPEN c; repeat fetch c INTO t; if not done then set @ stmt_text: = CONCAT ("optimize table", db_name ,'. ', t); PREPARE stmt FROM @ stmt_text; EXECUTE stmt; deallocate prepare stmt; end if; UNTIL done end repeat; CLOSE c; call optimize_tables2 ('database name') when calling END '); or call optimize_tables ('database name ');


BitsCN.com

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.