There has been lots of web developer arguing about the fact as which one is faster Database or Files. I always replied in most cases as database is faster than Flat file and After getting that reply they always argued as why do you think that database is faster. I explained them with lots of words but now thought of just bench marking them so that its clear as what is faster.
Before going into the codes I would like to explain about the attachments
shabbir.txt - A Flat file that has 22400 names stored with one line containing one record.
benchmark.sql - SQL Script to create a table with the same data as the flat file contains. Remember that it also has same order as the flat file
Now here is the PHP code to be put in a file
Results for my machine *
Test without condition!
Reading File ...
Reading Database ...
Result of benchmark
Flat File time span to do the operation ==> 0.087211132049561 .
Database time span to do the operation ==> 0.13485312461853 .
File operation was faster by 0.047642
Test with condition where names start with 'A'!
Reading File ...
Reading Database ...
Result of benchmark
Flat File time span to do the operation ==> 0.11205410957336 .
Database time span to do the operation ==> 0.018458127975464 .
Database operation was faster by 0.093596
Test writing to Database & File
Writing to File ...
Writing to Database ...
Result of benchmark
Flat File time span to do the operation ==> 0.00011086463928223 .
Database time span to do the operation ==> 0.00024604797363281 .
File operation was faster by 0.000135
* can vary the time from machine to machine and from run to run depending on webserver loads and other network factors
Inference
As we can see from the above results
File operations were faster when there is no condition applied to the results and so the first impression we get is Files are faster than database but if you take the scenario into real time site then hardly there is any time you fetch all the records from the database and so for consideration we can ignore this.
Now applying a very simple condition where names start with 'A' and we see that the database operation is much faster than and practically similar operations are done more frequently on database where its database which is faster.
All the sites tend to follow WORM - write once read many and so the write operation in files is faster but again this operation is not that considerable speed.
Before going into the codes I would like to explain about the attachments
shabbir.txt - A Flat file that has 22400 names stored with one line containing one record.
benchmark.sql - SQL Script to create a table with the same data as the flat file contains. Remember that it also has same order as the flat file
Now here is the PHP code to be put in a file
PHP Code:
<?php
echo("<h3>Test without condition!</h3>");
echo "Reading File ... <BR />";
$start = explode(" ",microtime());
$beginfile = $start[1]+$start[0];
$h = fopen("shabbir.txt","r");
if (!$h) return;
$msg="";
while (!feof($h))
fgets($h);
$start = explode(" ",microtime());
$endfile = $start[1]+$start[0];
echo "Reading Database ... <BR />";
$start = explode(" ",microtime());
$begindb = $start[1]+$start[0];
$db=mysql_connect("localhost","root","");
mysql_select_db("benchmark");
$r = mysql_query("Select f1 from t1");
while($f = mysql_fetch_array($r));
$start = explode(" ",microtime());
$enddb = $start[1]+$start[0];
echo "Result of benchmark";
$resultfile = $endfile - $beginfile;
$resultdb = $enddb - $begindb;
echo "<BR />Flat File time span to do the operation ==> $resultfile .";
echo "<BR />Database time span to do the operation ==> $resultdb .<br/>";
if($resultdb>$resultfile)
{
$diff=$resultdb - $resultfile;
printf("File operation was faster by %f",$diff);
}
else
{
$diff=$resultfile-$resultdb;
printf("Database operation was faster by %f",$diff);
}
mysql_close($db);
fclose($h);
//NEW TEST
echo("<h3>Test with condition where names start with 'A'!</h3>");
echo "Reading File ... <BR />";
$start = explode(" ",microtime());
$beginfile = $start[1]+$start[0];
$h = fopen("shabbir.txt","r");
while (!feof($h))
{
$line=fgets($h);
if(eregi("^a",$line));
}
$start = explode(" ",microtime());
$endfile = $start[1]+$start[0];
echo "Reading Database ... <BR />";
$start = explode(" ",microtime());
$begindb = $start[1]+$start[0];
$db=mysql_connect("localhost","root","");
mysql_select_db("benchmark");
$r = mysql_query("Select f1 from t1 where f1 like 'a%'");
while($f = mysql_fetch_array($r));
$start = explode(" ",microtime());
$enddb = $start[1]+$start[0];
echo "Result of benchmark";
$resultfile = $endfile - $beginfile;
$resultdb = $enddb - $begindb;
echo "<BR />Flat File time span to do the operation ==> $resultfile .";
echo "<BR />Database time span to do the operation ==> $resultdb .<br/>";
if($resultdb>$resultfile)
{
$diff=$resultdb - $resultfile;
printf("File operation was faster by %f",$diff);
}
else
{
$diff=$resultfile-$resultdb;
printf("Database operation was faster by %f",$diff);
}
//NEW TEST WRITING
echo("<h3>Test writing to Database & File</h3>");
echo "Writing to File ... <BR />";
$start = explode(" ",microtime());
$beginfile = $start[1]+$start[0];
$newfile=fopen("shabbir.txt",'a');
fputs($newfile,"Name");
fclose($newfile);
$start = explode(" ",microtime());
$endfile = $start[1]+$start[0];
$start = explode(" ",microtime());
$begindb = $start[1]+$start[0];
echo "Writing to Database ... <BR />";
$db=mysql_connect("localhost","root","");
mysql_select_db("benchmark");
$r = mysql_query("insert into t1(f1) values('Name')");
$start = explode(" ",microtime());
$enddb = $start[1]+$start[0];
echo "Result of benchmark";
$resultfile = $endfile - $beginfile;
$resultdb = $enddb - $begindb;
echo "<BR />Flat File time span to do the operation ==> $resultfile .";
echo "<BR />Database time span to do the operation ==> $resultdb .<br/>";
if($resultdb>$resultfile)
{
$diff=$resultdb - $resultfile;
printf("File operation was faster by %f",$diff);
}
else
{
$diff=$resultfile-$resultdb;
printf("Database operation was faster by %f",$diff);
}
?>
Test without condition!
Reading File ...
Reading Database ...
Result of benchmark
Flat File time span to do the operation ==> 0.087211132049561 .
Database time span to do the operation ==> 0.13485312461853 .
File operation was faster by 0.047642
Test with condition where names start with 'A'!
Reading File ...
Reading Database ...
Result of benchmark
Flat File time span to do the operation ==> 0.11205410957336 .
Database time span to do the operation ==> 0.018458127975464 .
Database operation was faster by 0.093596
Test writing to Database & File
Writing to File ...
Writing to Database ...
Result of benchmark
Flat File time span to do the operation ==> 0.00011086463928223 .
Database time span to do the operation ==> 0.00024604797363281 .
File operation was faster by 0.000135
* can vary the time from machine to machine and from run to run depending on webserver loads and other network factors
Inference
As we can see from the above results
File operations were faster when there is no condition applied to the results and so the first impression we get is Files are faster than database but if you take the scenario into real time site then hardly there is any time you fetch all the records from the database and so for consideration we can ignore this.
Now applying a very simple condition where names start with 'A' and we see that the database operation is much faster than and practically similar operations are done more frequently on database where its database which is faster.
All the sites tend to follow WORM - write once read many and so the write operation in files is faster but again this operation is not that considerable speed.


