Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing post bodies in database or files?

Tags:

php

mysql

blogs

I'm learning web-centric programming by writing myself a blog, using PHP with a MySQL database backend. This should replace my current (Drupal based) blog.

I've decided that a post should contain some data: id, userID, title, content, time-posted. That makes a nice schema for a database table. I'm having issues deciding how I want to organize the storage of content, though.

I could either:

  1. Use a file-based system. The database table content would then be a URL to a locally-located file, which I'd then read, format, and display.
  2. Store the entire contents of the post in content, ie put it into the database.

If I went with (1), searching the contents of posts would be slightly problematic - I'd be limited to metadata searching, or I'd have to read the contents of each file when searching (although I don't know how much of a problem that'd be - grep -ir "string" . isn't too slow...). However, images (if any) would be referenced by a URL, so referencing content would at least be an internally consistant methodology, and I'd be easily able to reuse the content, as text files are ridiculously easy to work with, compared to an SQL database file.

Going with (2), though, I could use a longtext. The content would then need to be sanitised before I tried to put it into the tuple, and I'm limited by size (although, it's unlikely that I'd write a 4GB blog post ;). Searching would be easy.

I don't (currently) see which way would be (a) easier to implement, (b) easier to live with.

Which way should I go / how is this normally done? Any further pros / cons for either (1) or (2) would be appreciated.

like image 336
simont Avatar asked Mar 07 '12 19:03

simont


1 Answers

For the 'current generation', implementing a database is pretty much your safest bet. As you mentioned, it's pretty standard, and you outlined all of the fun stuff. Most SQL instances have a fairly powerful FULLTEXT (or equivalent) search. You'll probably have just as much architecture to write between the two you outlined, especially if you want one to have the feature-parity of the other.

The up-and-coming technology is a key/value store, commonly referred to as NoSQL. With this, you can store your content and metadata into separate individual documents, but in a structured way that makes searching and retrieval quite fast. Some common NoSQL engines are mongo, CouchDB, and redis (among others).

Ultimately this comes down to personal preference, along with a few use-case considerations. You didn't really outline what is important to you as far as conveniences and your application. Any one of these would be just fine for a personal or development blog. Building an entire platform with multiple contributors is a different conversation.

like image 81
Morgon Avatar answered Oct 11 '22 14:10

Morgon