批量修改MySQL的表引擎

2021-04-22 15:30:20 字數 3891 閱讀 7131

批量修改mysql的表引擎

由於今天要匯入好多表,而且要批量轉換為ndb引擎,備份檔案的太大。開啟修改不現實。所以就寫了乙個sp。 裡面涉及到三個儲存過程。

1、儲存過程詳細語句:

這個儲存過程有點問題,要反覆執行幾次才能把乙個有好多資料的庫全部更新。

具體原因還沒有查清楚,暫時不能用到生產環境。

delimiter $$

create procedure `sp_alter_engine`(

in f_db_name varchar

(255)

,in f_table_name varchar

(255)

,in f_engine_name varchar

(255)

)begin

-- get the total of the table with given database.

declare cnt1 int default 0;

-- increment variable.

declare i int default 0;

-- the true statement.

select

count

(1)from information_schema.tables where table_schema = f_db_name and `engine` is

notnull

and `engine` !

= f_engine_name into cnt1;

-- to determinate whether the given table's name is empty or not.

-- begin if.

if char_length(f_table_name)

= 0 then

-- begin while.

while i < cnt1

doset

@stmt =

concat

('select table_name from information_schema.tables where table_schema='

'',f_db_name,

''' and `engine` is not null and `engine` != '

'',f_engine_name,

''' limit '

,i,',1 into @tbname');

prepare s1 from

@stmt;

execute s1;

deallocate prepare s1;

set@stmt =

concat

('alter table '

,@tbname,

' engine '

,f_engine_name)

;prepare s1 from

@stmt;

execute s1;

deallocate prepare s1;

set@stmt ='';

set i = i + 1;

end while;

-- end while.

else

-- change specific table's engine.

set@stmt =

concat

('alter table '

,f_db_name,

'.',f_table_name,

' engine '

,f_engine_name)

;prepare s1 from

@stmt;

execute s1;

deallocate prepare s1;

set@stmt ='';

endif

;-- end if;

end$

$delimiter ;

2、下面這兩個結合可以用到生產環境。

1)、修改整個庫,呼叫第三個sp。

delimiter $$

drop procedure if

exists `t_girl`.

`sp_alter_db_engine`$$

create definer=

`root`@

`localhost` procedure `sp_alter_db_engine`(

in f_db_name varchar

(255)

,in f_engine_name varchar

(255)

)begin

-- get the total number of tables.

declare cnt1 int default 0;

declare i int;

set i = 0;

select

count

(1)from information_schema.tables where table_schema = f_db_name into cnt1;

while i < cnt1

doset

@stmt =

concat

('select @tbname:=table_name from information_schema.tables where table_schema='

'',f_db_name,

''' order by table_name desc limit '

,i,',1 into @tbname');

prepare s1 from

@stmt;

execute s1;

deallocate prepare s1;

set@stmt ='';

set@tbname =

concat

(f_db_name,

'.',

@tbname)

;call sp_alter_table_engine(

@tbname,f_engine_name)

;set i = i + 1;

end while;

end$

$delimiter ;

2)、修改單個表

delimiter $$

drop procedure if

exists `t_girl`.

`sp_alter_table_engine`$$

create definer=

`root`@`%

` procedure `sp_alter_table_engine`(

in f_tb_name varchar

(255)

,in f_engine_name varchar

(20)

)begin

set@stmt =

concat

('alter table '

,f_tb_name,

' engine='

,f_engine_name)

;prepare s1 from

@stmt;

execute s1;

deallocate prepare s1;

set@stmt ='';

end$

$delimiter ;

3、測試結果:

mysql批量修改表引擎

應用場景 兩台mysql資料庫讀寫分離,將讀庫的引擎修改為myisam來加快查詢速度。操作 通過字串拼接的方法拼出sql語句,到命令列執行。1 2select concat table name,engine from information schema.tables where table sc...

MySQL裡批量修改表引擎

裡面涉及到三個儲存過程 1 儲存過程詳細語句 這個儲存過程有點問題,要反覆執行幾次才能把乙個有好多資料的庫全部更新。具體原因還沒有查清楚,暫時不能用到生產環境。invarchar 255 in varchar 255 in varchar 255 begin get the total of the...

shell指令碼批量修改mysql表引擎

進入到mysql的資料目錄,例如 data 進入到data目錄,如果不進入打絕對路徑會帶著 data 表名 cd data 列出所有表的資料結構檔案,然後去掉.frm,追加到檔案中就是所有的表名了 ls frm awk f frm root all tables.txt 當然,你也可以用另一種方式,...