views:

242

answers:

2

Possible Duplicate:
Storing Images in DB - Yea or Nay?

Which approach is better? What are the advantages and disadvantages?

Advantages of storing images in database, in my opinion, are:

  • database integrity (it's easier to keep all database entries valid with foreign keys than to keep an eye on filesystem and make required changes there during each change in database)
  • less work and easier maintenance (you only need to take care of one place of data storage - database)

Disadvantage would imho be:

  • greater load on database server

Do you agree with the points above?

Currently what I do is I store small images such as avatars and thumbnails in the database as BLOBs and I store large images such as full size photos or wallpapers in the filesystem. Do you think this is a good approach? If yes, is there some magic number (file size) that decides if it's still advantageous to store an image in the database or not? If not, should I store all images just in one place (database or filesystem)?

+1  A: 

IMHO binary data has no place to be in a database. (of course there can be exception to this rule)

Rufinus
Not even little images like avatars or thumbnails?
Richard Knop
+1  A: 

I've had major perf problems attempting to store images in the database. The database size gets a lot larger and queries on tables with BLOB's become a lot slower. It also adds additional complexity your code as you have to use non standard data access to retrieve your images and have to configure for lazy loading. It also significantly increases backup and replication times.

Storing in files is much easier to handle, you can easily backup and rsync only the ones that have changed.

Basically I never attempt to blob anything other than metadata into a database.

mythz
I would argue backup is easier with database because you only need to care about backuping the database, you don't need to backup the filesystem. Also the point about complexity is not a good one, it's exactly the same as with filesystem, you just need to send correct header and mime type to the browser to display the image. And that's one line of code max.
Richard Knop
You can't do incremental backups with blobs in a database, you can with files on a file-system, i.e. only backup the files that have changed.
mythz
@richard: one line of code max... exactly this is the bottleneck, take php for example (but is the same for ruby, java, perl too) you need php to connect to the database and send the headers, this means you most likly have a huge apache process. having files in the filesystem let you use eg. lighttpd for this files, which can deliver images to the clients much faster then you can when they are in the db.
Rufinus