1. 程式人生 > 實用技巧 >mysql索引批量在postgres裡面重建

mysql索引批量在postgres裡面重建

使用AWS的dms同步工具,把mysql資料同步到postgres,工具本身是沒辦法同步索引這些資訊,也還有一些額外的限制,重建索引就相當麻煩了,寫了一個指令碼:

mysql執行,查詢mysql裡面有哪些索引,生成postgres裡面建索引的語法:

select table_name,concat(cc.ct_inx,concat('(',group_concat(concat('"',substring_index(substring_index(cc.colns,',',b.help_topic_id + 1),',' ,- 1) ,'"')),')'))AS add_inx
       
from (select CONCAT('create index idx_',concat(ma.table_name,'_',FLOOR(1+ rand()*99)),' on ') as ct_inx,colns,table_name from (SELECT a.TABLE_NAME,a.index_name, GROUP_CONCAT(column_name ORDER BY seq_in_index) AS colns FROM information_schema.statistics a -- where TABLE_SCHEMA='copytrading' and TABLE_NAME in
('t_trades') GROUP BY a.TABLE_NAME,a.index_name)ma where ma.INDEX_NAME <> 'PRIMARY')cc JOIN mysql.help_topic b ON b.help_topic_id < (length(cc.colns) - length(REPLACE (cc.colns, ',', '')) + 1) group by cc.colns order by 1;

然後拿到postgres,直接執行,這樣原來mysql有什麼索引,postgres裡面也有什麼索引了,否則很容易出現同步延時等一些效能問題。