bones.felipe
bones.felipe

Reputation: 596

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.

Upvotes: 1

Views: 104

Answers (1)

Jivan
Jivan

Reputation: 23068

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 ;)

Upvotes: 1

Related Questions