What are faster database queries or writing / reading files

I know that in normal cases it’s faster to read / write from a file, but if I created a chat system: Would it be faster to write and read from a file or insert / select data in db and cahe results?

+7
source share
4 answers

The database is faster. And what's important to you is dealing with simultaneous access.

+8
source

Do you really want the mechanical action of the disc every time someone dials? Burning to disk is a terrible idea. Cache messages in memory. Clear the message when it is sent to all users in the room. The cache will remain small, most of the time empty. This is your best option if you do not need a history magazine.

But if you need a magazine ....

If you write a large amount of data in 1 pass, I guarantee that the file will smoke database insert performance. The bulk insert function of the database may correspond to a file, but this requires a source of data files. You will need to queue a large number of messages in memory, and then periodically reset the files.

For many small records, the gap will close and the database will move forward. Indexes will affect insertion speed. If thousands of users are pasting into a highly indexed table, you may have problems.

Do your own tests to prove that faster. Simulate a realistic load, not 1 user test.

+2
source

Databases are far away.

Databases are optimized for storing data that is constantly updated and changed, as in your case. File storage is designed for long-term storage with a few changes.

(even if the files were faster, I would still work with databases because it was easier to develop and maintain)

0
source

Since I assume that your system will continuously write / read data (when people print their messages), writing them to a file takes longer due to the file processing procedure, i.e.

  • open file for writing
  • lock file
  • write and save
  • unlock file

I would go with db.

0
source

All Articles