1

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.

1
  • No brainer: make one call that returns 1000 records.... every call to the database is an overhead, and the fewer queries you make the better your performance will be. 1000 calls is a lot of overhead, 1 call is 1000 times less Commented Feb 2, 2014 at 0:28

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

"Time to fetch one record at a time, 1000 times : 9.97631549835E-5 ms Time to fetch 1000 records at once : 2.9571056366E-6 ms", just like Mark Berker said, "the fewer queries you make the better your performance will be."
Oops - it was * 1000 that I meant not / 1000 - but the proportions stay the same, you have 99ms versus 2.9ms which is a factor 30, which is astronomically huge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.