Iterate over very large array which is stored in a jonb column without loading it into memory

81 Views Asked by At

I have a table in a postgres DB which contains a jsonb column let's call it "data" that contains very large array stored as the following format

[
{
 "id" : 1
 "title" : "title 1",
 "description" : "description 1",
},
{
 "id" : 2
 "title" : "title 2",
 "description" : "description 2",
}  // etc... until 1GB of data
]

My eloquent model contains the following declaration.

class Item extends Model
{
    protected $casts = [
        'data' => 'array'
    ];

If I use a statement like this

$array = $item->data;

It will cause an out of memory exception so how can I iterate over the array and preferably uses the cast in the model without loading the entire data into memory ?

0

There are 0 best solutions below