Mysql_num_rows () php - is it efficient?

I have several SELECT in a PHP page, and I used Dreamweaver to create them.

After going through the code that he generated, there seemed to be a lot of fluff that I could cut in most cases, and the mysql_num_rows() for each statement was an example.

So, I am wondering if anyone can tell me if this really saves resources - given that the query is executed independently, is there any actual overhead for this?


UPDATE: Following Chriszuma's advice on 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 seems to be

+6
source share
2 answers

I would expect that such a call would have an extremely minimal impact on performance. It simply counts the lines of its internal query. The SQL query itself will take up most of the processing time.

If you want to know exactly, you can do microtime () before and after the call to find out how long it takes.

 $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 microsecond range.

+3
source

mysql_num_rows () counts rows after they have been extracted. It looks like you took all the lines and saved them in a PHP array, and then ran count($array) . But mysql_num_rows () is implemented in C in the MySQL client library, so it should be slightly more efficient than the equivalent PHP code.

Please note that for mysql_num_rows () to work, you must have the full result of your query in PHP memory space. Thus, there is overhead in the sense that the set of query results can be large and take up a lot of memory.

+4
source

Source: https://habr.com/ru/post/897402/


All Articles