Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Slick Lazy Fetch

Tags:

scala

slick

I want to be able to fetch all records from a very big table using Slick. If I try to do this through foreach, for or list fetching; I get an Out Of Memory Exception.

Is there any way to use "cursors" with Slick or lazy loading that only fetch the object when needed reducing the amount of memory used?

like image 606
Octavio Luna Avatar asked Jan 16 '13 23:01

Octavio Luna


2 Answers

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.

like image 74
dirceusemighini Avatar answered Nov 16 '22 02:11

dirceusemighini


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

like image 29
Mermoz Avatar answered Nov 16 '22 01:11

Mermoz