views:

356

answers:

5

Hello,

I am planning the development of a photo gallery application for a client. I am developing the app in asp.net 3.5 and would like to develop it so that I can re-use the application across multiple platforms using various front-ends. Basically, I am wondering what are the dis-advantages and advantages of storing images in the database as binary files as opposed to simply storing the files in an application folder.

Any advice would be greatly appreciated!

Thanks, Tristan

+6  A: 

The disadvantage of storing as binary is that you blow the database size to incredible sizes. If you were to use an express edition of SQL Server, which is limited to 4GB per database, you photo gallery would "finish" quite soon.

The advantage is that you can easily manipulate the access restrictions per file and per user. You just look at user rights and decide whether you server back the image or not.

Developer Art
This is a very good point, I had planned on using SQL Server Express.
TGuimond
I don't think there is any advantage of storing images inside database at all. Access restrictions data can be easily placed as a field next to imagename field.
FractalizeR
I agree that file system is better (and store the path), but keep in mind that putting the images in the database also offers transactional consistency. In SQL Server 2008 you can also investigate FILESTREAM, which is the best of both worlds: you store the images in the database but it streams to/from the file system, so you get transactional consistency and other inherent DB advantages, without exploding your database size.
Aaron Bertrand
Frank Kalis
very good points.. +1
jinsungy
+17  A: 

SQL Server 2008 supports FILESTREAM storage.

The files are stored on an NTFS volume like plain files, but are subject to transaction control and can be accessed via special file names passed to Win32 API functions (and of course any API built upon it) with additional SQL Server security checks (like GRANT options etc).

Quassnoi
+4  A: 

File system storage will offer much better performance in saving and serving images, and is supported in every platform. If you can live without the security and transactional goodies you get from db storage, then I would go with the file system.

Ray
Cheers, thats what I'm gonna do.
TGuimond
+1  A: 

This debate has been going on in almost any SQL Server community for ages. there are good arguments for both sides and there is definitely not just one size fits all answer. It really depends on your individual situation and on many factors, such as number of users, avg. file size, update frequency, read/write ratio, disk-subsystem yadayadayada...

But as you mention SQLExpress probably the most important factor is the max database size limit and this is a very good reason to go for the filesystem approach. Anyway, this research paper might still be interesting for you: To BLOB or Not To BLOB: Large Object Storage in a Database or a Filesystem?

This paper used to be on the Microsoft Research site here: http://research.microsoft.com/apps/pubs/default.aspx?id=64525, but that link doesn't work for me. SQL Server has come a long way since then. Quassnoi already mentioned FILSTREAM, for example.

Frank Kalis
+2  A: 

We had large LOB application that provided Bank tellers identification information about the member standing in front of them. Our textual data was stored in SQL Server. Image data was stored in files. The database field simply had a filename. This approach works well if you are behind the firewall. Reading and writing files is easy. The trouble is the file management. You should secure the file system so that random people cannot view the directory. Also, backups are more complicated with loose image files. You have to backup the database and the image files. The fields can reference paths that no longer exist. For example, some IT dude decides to move the image folder and now all the references in the db are broken. If your application needs to pass information through the firewall, I would suggest storing images in the SQL Server using the mentioned FileStream storage.

Storing the images in the database would have saved us some grief. We would have only had to backup the database, it would be more secure, the references would never break and we would not have had to jump through hoops to get files from network outside the firewall.

Steve
@Steve: it's a `FI_L_ESTREAM`. It only passes through a filewall.
Quassnoi
@Quassnoi, good catch. A FireStream type would be pretty cool though.
Steve
Maybe if you were calling a web service through XFire http://xfire.codehaus.org/
Ken Liu