我試圖用sql.rows()Groovy方法獲取一些數據,並且它花了很長時間才返回值。爲什麼sql.rows Groovy方法很慢
所以我嘗試了「標準」的方式,它的速度要快得多(快150倍)。
我錯過了什麼?
請看下面的代碼:第一種方法返回結果約2500毫秒,第二個方法返回15毫秒!
class MyService {
javax.sql.DataSource dataSource
def SQL_QUERY = "select M_FIRSTNAME as firstname, M_LASTNAME as lastname, M_NATIONALITY as country from CT_PLAYER order by M_ID asc";
def getPlayers1(int offset, int maxRows)
{
def t = System.currentTimeMillis()
def sql = new Sql(dataSource)
def rows = sql.rows(SQL_QUERY, offset, maxRows)
println "time1 : ${System.currentTimeMillis()-t}"
return rows
}
def getPlayers2(int offset, int maxRows)
{
def t = System.currentTimeMillis();
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
statement.setMaxRows(offset + maxRows -1);
ResultSet resultSet = statement.executeQuery(SQL_QUERY);
def l_list =[];
if(resultSet.absolute(offset)) {
while (true) {
l_list << [
'firstname':resultSet.getString('firstname'),
'lastname' :resultSet.getString('lastname'),
'country' :resultSet.getString('country')
];
if(!resultSet.next()) break;
}
}
resultSet.close()
statement.close()
connection.close()
println "time2 : ${System.currentTimeMillis()-t}"
return l_list
}
感謝您的迴應,但在我的測試中,我一次只能讀取10行:所以SqlGroovyMethods.toRowResult只能調用10次。它無法解釋性能差異。 – Yann 2012-03-29 12:00:54
你有沒有用'jvisualvm'來看看哪個函數花費時間? – 2012-03-29 12:14:01