2017-09-27 181 views
0

我必須將MSSQL DB與MYSQL DB同步。 (> 500個表格,總數> 10000000行)。
我有一個工作解決方案,但性能是不可接受的。
瓶頸可能是在這裏:Groovy SQL eachRow格式輸出

我從MSSQL數據庫讀取

mssqlDaten = mssql.eachRow (....) { row_data -> ... 
因爲改變字段名

在不同的表我使用一個for循環來創建插入語句

str = "insert into current_table values (" 
    for (over all fields from the current table) { 
    str += "'" + row_data[i] + "', " 
    } 
    str += ")" 
    mssql.execute (str) 

這可能是我必須通過循環的每一行的問題。

有人想法直接以格式(ValueField1,ValueField2,....)獲取行數據。當我現在打印行數據時,我得到(NameField1:ValueField1,NameField2.ValueField2,...)。

+0

你能提供的源和目標表的模式? –

+0

模式是相同的,因爲在我開始寫入數據之前,我創建了包含所有字段和數據代碼的表。 – Helge

+0

難道你不能像'insert into mytable select * from mytable',不確定你如何鏈接你的模式在mysql中,但在Oracle中你有一個數據庫鏈接,所以更新會更像'insert into mytable select *從dblinked.mytable',你可以查詢INFORMATION_SCHEMA.TABLES的所有表(也許可以(https://dev.mysql.com/doc/refman/5.7/en/tables-table.html)),然後遍歷每個表並執行插入 –

回答

0

嘗試做插入有一批這樣的:

def table = "MYTABLE" 
//the list of column names could be selected from source database 
def columns = ["A","B","C","D"] 

def sQuery = "select ${columns.join(',')} from $table".toString() 

//build insert query with column list and ? as value placeholders 
//as result we have query like : insert into MYTABLE (A,B,C,D) values (?,?,?,?) 
def iQuery = "insert into $table (${columns.join(',')}) values (${ columns.collect{'?'}.join(',') })".toString() 

def updateCounts = mysql.withBatch(100, iQuery) { ps -> 
    mssql.eachRow (sQuery) { row_data -> 
     def row_array = columns.collect{ colname-> rowdata[colname] } 
     ps.addBatch(row_array) 
    } 
} 
+0

still與不同表格的複雜性有關的問題。第一個表130字段,第二個20字段,第三個400字段,...插入部分我可以填充表名。但不知道我可以如何將字段名放在ps.addBatch([這裏我必須將字符串放在fieldnames中])。 – Helge

+0

@幫助,我添加了一些動態的查詢生成的例子 – daggett