Php/MySQL Performance

Suppose I have to do a MySQL query through a php Object, what is better?, make the method to return explictly one record and call this method let say 1000 times (varying the parameters) or give the method a range of 1000 and return 1000 records in one call?. All in terms of speed/performance.

Answers


There's nothing better than a little exercise

If you want to have a little fun, you can try this by yourself:

function getOneRecordAtATime()
{
    for ($i = 0; $i < 1000; $i++)
    {
        // call the db and retrieve one record
    }
}

function getAllRecords()
{
    // call the db once and retrieve 1000 records
}

$startOne = microtime(true);
getOneRecordAtATime();
$endOne = microtime(true);

$startTwo = microtime(true);
getAllRecords();
$endTwo = microtime(true);

$timeOne = ($endOne - $startOne) * 1000;
$timeTwo = ($endTwo - $startTwo) * 1000;

print "Time to fetch one record at a time, 1000 times : ".$timeOne." ms\n";
print "Time to fetch 1000 records at once : ".$timeTwo." ms\n";

Tell us the results ;)


Need Your Help

Accessing Service Layer from custom Controller in Spring MVC

java spring spring-mvc spring-roo

I am new to Spring and ROO and this Annotation/Aspect hell.

Sequel model over two joined tables

ruby orm model left-join sequel

I have a legacy PostgreSQL database, which has a single model split into two tables, with one-to-one mapping between them.