It's a real php interview question. I know the answer is not just which one is faster. We can answer it in many aspects. Can anyone give me some suggestions please?
-
Possible duplicate of reading from MySQL is faster or reading from a file is faster?Kumar Rakesh– Kumar Rakesh2016-08-25 06:01:53 +00:00Commented Aug 25, 2016 at 6:01
-
@Kumar, thanks for your helpful link to my question. I tried to search the similar questions before asking, but no good.:)X.Herry– X.Herry2016-08-25 09:34:55 +00:00Commented Aug 25, 2016 at 9:34
3 Answers
files:
- reading file: fast
- predicting format/codepage: slow, painstaking, error prone
- file permissions management
- multiple write access not possible
- locking mechanism strategy required
- parsing file: relatively fast. depending on data complexity
- file seek of file in directory with many other files(1000+): extremely slow as the OS will iterate through the file list in directory to find your requested file with a binary search(if you're lucky)
- reading not possible when other is writing
- threaded fork issues
- large filesizes if stored in text
- In short: only use files for static data like configuration files. Never for dynamic data
Database:
- better management of all of the above
- compact storage
- fast lookup engine
- easy combining of related fact
- easy to share access with other machines/programs
- rollback mechanisms built in.
- Don't use for configuration that remain static.
6 Comments
This is an example of an interview question with no correct answer. A case could be made for either of these things.
For files you might say they're quick to load, that there's a lot of kernel optimization around fetching them from disk and providing them to a user process, and even more around sending them directly from disk to a socket via something like sendfile. That would be true.
Then for databases you could say that frequently accessed data is stored in memory so there's no I/O round-trip to disk, that could be faster, especially if you're comparing reading parts of a file using a suboptimal structure versus records in a database. This is an algorithmic concern as well.
So it really depends on what kinds of files and what sort of read/write access patterns are involved. To say either of these things is faster is to miss the point of the question.