What is faster, access data in file or database?

8

In performance, which method is faster to recover data, 1) read a file (which will be generated only once with PHP) or 2) get this data from a database?

This data will always be displayed on the page in question. So far I'm using a .json file and reading the files there, but I thought I could do it all directly with a database.

    
asked by anonymous 29.12.2014 / 18:39

2 answers

10

Depends on intent. It seems to me by the description that the file is more business. If it's exactly like you said.

But there are those who like everything in the database, at least in SQLite. Some prefer to in a database if they are already using one for other things. This can be useful for facilitating backup replication, access security, etc. Of course if you know that all your data are in one place it helps the organization. But if you have all the extra files needed in one place it will not create too much trouble. It is common to put in the database because it is already used to manipulate it, already has functions that easily access the DB.

The performance of direct file access is greater. But the speed of the database is so fast that it does not make much difference. If you do not have to be recording anything in this file mainly concurrently, you can use it without problems. But the question is whether it will give more work to do an access differently.

If you do not have a real performance problem, reliability or anything, it's more a matter of taste.

I see no clear advantages or disadvantages in the two approaches, I repeat, in the case described.

    
29.12.2014 / 19:12
4

Fastest is not always the best for your application.

Generally, when it comes to data recovery, it is recommended to use the database.

In a database you can select the record you want to read, since a file is more difficult.

Of course, this all depends on the use case. I would not use, for example, the database to save webservice connection data from my system. I could do this in a config file simply.

As said in a comment, you have to balance to see which of the two meets the most need.

Worrying too much about performance can sometimes be more detrimental than beneficial. I fully agree with the "doctrine of balance" in this case.

Now, I do not say that we should totally ignore it (the performance). If you are going to use a database, you should do the right procedures so that it does not become a burden on the system (I am talking about poorly constructed queries, lack of indexes).

In the end, I do not think it is convenient to make comparisons between the two modes, since each depends on how and in what you will apply. If it does not become the same discussion of POO vs. procedural programming .

    
10.12.2015 / 12:39