开发者

Should I store values in the database or in an XML or text file?

I have a few simple questions about data storage开发者_如何学Python on a website I am developing. The site in question will allow users a personal profile space which they have the ability to customise with text, images, BB code and alike. These profiles will have a character limit of around 5000. I was wondering how best to store this data, as it seems like a large amount to store in the database.

I was thinking about maybe using simple text files to store the data, and name them with the users ID. What do you guys think would be best for access and write speed, and would it detract from database read/write performance; as viewing and updating profiles will be a large part of the website.

Thanks guys. I look forward to hearing your opinions.


I would go for storing them in the database because:

  1. All data in the database can use transactions to keep stuff consistent;
  2. A single insert (transaction) will take care of all your storage needs;
  3. A single select will retrieve all data;
  4. Backup the database and you are done;
  5. If you run into slowness, setting up a master-slave scheme or partitioning is easy;
  6. You can use database tools to search in the data;

On the other hand if you use the filesystem:

  1. filesystems do not have transactions, what happens if an insert fails, and you need to rollback, the file has already been saved;
  2. what happens if the filesystem gets out-of-sync with the database?
  3. It complicates your code, adding to the bugpressure;
  4. Writing to the filesystem adds an extra security hole, because your DB-app can now write and perhaps overwrite existing files on the filesystem.

More generally
Using the filesystem is used because storing large blobs in a db can be slow.
However you should not optimize until slowness sets in. Premature optimization is the root of all evil.
Slowness may never be a problem and besides you can do lots of optimization in the DB as well.


If you’re already using a database, it will be much easier to store it there. 5000 characters is nothing. Performance seems kind of irrelevant here.


Saving things like this into a database is probably the best option. Until you have files that are at least megabytes in size, any decent database should handle them fine.


Is the profile information in any way at all related to the existing database information? Is the structure of the profile information completely regular, or does the user have some degree of control the structure of his or her profile? How many users will you have?

If the profile information is completely separate from the database functionality of the database or if it's decidedly variably structured, and if you have few enough files so as not to bog down the file system, then storing it in separate XML files is worth considering.

If there are a very large number of profiles and the information is amenable to relational storage then I'd probably put it in a database. If it's tied to the functionality of your main database it probably belongs there. If not, you could consider a separate database just for the profiles.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜