Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想在 sorm 中迭代特定表的所有记录,但我想以一种内存效率高的方式来做。
我今天使用的代码是:
Db.query[Items].whereEqual("title", someTitle).fetch.foreach { webCount => //do something }
问题是此代码首先加载所有记录,然后再进入每个项目。有什么方法可以流式传输记录吗?
理想情况下,此类功能需要支持数据库游标,但尚未实现。
但是,这可以通过手动批处理来解决:
val results : Stream[ Items ] = { val batchSize = 256 Stream .from(0) .map(Db.query[Items].whereEqual.limit(batchSize).offset(_ * batchSize).fetch) .takeWhile(_.nonEmpty) .flatten }
当然,您可以将此模式包装在实用函数或隐式转换中。