/*存在则删除存储过程*/
drop procedure if EXISTS add_index_while_procure;
/*查看存储过程*/
show procedure status;
show create procedure add_index_while_procure;
1 delimiter $$ 2 /*创建带参存储过程 dbName库名称;tablePrefixName表名称前缀;minTableNumber表最小序号;maxTableNumber表最大序号;indexName索引名称;indexValue索引列*/ 3 create PROCEDURE add_index_while_procure(IN dbPrefixName VARCHAR(50),IN tablePrefixName VARCHAR(50),IN minTableNumber INT,IN maxTableNumber INT,IN indexName varchar(50),IN indexValue varchar(200)) 4 BEGIN 5 declare table_name_number int; 6 7 set table_name_number=minTableNumber; 8 while(table_name_number<=maxTableNumber) DO 9 /*添加索引语句*/ 10 set @STMT :=CONCAT("alter table ",dbPrefixName,".",tablePrefixName,table_name_number," ADD index " ,indexName," ",indexValue,";"); 11 /*输出执行语句*/ 12 select @STMT; 13 prepare STMT FROM @STMT; 14 EXECUTE STMT; 15 16 set table_name_number=table_name_number+1; 17 end while; 18 END 19 $$ 20 delimiter;
/*调用存储过程 所有子表及主表
ALTER TABLE manager.tableName ADD INDEX idx_station_time (STATION_CODE, TIME) USING BTREE ;
*/
call add_index_while_procure("manager","tableName_",1906,1912,"idx_code_time","(CODE, TIME)");
---------------------------
1 delimiter $$ 2 /*创建带参存储过程 dbName库名称;tablePrefixName表名称前缀;minTableNumber表最小序号;maxTableNumber表最大序号;indexName索引名称;indexValue索引列*/ 3 create PROCEDURE drop_index_while_procure(IN dbPrefixName VARCHAR(50),IN tablePrefixName VARCHAR(50),IN minTableNumber INT,IN maxTableNumber INT,IN indexName varchar(50)) 4 BEGIN 5 declare table_name_number int; 6 7 set table_name_number=minTableNumber; 8 while(table_name_number<=maxTableNumber) DO 9 /*添加索引语句*/ 10 set @STMT :=CONCAT("alter table ",dbPrefixName,".",tablePrefixName,table_name_number," DROP index " ,indexName,";"); 11 /*输出执行语句*/ 12 select @STMT; 13 prepare STMT FROM @STMT; 14 EXECUTE STMT; 15 16 set table_name_number=table_name_number+1; 17 end while; 18 END 19 $$ 20 delimiter;
/*调用存储过程 所有子表及主表
ALTER TABLE manager.tableName DROP INDEX idx_station_time;
*/
call drop_index_while_procure("manager","tableName_",1906,1912,"idx_station_time");
参考:
https://blog.csdn.net/qq_23132561/article/details/100888454