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 lessMark Baker– Mark Baker2014-02-02 00:28:32 +00:00Commented Feb 2, 2014 at 0:28
Add a comment
|
1 Answer
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 ;)
2 Comments
bones.felipe
"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."
Jivan
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.