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

标签 database groovy out-of-memory

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

我没有使用数据集的经验,是否有关于如何将数据集对象用于大表的指南?

最佳答案

您真的需要一次检索所有行吗?如果没有,那么您可以使用下面显示的方法分批检索它们(例如)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());

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

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

关于database - Groovy sql 数据集导致 java.lang.OutOfMemory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3665717/

相关文章:

mysql - 使用 INNER JOIN 处理两批列

sql - Oracle 中的自愿事务优先级

groovy - Spock 的@Narrative 和@Title 注释

python - 内存映射 ndarray 上的 numpy.std 因 MemoryError 失败

android - BitmapFactory OOM 让我抓狂

mysql - 哪个数据库管理器适用于 100Go 表?

mongodb - Mongodump 和 mongorestore;未找到字段

mysql - Groovy SQL 中的 CASE 语句

groovy - Groovy 中的 String 没有此类属性错误

vb.net - vb.net 应用程序出现 OutOfMemoryException