Mysql 数据库引擎的条件更改
我想将数据库的表格式更改为InnoDB。但我只希望在表格式还不是InnoDB的情况下运行该操作 如果执行以下命令,则如果表格式已为InnoDB,则会反复启动时间密集型操作(类似于修复):Mysql 数据库引擎的条件更改,mysql,Mysql,我想将数据库的表格式更改为InnoDB。但我只希望在表格式还不是InnoDB的情况下运行该操作 如果执行以下命令,则如果表格式已为InnoDB,则会反复启动时间密集型操作(类似于修复): ALTER TABLE `MyTable` ENGINE InnoDB; 如果目标格式已经是InnoDB,是否有一个条件可以在这种情况下插入,以使操作运行得更快 我在想这样的事情: ALTER TABLE `MyTable` ENGINE InnoDB WHERE ENGINE != 'InnoDB'; AL
ALTER TABLE `MyTable` ENGINE InnoDB;
如果目标格式已经是InnoDB,是否有一个条件可以在这种情况下插入,以使操作运行得更快
我在想这样的事情:
ALTER TABLE `MyTable` ENGINE InnoDB WHERE ENGINE != 'InnoDB';
ALTER TABLE IGNORE `MyTable` ENGINE InnoDB;
您可以使用
信息\u schema.TABLES
,生成脚本:
set @db_schema = 'test';
set @alter_query = 'alter table `{table}` engine=innodb;';
select group_concat(replace(
@alter_query,
'{table}',
replace(t.TABLE_NAME, '`', '``')
) separator '\n') as script
from information_schema.TABLES t
where t.TABLE_SCHEMA = @db_schema
and t.ENGINE = 'MyISAM';
然后,您需要复制结果并执行它 更新 如果您需要在一次运行中执行它,您可以在
information\u schema.TABLES
上定义一个带有光标的存储过程并执行它:
drop procedure if exists tmp_alter_myisam_to_innodb;
delimiter //
create procedure tmp_alter_myisam_to_innodb(in db_name text)
begin
declare done int default 0;
declare tbl text;
declare cur cursor for
select t.TABLE_NAME
from information_schema.TABLES t
where t.TABLE_SCHEMA = db_name
and t.ENGINE = 'MyISAM';
declare continue handler for not found set done = 1;
open cur;
fetch_loop: loop
fetch cur into tbl;
if done then
leave fetch_loop;
end if;
set @stmt = 'alter table `{table}` engine=innodb;';
set tbl = replace(tbl, '`', '``');
set @stmt = replace(@stmt, '{table}', tbl);
prepare stmt from @stmt;
execute stmt;
deallocate prepare stmt;
end loop;
close cur;
end //
delimiter ;
call tmp_alter_myisam_to_innodb('my_db');
drop procedure tmp_alter_myisam_to_innodb;
也许这将帮助您这只是一个开始,但我只能提供一个sql文件进行转换(没有脚本)。所以我需要一种直接执行结果的方法。“然后你需要复制结果并执行它。”这是不可能的。我只有一个sql文件,无法手动执行任何操作。选择[…]进入文件工作,但源命令不可用。