Which is faster, access data in file or database?

Asked

Viewed 1,720 times

8

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

This data will always be shown on the page in question. So far I am using a file .json and reading the files there, but thought about the possibility of doing this all directly with a database.

  • 2

    In my opinion you should first make a balance! You have to see which type is most useful to you, because not always the fastest is the best to work.

2 answers

12


It depends on the intention. It seems to me from the description that the file is more business. If it is exactly as you said.

But there are those who like to put everything in database, at least in Sqlite. Some prefer to put in a database if you are already using one for other things. This can be useful to facilitate backup replication, access security, etc. Of course if you know that all your data is in one place helps the organization. But if you have all the extra files needed in one place it won’t create much trouble either. 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 direct file access performance is higher. But the database speed is so fast too that it doesn’t make much difference. If you don’t have to keep recording anything in this file mainly concurrently, you can use it without problems. But the question is whether it will take more work to make an access in a different way.

If you don’t have a real problem of performance, reliability or anything like that, it’s more a matter of taste.

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

6

The 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, already a file that is more difficult.

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

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

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

Now, I’m not saying 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 for the system (I’m 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 one depends on how and in what you will apply. If it doesn’t become the same discussion of POO vs procedural programming.

  • 2

    It’s hard here at SOPT

  • 1

    In fact the answer was much simpler than it seems - portability the information generated and reused and security guard for the different environments - times when there are no requirements of one or the other use what best serve.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.