Scala Slick Lazy Fetch

我的梦境 提交于 2019-12-04 23:34:53

Not sure what do you mean by cursors, but you can fetch partial data using pagination:

query.drop(0).take(1000) will take the first 1000 records

query.drop(1000).take(1000) will take from 1001 to 2000 lines of the table.

But this query efficiency will depend on your database, if it will support it, if the table is right indexed.

Mermoz

you could use the combination of iterator which returns an iterator:

 val object = Objects.where(...).map(w => w).iterator()

and a groupby:

val chunkSize = 1000
val groupedObjects = objects.grouped(chunkSize)
groupedObjects.foreach {objects => objects.par.map(h => doJob(h))}

as suggest in this answer

dirceusemighini's answer is correct. I ran into a similar issue a few days ago due to wrong assumption about Query.list(), so I can give some more context. From Slick reference:

"Queries are executed using methods defined in the Invoker trait (or UnitInvoker for the parameterless versions). There is an implicit conversion from Query, so you can execute any Query directly. The most common usage scenario is reading a complete result set into a strict collection with a specialized method such as list or the generic method to which can build any kind of collection"

It is indeed true that Query.list() loads the complete result set in memory. With this in mind, you can have multiple approaches for your problem.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!