• 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.


    The first way is slower because because the count() function has to be called in every iteration of the loop. The count() method itself is pretty fast, but there is still some overhead in calling the function at all. By moving it outside the loop, you’re performing what is called “loop invariant code motion”, or sometimes “hoisting”.


Comments on this post

Leave a Reply

  • Security Code :

    5 + = eight