zozo
zozo

Reputation: 8582

what is faster database querys or file writing/reading

I know that in normal cases is 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 to insert/select data in a db and cahe results?

Upvotes: 11

Views: 9627

Answers (4)

Lord Tydus
Lord Tydus

Reputation: 544

Do you really want a mechanical disk action every time someone types? Writing to disk is a horrible idea. Cache messages in memory. Clear the message once it is sent to all users in the room. The cache will stay small, most of the time empty. This is your best option if you don't need a history log.

But if you need a log....

If you write a large amount of data in 1 pass, I guarantee the file will smoke database insert performance. A bulk insert feature of the database may match the file, but it requires a file data source to begin with. You would need to queue up a lot of messages in memory, then periodically flush to the file.

For many small writes the gap will close and the database will pull ahead. Indexes will influence the insert speed. If thousands of users are inserting to a heavily indexed table you may have problems.

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

Upvotes: 3

Milo Wielondek
Milo Wielondek

Reputation: 4352

Since I presume your system would write/read data continuously (as people type their messages), writing them to a file would take longer time because of the file handling procedure, i.e.

  1. open file for writing
  2. lock file
  3. write & save
  4. unlock file

I would go with db.

Upvotes: 0

orlp
orlp

Reputation: 117681

Databases by far.

Databases are optimized for data storage which is constantly updated and changed as in your case. File storage is for long-term storage with few changes.

(even if files were faster I would still go with databases because it's easier to develop and maintain)

Upvotes: 0

Will
Will

Reputation: 6286

Database is faster. AND importantly for you, deals with concurrent access.

Upvotes: 8

Related Questions