简体   繁体   中英

MySQL vs File Databases

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?

对于小型单用户“数据库”,文件系统可能会更快 - 随着大小和复杂性的增加,像MySQL或SQL Server这样的数据库服务器很难被击败。

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.

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).

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.

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....

This is really cool. It's a PHP class that controls a flat-file database with queries http://www.fsql.org/index.php

For blogs, I recommend caching the pages because blogs usually only have static content. This way, the queries only get run once while caching. You can update the cached pages when a new blog post is added.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM