簡體   English   中英

Groovy SQL eachRow格式輸出

[英]Groovy SQL eachRow format output

我必須將MSSQL DB與MYSQL DB同步。 (> 500個表,總計> 10000000行)。
我有一個可行的解決方案,但性能不可接受。
瓶頸可能在這里:

我從MSSQL數據庫中讀取

mssqlDaten = mssql.eachRow (....) { row_data -> ...

由於不同表中字段名稱的更改,我使用了for循環來創建insert語句

    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,...)。

嘗試像這樣批量插入:

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 )
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM