Welcome Guest, Not a member yet? Register   Sign In
Fastest method to return number of rows from a query
#1

[eluser]whiteae[/eluser]
I was wondering what is the fastest way to return the count of a certain query. Would it be faster to do a simple $query=" select count(*) from table" or use codeigniter's active record method such as $this->db->count_all(), or another way of doing it. The database is growing and this isn't a problem right now, but once it gets much larger, it might take quite awhile to get results back. Thanks!
#2

[eluser]TWP Marketing[/eluser]
Use $query->num_rows() which is described in the User Guide
http://ellislab.com/codeigniter/user-gui...sults.html
#3

[eluser]whiteae[/eluser]
I had been using that but wouldn't that be slower because it would have to grab the results from the query and then count the rows? Wouldn't count(*) be better because it counts the rows in the process of retrieving them?
#4

[eluser]CroNiX[/eluser]
I don't think it would be slower because it (the db object) is already doing it, every time, whether you access the count with num_rows() or not. Adding extra queries would be slower it seems as you're increasing the work.

BTW, AR's "count_all()" is doing exactly that...SELECT COUNT(*).
#5

[eluser]TWP Marketing[/eluser]
I believe the number is part of the query object and is merely returned by the function $query->num_rows().
Someone will please correct me if I'm wrong here, but the count has already been made by the db driver when it creates the query object.
I see in /system/database/DB_result.php that the function num_rows() simply returns the var num_rows which is part of the query object object




Theme © iAndrew 2016 - Forum software by © MyBB