Groovy sql数据集导致java.lang.OutOfMemory

Ska*_*rab 5 database groovy out-of-memory

我有一个252759元组的表.我想使用DataSet对象让我的生活更轻松,但是当我尝试为我的表创建一个DataSet时,3秒后,我得到了java.lang.OutOfMemory.

我没有数据集的经验,是否有任何指导如何使用DataSet对象的大表?

Dón*_*nal 7

你真的需要一次检索所有行吗?如果没有,那么您可以使用下面显示的方法批量检索它们(例如)10000.

def db = [url:'jdbc:hsqldb:mem:testDB', user:'sa', password:'', driver:'org.hsqldb.jdbcDriver']

def sql = Sql.newInstance(db.url, db.user, db.password, db.driver)
String query = "SELECT * FROM my_table WHERE id > ? ORDER BY id limit 10000"

Integer maxId = 0

// Closure that executes the query and returns true if some rows were processed
Closure executeQuery = {

    def oldMaxId = maxId 
    sql.eachRow(query, [maxId]) { row ->

         // Code to process each row goes here.....
         maxId = row.id
    }
    return maxId != oldMaxId 
}


while (executeQuery());
Run Code Online (Sandbox Code Playgroud)

AFAIK limit是特定于MySQL的功能,但大多数其他RDBMS具有限制查询返回的行数的等效功能.

此外,我还没有测试(甚至编译)上面的代码,所以小心处理!


Eri*_*ler 1

为什么不首先为 JVM 提供更多内存呢?

java -Xms<initial heap size> -Xmx<maximum heap size>
Run Code Online (Sandbox Code Playgroud)

252759 个元组听起来不像是 4GB RAM + 某些虚拟内存无法在内存中处理的机器。