EllisLab text mark
Advanced Search
db->count_all_results() VS db->query(“SELECT COUNT…”)
Posted: 05 September 2010 04:34 PM
Joined: 2009-12-29
46 posts


I’m wondering what way is the most “performance friendly” to count rows matching a where clause.
I just read the user guide and found this:

$this->db->like(‘title’, ‘match’);
echo $this->db->count_all_results();

I’m wondering if the query is fully executed behind, and counted after, or if a “SELECT COUNT” is executed by codeigniter.

Thank you by advance.


Posted: 05 September 2010 07:15 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2009-03-21
680 posts

Good question.  I’m not sure of the answer yet, but you can find out easily like this:



The last_query() method will show you the query generated by the active record class.  Let us know the results, I’m curious.


EDIT: count_all_results() will create a SELECT COUNT(*) query.

Posted: 06 September 2010 09:21 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2009-12-29
46 posts

cool smile thank you for your help !

Edit: this thread completes the response:http://ellislab.com/forums/viewthread/166535/
Instead of runing “last query”, you can run this: $this->output->enable_profiler(TRUE);

See ya