views:

735

answers:

8

When you save your images (supose you have lots of them) do you store then as blobs in your Database, or as files? Why?

Duplicate of: http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay

+8  A: 

I usually go with storing them as files, and store the path in the database. To me, it's a much easier and more natural approach than pushing them into the database as blobs.

One argument for storing them in the database: much easier to do full backups, but that depends on your needs. If you need to be able to easily take full snapshots of your database (including the images), then storing them as blobs in the database is probably the way to go. Otherwise you have to pair your database backup with a file backup, and somehow try to associate the two, so that if you have to do a restore, you know which pair to restore.

bcwood
if you have a web app, files are the way to go, you build the HTML with the path info from the database that points to the file that is stored where the client browser will pull it down from.
KM
+1  A: 

If I'm running on one web server and will only ever be running on one web server, I store them as files. If I'm running across multiple webheads, I put the reference instance of the image a database BLOB and cache it as a file on the webheads.

chaos
+3  A: 

It depends on the size of the image.

Microsoft Research has an interesting document on the subject

Sean
A: 

Blobs can be heavy on the db/scripts, why not just store paths. The only reason we've ever used blobs is if it needs to be merge replicated or super tight security for assets (as in cant pull image unless logged in or something)

+1  A: 

Given that you might want to save an image along with a name, brief description, created date, created by, etc., you might find it better to save in a database. That way, everything is together. If you saved this same info and stored the image as a file, you would have to retrieve the whole "image object" from two places...and down the road, you might find yourself having syncing issues (some images not being found). Hopefully this makes sense.

Mikey
A: 

By saving you mean to use them to show in a webpage or something like that? If it's the case, the better option will be to use files, if you use a database it will be constantly hammered with the request for photos. And it's a situation that doesn't scale too well.

The Disintegrator
+1  A: 

I've tried to use the db (SQL Server and MySQL) to store medium (< 5mb) files, and what I got was tons of trouble.

1) Some DBs (SQL Server Express) have size limits;

2) Some DBs (MySQL) become mortally slow;

3) When you have to display a list of object, if you inadvertedly do SELECT * FROM table, tons of data will try to go up and down from the db, resulting in a deadly slow response or memory fail;

4) Some frontends (ruby ActiveRecord) have very big troubles handling blobs.

Just use files. Don't store them all in the same directory, use some technique to put them on several dirs (for instance, you could use last two chars of a GUID or last two digits of an int id) and then store the path on db.

giorgian
+1  A: 

The question is, does your application handle BLOBS or other files like other application data? Do your users upload images alongside other data? If so, then you ought to store the BLOBs in the database. It makes it easier to back up the database and, in the event of a problem, to recover to a transactionally consistent state.

But if you mean images which are part of the application infratstructure rather than user data then probably the answer is, No.

APC