Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql_num_rows() php - is it efficient?

I have several SELECT statements on a PHP page, and I used Dreamweaver to generate those.

After going through the code it generated, there seemed to be alot of fluff which I could cut out under most circumstances, a mysql_num_rows() line for each statement being an example.

So I'm wondering if anyone can tell me whether or not this actually saves resources - considering the query is being run regardless, is there any actual overhead for this?


UPDATE: After following Chriszuma's suggestion about microtime, here are my results:

//time before running the query
1: 0.46837500 1316102620

//time after the query ran
2: 0.53913800 1316102620

//time before calling mysql_num_rows()
3: 0.53914200 1316102620

//time after mysql_num_rows()
4: 0.53914500 1316102620 

So not much overhead at all, it seems

like image 226
totallyNotLizards Avatar asked Sep 15 '11 15:09

totallyNotLizards


1 Answers

mysql_num_rows() counts rows after they have been fetched. It's like you fetched all rows and stored them in a PHP array, and then ran count($array). But mysql_num_rows() is implemented in C within the MySQL client library, so it should be a bit more efficient than the equivalent PHP code.

Note that in order for mysql_num_rows() to work, you do have to have the complete result of your query in PHP's memory space. So there is overhead in the sense that a query result set could be large, and take up a lot of memory.

like image 156
Bill Karwin Avatar answered Sep 28 '22 10:09

Bill Karwin