Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best strategy for storing documents in SQL Server 2008

One of our teams is going to be developing an application to store records in a SQL2008 database and each of these records will have an associated PDF file. There is currently about 340GB of files, with most (70%) being about 100K, but some are several Megabytes in size. Data is mostly inserted and read, but the files are updated on occasion. We are debating between the following options:

  1. Store the files as BLOBs in the database.

  2. Store the files outside the database and store the paths in the database.

  3. Use SQL2008's Filestream feature to store the files.

We have read the Micrsoft best practices regarding filestream data, but since the files vary in size, we are not sure which path to choose. We are leaning toward option 3 (filestream), but have some questions:

  1. Which architecture would you choose given the amount of data and file sizes noted above?

  2. Data access will be done using SQL authentication, not Windows authentication, and the web server will likely not be able to access the files using Windows API. Would this make filstream perform worse than the other two options?

  3. Since the SQL backups include the filestream data, this would lead to very large database backups. How do others handle backing up databases with a large amount of filestream data?

like image 663
DCNYAM Avatar asked Sep 30 '10 18:09

DCNYAM


People also ask

Can you store documents in a SQL database?

Unstructured files can be stored in the VARBINARY or IMAGE column of a SQL Server table. This approach is effective to maintain transactional consistency and reduces the file management complexity, but when the client application reads data from the SQL table, it uses SQL memory which leads to poor performance.

Is it better to store files in database or filesystem?

Database provides a proper data recovery process while file system did not. In terms of security the database is more secure then the file system (usually).

What data type will you use to save the Word document in SQL table?

In SQL Server, BLOBs can be standard varbinary(max) data that stores the data in tables, or FILESTREAM varbinary(max) objects that store the data in the file system.


2 Answers

OK, here we go. Option 2 is a really bad idea - you end up with untestable integrity constraints and backups that are not guaranteed to be consistent per definition because you can not take point in time backups. Not a problem in MOST scenarios, it turns into one the moment you have a more complicated (point in time) recovery.

Options 1 and 3 are pretty equal, albeit with some implications.

  • Filestream can use a lot more disc space. Basically, every version has a guid, if you make updates the old files stay around until the next backup.
  • OTOH the files do not count as db size (express edition - not against the 10gb limit should you use it) and access is further down possible using a file share. This is added flexibility.

  • In database has the most limited options regarding access (no way for the web server to just open the file after getting the path from the sql - it has to funnel the complete file through the sql protocol layer) but has advantages in regards of having less files (numbers). Putting the blobs into a separate table and that one a separate set of spindles may be strategically a good idea.

Regarding your questions:

1: I would go with in database storage. Try out both - filestream and not. As you use the same API anyway, this is a simple change in the table definition.

2: Yes, worse than direct file access, but it would be more protected than direct file access. Otherwise I do not think filestream and blob make a significant difference.

3: where do you have a huge backup here? Sorry to ask, but your 340gb is not exactly a large database. And you need to back it up ANYWAY. Better do it in one consistent state, which is what you achieve with db storage. Plus integrity (no one accidentally deleting unused documents without cleaning up the database). The DB is not significantly larger than doing that split, and it is a simple one place backup.

At the end, the question is db integrity and ease of backing things up. Win for SQL Server unless you get large - and this means 360 terabyte of data.

like image 54
TomTom Avatar answered Nov 15 '22 23:11

TomTom


Store the files outside the database and store the paths in the database.

because it takes too much space to store files in the database.

like image 25
Beth Avatar answered Nov 15 '22 22:11

Beth