Optimize Code In Php(Code Optimization)

Select COUNT() will be faster than mysql_num_rows(), internally the server will process the request differently. mysql_query() transfers all result records from the MySQL into the php process before it returns. That alone would make the mysql_num_rows() version slower. Furthermore for some engines MySQL can serve a Count(*) request from the index of the table without hitting the actual data. A SELECT * FROM foo on the other hand results in a full table scan and MySQL has to read every single dataset 

<?php // The better way , it will be faster $sql = “select count(id) from user where email like’%gmail%’”; $res = mysql_query($sql) or die(mysql_error()); echo $value = mysql_fetch_row($res);   // it will be slower $sql = “select count(id) from user where email like’%gmail%’”; $res = mysql_query($sql) or die(mysql_error()); echo $value = mysql_num_rows($res);   ?>   <?php //Example ONE     for($i = 0; $i < count($myLargeArray); $i++ ) { echo myLargeArray[$i]; }   //Example TWO   $count = count($myLargeArray); for($i = 0; $i < $count; $i++ ) { echo myLargeArray[$i]; }   ?>

No comments:

Post a Comment