samayo
samayo

Reputation: 16495

writting to text file vs updating a database

I would like to know if there is any way of measuring the overhead time difference, and resource consumption (memory) between overwriting a file vs updating a mysql database. Lets just assume that I am updating one row, BUT with two statements. Meaning I would have have to fetch the database for a result first as:

SELECT personName FROM emplyees WHERE id = 1

then having fetched that name I would do update

UPDATE employees SET personName = 'foo' WHERE personsName = '$queryResult'

Now, all of that query over a simple file overwrite, as something like this

$file = 'personName.txt';
fwrite(fopen($file, 'w'), 'newName');

I am guessing of course, the overwriting the file takes less time and resource, but until-and-to-what extent? How much heaver should the file have to be to consider choosing the query for executing a similar task?

Just as a wild guess, I am thinking wrtting to a file will be 10 times faster than update the row, but if anyone has a better explanation and practical guides on how to obtain the accurate information, It would be helpful.

thanks

Upvotes: 3

Views: 979

Answers (1)

alandarev
alandarev

Reputation: 8635

Objective: Measure the time taken to update a string column in a MySQL table with single row versus overriting a file with the same string in the File System.

Consider whether you want time taken to connect to the mysql server and disconnect be included (I got a gut feeling they will take more time than the query itself)

...
$val = $_SERVER['REMOTE_ADDR']
$start = microtime(true);

$con=mysqli_connect("example.com","user","pass","my_db");
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$mysqli->query("UPDATE employees SET personName= '$val'");
$mysqli->close();

echo "Time taken for MySQL: ".microtime(true)-$start."s";

$start = microtime(true);

$file = 'personName.txt';
$fpointer = fopen($file, 'w');
fwrite($fpointer, $val);
fclose($fpointer);

echo "Time taken for FileSystem: ".microtime(true)-$start."s";
...

Let us know the results :)

Upvotes: 2

Related Questions