groovy sql eachRow和rows方法 [英] groovy sql eachRow and rows method
问题描述
我对grails和groovy很陌生。
任何人都可以请向我解释这两个groovy sql方法之间的区别
sql.eachRow
sql .rows
另外,哪种效率更高?
我正在研究从数据库中检索数据的应用程序(结果集非常大),并将其写入CSV文件或返回JSON格式。
I想知道上面提到的两种方法中的哪一种用来使该过程更快更有效。 任何人都可以向我解释这两个groovy 很难确切地说出你指的是哪2种方法,因为每种方法都有大量的重载版本。然而,在任何情况下, 而
sql方法之间的
差异sql.eachRow sql.rows
eachRow
都不会返回任何内容。
void eachRow(String sql,Closure闭包)
rows
返回行列表
列行(String sql)
$ p $所以如果你使用
eachRow
,闭包就作为第二个参数处理每一行,例如
sql.eachRow(select * from PERSON where lastname ='murphy'){row - >
println$ row.firstname
}
如果您使用 rows
这些行被返回,因此应该由调用者处理,例如
rows(select * from PERSON where lastname ='murphy')。each {row - >
println$ row.firstname
}
另外,哪种效率更高?
这个问题几乎无法回答。即使我自己实现了这些方法,也无法知道哪个方法会更好地执行 ,因为我不知道
或影响方法性能的任何其他因素,这些因素无法单独由源代码确定。您可以通过衡量每种方法的效果来获得有效答案的唯一方法。
尽管我上面已经说过,但如果这两者之间的表现差异有任何意义,我会感到惊讶,所以如果我是你,我会选择哪一个你觉得更方便。如果您稍后发现此方法是性能瓶颈,请尝试使用另一个瓶颈(但我敢打赌,您一美元一分钱就不会有任何区别)。
I am new to grails and groovy. Can anyone please explain to me the difference between these two groovy sql methods
sql.eachRow
sql.rows
Also, which is more efficient?
I am working on an application that retrieves data from the database(the resultset is very huge) and writes it to CSV file or returns a JSON format.
I was wondering which of the two methods mentioned above to use to have the process done faster and efficient.
Can anyone please explain to me the difference between these two groovy sql methods sql.eachRow sql.rows
It's difficult to tell exactly which 2 methods you're referring 2 because there are a large number of overloaded versions of each method. However, in all cases, eachRow
returns nothing
void eachRow(String sql, Closure closure)
whereas rows
returns a list of rows
List rows(String sql)
So if you use eachRow
, the closure passed in as the second parameter should handle each row, e.g.
sql.eachRow("select * from PERSON where lastname = 'murphy'") { row ->
println "$row.firstname"
}
whereas if you use rows
the rows are returned, and therefore should be handled by the caller, e.g.
rows("select * from PERSON where lastname = 'murphy'").each {row ->
println "$row.firstname"
}
Also, which is more efficient?
This question is almost unanswerable. Even if I had implemented these methods myself there's no way of knowing which one will perform better for you because I don't know
- what hardware you're using
- what JVM you're targeting
- what version of Groovy you're using
- what parameters you'll be passing
- whether this method is a bottleneck for your application's performance
or any of the other factors that influence a method's performance that cannot be determined from the source code alone. The only way you can get a useful answer to the question of which method is more efficient for you is by measuring the performance of each.
Despite everything I've said above, I would be amazed if the performance difference between these two was in any way significant, so if I were you, I would choose whichever one you find more convenient. If you find later on that this method is a performance bottleneck, try using the other one instead (but I'll bet you a dollar to a dime it makes no difference).
这篇关于groovy sql eachRow和rows方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!