tags:

views:

510

answers:

7

So I'm going to be working on a home made blog system in PHP and I was wondering which way of storing data is the fastest. I could go in the MySQL direction, or I could go with my own little way of doing it which is storing all of the information (encoded in JSON) in files.

Which way would be the fastest, MySQL or JSON files?

+2  A: 

For a small, single user 'database', a file system would likely be quicker - as the size and complexity grows, a database server like MySQL or SQL Server is hard to beat.

EJB
Disadvantage with FS is you only have one key (file-name). Any other data is opaque. Also other key constraints are not enforced.
Martin York
A: 

Though I don't know for certain, it seems to me that a MySQL database would be a lot faster, especially as the amount of data gets larger and larger.

Also, using MySQL with PHP is super easy, especially if you use an abstraction class like ezSQL. ezSQL makes working with a database really simple and I think you'd be creating more unnecessary work for yourself by going the home-brewed JSON direction.

Justin Bennett
+1  A: 

I would choose a MySQL database - simply because it's easier to manage.

JSON is not really a format for storage, it's for sending data to JavaScripts. If you want to store data in files look into XML or Serialized PHP (which I suspect is what you are after, rather than JSON).

Ross
XML isn;t made for storage either, XML is designed for data transportation between different applications.
Pim Jager
You don't necessarily want to be storing any serialised form in the long term, either, since the serialisation format could change in a way that's incompatible with previous versions.
Rob
@Pim, XML is actually great for storage if you want data portability and want to leverage XPath/SimpleXML out of the box.
aleemb
-1, because you say JavaScript is for sending data to "JavaScripts" while XML or serialized PHP(?) is good for storing data.
Ionuț G. Stan
@Ionut: I was under the impression JSON is not designed for storage but for providing data. I may have worded it poorly - if you can help I'd appreciate it.
Ross
Don't forget that there are loads of resources on the web describing how to set up blog systems using PHP and MySQL. There are probably a lot fewer tutorials/examples/etc. using JSON or XML encoded flat files. I think in this case development speed is more important than theoretical performance.
GloryFish
+1  A: 

Forgive me if this doesn't answer your question very directly, but since it is a homecooked blog system is it really worth spending time thinking about what storage backend right now is faster?

You're not going to be looking at 10,000 concurrent users from day 1, it doesn't sound like it will need to scale to any maningful degree in the foreseeable future.

Why not just stick with MySQL as a sensible choice rather than a fast one? If you really want some sense that you designed for speed maybe bolt sqlite on instead.

JosefAssad
+1  A: 

Since you are thinking you may not have the need for a complex relational structure, this might be a fun opportunity to try something more down the middle.

Check out CouchDB, it is a document-based, schema free database (yet still indexable). The database is made of documents that contain named fields (think key-value pairs).

Have fun....

Rydell
+2  A: 

I would definately choose a DB option (as you need to be able to search and index stuff). But that does not mean you need a fully realized separate DB service.

MySQL is definitely the more scalable solution.
But the downside is you need to set up and maintain a separate service.

On the other hand there are DBs that are file based and still give you access with standard SQL (SQLite SQLite.org) jumps to mind. You get the advantages of SQL but you do not need to maintain a separate service. The disadvantage is that they are not as scalable.

Martin York
A: 

I've done both. I like files for very simple problems and databases for complicated problems.

For file solutions, note these problems as the number of files increases:

1) Much more disk space is used than you might expect, because even tiny files use up a whole block. Blocks are fairly large on filesystems which support large drives.

2) Most filesystems get very slow when the number of files in a directory gets very large. My solution to this (assuming the names of the files are reasonably spread out across the alphabet) is to create a directory consisting of the first two letters of the filename. Thus, the file, "animal.txt" would be found at an/animal.txt. This works surprisingly well. If your filenames are not reasonable well-distributed across the alphabet, use some sort of hashing function to create the directories. Sounds a little crazy, but this can work very, very well, and I've used it for very fast solutions with tens of thousands of files.

But the file solutions really only fit sometimes. Unless you have a great reason to go with files, use a database.