开发者

Faster way to update with Groovy SQL

开发者 https://www.devze.com 2023-03-16 11:35 出处:网络
I am updating rows of a MySQL database with groovy and with the method I am using things are very slow. I was hoping you could improve on the performance of my example:

I am updating rows of a MySQL database with groovy and with the method I am using things are very slow. I was hoping you could improve on the performance of my example:

sql.resultSetConcurrency = ResultSet.CONCUR_UPDATABLE

sql.eachRow("SELECT * FROM email) { bt ->
    bt.extendedDesc = update(bt.name, bt.direction)
}

sql.resultSetConcurrency = ResultSet.CONCUR_READ_ONLY

Then there is the update method:

def update(name, direction) {
    if (direction == 'Outgoing') {
        result = 'FROM: '+name
    } else {
        result = 'TO: '+name
    }
    if(result.size() > 75) {
        result = result.substring(0, 72) + "..."
    }
    return result
}

So it updates one field of each entry in email (extendedDesc in this example) using a method that needs 2 other fields passed to it as parameters.

It is very开发者_开发技巧 slow, around 600 entries updated per minute, and email has 200000+ entries =/

Is there a better method to accomplish this? Should use Groovy if possible, as it needs to run with all my other Groovy scripts.


You are doing your update as a cursor based, updatable query, which has to read every record and conditionally write something back. You're doing all the heavy lifting in the code, rather than letting the database do it.

Try using an update query to only update the records matching your criteria. You won't need eachRow to do this.

0

精彩评论

暂无评论...
验证码 换一张
取 消