activity store in file or MySQL database using php which is better

Logs using files are more efficient, however logs stored in the database are easier to read, even remotely.

however that connecting and inserting rows into the database is error prone (database server down, password wrong, out-of-resources).

Log to db instead if you need other people needs to read logs in a web interface or if you need the ability to search through logs. As someone else has pointed out also concurrency matters, if you have a lot of users log to db could scale better.

Yes, it’s faster to write to files but it’s far faster for you to find what you need in the logs if they are in a database.

I think storing logs in database is not a good idea. The pros of storing logs to databases over files is that you can analyze your logs much more easily with the power of SQL, the cons, however, is that you have to pay much more time for database maintenance. You’d better to set up a separate database server to store your logs or your might get too much log INSERT which will decrease your database performance to production use; also, it’s not easy to migrate, archive logs in database, compared with files.

Error logging is best limited to files in my opinion, because if there is a problem with the database.

When using filesystem careful with :

  • Confidentiality : Put documents outside of your Apache Document Root. Then a PHP Controller of yours will output documents.
  • Sharded path : do not store thousands of documents in the same directory, make different directories. You can shard with a Hash on the Filename for example. Such as /documents/A/F/B/AFB43677267ABCEF5786692/myfile.pdf.
  • Inode number : You can run out of inodes if you store a lot of small files (might not be your case if storing mostly PDF and office documents).


Leave a Reply