Ниже приведен скрипт mysql для оптимизации базы данных MySQL. Однако, как уже говорилось ранее, это редко требуется, поскольку InnoDB сам может сделать то же самое.
USE VIPLamar;
DROP PROCEDURE IF EXISTS Lamar_Index_Rebuild_Script;
DELIMITER //
-- call Lamar_Index_Rebuild_Script();
CREATE PROCEDURE Lamar_Index_Rebuild_Script()
mysp:BEGIN
Declare v_max_counter int;
Declare v_counter int;
Declare v_query longtext;
Declare v_table_name varchar(1000);
Declare v_datetime datetime default now();
DROP temporary TABLE if exists temp_todo;
create temporary table temp_todo
(
id int auto_increment primary key,
`table_name` varchar(1000)
);
create table if not exists VIP_Lamar_Index_Rebuild_Script
(
id int auto_increment primary key,
`table_name` varchar(1000),
start_date datetime,
finish_date datetime
);
insert into temp_todo (table_name)
SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = DATABASE();
-- loop thru table
-- check if table exists
Select max(id) Into v_max_counter from temp_todo;
Set v_counter = 1;
WHILE v_counter <= v_max_counter
Do
select `table_name` into v_table_name from temp_todo where id = v_counter;
IF (EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE table_name = v_table_name AND table_schema = DATABASE()))
THEN
set v_query = concat('OPTIMIZE TABLE ' , v_table_name , ';');
set @stmt = v_query;
PREPARE tempstatement FROM @stmt;
EXECUTE tempstatement;
DEALLOCATE PREPARE tempstatement;
END if;
set v_counter = v_counter +1 ;
end while;
insert into VIP_Lamar_Index_Rebuild_Script(`table_name`,start_date,finish_date)
select table_name,v_datetime,now() from temp_todo
;
-- select * from VIP_Lamar_Index_Rebuild_Script;
-- drop table VIP_Lamar_Index_Rebuild_Script;
DROP temporary TABLE if exists temp_todo;
END;
//
DELIMITER ;