Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing files in SQL Server

Tags:

sql-server

It's an old question I know, but with SQL Server 2012 is it finally ok to store files in the database, or should they really be kept in the filesystem with only references to them in the database?

If storing them in the database is considered acceptable these days, what is the most effective way to do it?

I'm planning to apply encryption so I appreciate processing will not be lightning fast.

like image 618
CompanyDroneFromSector7G Avatar asked Nov 16 '12 16:11

CompanyDroneFromSector7G


People also ask

Can I store files in SQL Server?

SQL Server already has the FILESTREAM feature. The FILESTREAM feature provides efficient storage, management, and streaming of unstructured data stored as files on the file system.

How do I save a file in SQL Server database?

You will need to attach it to your SQL Server. The files will be read into a File Stream and then the File Stream will be converted into byte array using BinaryReader in order to save into the database table. Once the File is converted into Byte Array it will be inserted into the database.

Is it a good idea to store files in DB?

Also, keep in mind that Web servers are cheap and you can easily add more to balance the load, whereas the database is the most expensive and hardest to scale part of a web architecture usually. There are some opposite examples (e.g., Microsoft Sharepoint), but usually, storing files in the database is not a good idea.


2 Answers

There's a really good paper by Microsoft Research called To Blob or Not To Blob.

Their conclusion after a large number of performance tests and analysis is this:

  • if your pictures or document are typically below 256K in size, storing them in a database VARBINARY column is more efficient

  • if your pictures or document are typically over 1 MB in size, storing them in the filesystem is more efficient (and with SQL Server 2008's FILESTREAM attribute, they're still under transactional control and part of the database)

  • in between those two, it's a bit of a toss-up depending on your use

If you decide to put your pictures into a SQL Server table, I would strongly recommend using a separate table for storing those pictures - do not store the employee photo in the employee table - keep them in a separate table. That way, the Employee table can stay lean and mean and very efficient, assuming you don't always need to select the employee photo, too, as part of your queries.

For filegroups, check out Files and Filegroup Architecture for an intro. Basically, you would either create your database with a separate filegroup for large data structures right from the beginning, or add an additional filegroup later. Let's call it "LARGE_DATA".

Now, whenever you have a new table to create which needs to store VARCHAR(MAX) or VARBINARY(MAX) columns, you can specify this file group for the large data:

 CREATE TABLE dbo.YourTable      (....... define the fields here ......)      ON Data                   -- the basic "Data" filegroup for the regular data      TEXTIMAGE_ON LARGE_DATA   -- the filegroup for large chunks of data 

Check out the MSDN intro on filegroups, and play around with it!

like image 186
marc_s Avatar answered Oct 01 '22 20:10

marc_s


There's still no simple answer. It depends on your scenario. MSDN has documentation to help you decide.

There are other options covered here. Instead of storing in the file system directly or in a BLOB, you can use the FileStream or File Table in SQL Server 2012. The advantages to File Table seem like a no-brainier (but admittedly I have no personal first-hand experience with them.)

The article is definitely worth a read.

like image 43
David Avatar answered Oct 01 '22 22:10

David