Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
A Queries with a good few rows take a long time to execute
  • Hello,

    I have a table with 35 000 rows. A simple query to select all rows of this table take long time ...

    I make a script with just this code : 

    When my code is : $logins = Model_Client_Login::query()->limit(1000)->get();
    The script is ending in 0.5 seconds

    When my code is : $logins = Model_Client_Login::query()->limit(2000)->get();
    The script is ending in 1.3 seconds

    When my code is : $logins = Model_Client_Login::query()->limit(3000)->get();
    The script is ending in 3.2 seconds

    When my code is : $logins = Model_Client_Login::query()->limit(4000)->get();
    The script is ending in 5 seconds

    When my code is : $logins = Model_Client_Login::query()->limit(5000)->get();
    The script is ending in 10 seconds

    When my code is : $logins = Model_Client_Login::query()->limit(6000)->get();
    The script is ending in 18 seconds

    When my code is : $logins = Model_Client_Login::query()->get();
    I forgive because the page is not loading.

    I Wonder why because the same query take in PHPMyAdmin 0.03 seconds.
  • For informations : 

    When my code is : $logins = DB::select()->from('client_logins')->as_object()->execute();
    The script is ending in 0.2 seconds

    Why when I use ORM, it too long ?
    When I must use Modle::query or DB::select ?
  • philipptempelphilipptempel
    Accepted Answer
    Dear remi, You need to know that any call to a model's get() method (actually, it's a call to the ORM class and more precisely to a Query object) does not only retrieve the records from the database but also creates an object in memory for every row. Thus, if you query all rows of 35,000 rows, you will end up with 35,000 objects in memory of the Model_Client class. The biggest performance impact comes from the fact that you need memory for 35,000 objects which are created one by one.
    PhpMyAdmin on the other hand just takes the rows and displays the columns. There is no object creation. I'm pretty sure phpMyAdmin does nothing but echo each row's columns' values. That's why phpMyAdmin is much faster.

    There's two ways to circumvent that: Either, don't query all 35,000 objects at once but go through 1,000 or 2,500 at a time. Or create stdClass objects from the query with the ->as_object() methods before calling ->get();
  • OK Thanks

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion