Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 986
zozo Avatar asked Jan 15 '11 14:01

zozo


2 Answers

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.

like image 169
Lord Tydus Avatar answered Nov 20 '22 11:11

Lord Tydus


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

like image 42
Will Avatar answered Nov 20 '22 10:11

Will