简体   繁体   中英

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

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.

I would expect that such a call would have an extremely minimal impact on performance. It is just counting the rows of its internally-stored query result. The SQL query itself is going to take the vast majority of processing time.

If you want to know for sure, you can execute microtime() before and after the call to see exactly how long it is taking.

$startTime = microtime(true);
mysql_num_rows();
$time = microtime(true) - $startTime;
echo("mysql_num_rows() execution: $time seconds\n");

My suspicion is that you will see something in the microseconds range.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM