X.Herry
X.Herry

Reputation: 65

php reading a file and reading data from mysql which one is faster

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?

Upvotes: 1

Views: 181

Answers (3)

tadman
tadman

Reputation: 211590

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.

Upvotes: 1

Tschallacka
Tschallacka

Reputation: 28722

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.

Upvotes: 2

Irfan Khan
Irfan Khan

Reputation: 415

Reading one file = fast.

Reading many / big files = slow.

Reading singular small entries from database = waste of I/O.

Combining many entries within the database = faster than file accesses.

Upvotes: 0

Related Questions