简体   繁体   English

使用 Groovy SQL 更新的更快方法

[英]Faster way to update with Groovy SQL

I am updating rows of a MySQL database with groovy and with the method I am using things are very slow.我正在使用 groovy 更新 MySQL 数据库的行,并且使用我使用的方法非常慢。 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.因此,它使用一种需要将其他 2 个字段作为参数传递给它的方法来更新 email(在此示例中为扩展描述)中每个条目的一个字段。

It is very slow, around 600 entries updated per minute, and email has 200000+ entries =/它非常慢,每分钟更新大约 600 个条目,而 email 有 200000+ 个条目 =/

Is there a better method to accomplish this?有没有更好的方法来做到这一点? Should use Groovy if possible, as it needs to run with all my other Groovy scripts.如果可能,应该使用 Groovy,因为它需要与我的所有其他 Groovy 脚本一起运行。

You are doing your update as a cursor based, updatable query, which has to read every record and conditionally write something back.您正在将更新作为基于 cursor 的可更新查询进行,它必须读取每条记录并有条件地写回一些内容。 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.您不需要 eachRow 来执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM