We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

ODM Find() is slow with 10k records

Hi,

I have a collection called reviews. Inside this collection, I have 10,000+ documents. When I do a find() and use paginator, the page loads quite slow.

I think is because I have too many documents. How can I optimize this,

 $reviews_res = Reviews::find();

  $paginator = new \Phalcon\Paginator\Adapter\NativeArray(
        array(
            "data" => $reviews_res,
            "limit"=> 18,
            "page" => $currentPage
        )
    );

    $reviewslistpaginated = $paginator->getPaginate();

Advice appreciated. :D

edited May '17

Hey friend,

first of all, ODM/ORM's will always be slower than normal quries. In your case you are selecting all 10k records and paginate them with NativeArray adaptor, its like doing array_chunk on huge array...

I would recommend to rewrite the query with QueryBuilder and use the appropriate adapter: https://docs.phalcon.io/en/3.0.0/reference/pagination.html



145.0k
Accepted
answer

Don't use paginator, just use find:

 $reviews_res = Reviews::find([
     'limit' => '18',
     'skip' => $currentPage*18 - 18
 ]);

I guess we need paginator adapter for collection, can you create issue on github?

Collection doesn't have query builder, right ? :D

Ohhhh I'm talking nonsence! Thank you for the correction Wojciech :)