Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: How to store binary data (e.g. Word file)?

Tags:

I'm not a DBA, so I just don't know the upsides and downsides of using various methods of storing binary data in an SQL Server.

  • varbinary(n) stores only 8000 bytes
  • varbinary(max) stores 2 GB
  • BLOBs are meant for big files, but come with "administrative" overhead (the files being only referenced in the DB, but actually sitting on the local harddisk)

I basically have about 1000 documents (per year) which I want to store in a SQL Server 2008 R2 database (being filled with a simple ASPX form upload, being viewed by a simple Gridview with download). All documents will probably be around 2MB - 8MB (Word, Excel files). My guess is that I should be using a table MyDocuments with the following layout:

MyDocuments 
    Data       varbinary(max)
    Title      varchar(255)
    ModifiedOn datetime()
    ModifiedBy varchar(100)

Am I on track with varbinary(max)? Or am I going in the wrong direction (e.g. Performance-wise)?

like image 689
Dennis G Avatar asked Dec 15 '10 13:12

Dennis G


People also ask

What 2 formats does SQL Server store binary data in?

Binary, Varbinary & Varbinary(max) are the binary string data types in SQL Server. These data types are used to store raw binary data up to a length of (32K – 1) bytes.

How do you store binary data?

Binary data can be stored in a table using the data type bytea or by using the Large Object feature which stores the binary data in a separate table in a special format and refers to that table by storing a value of type oid in your table.

What are SQL binary files?

In SQL, binary data types are used to store any kind of binary data like images, word files, text files, etc. in the table. In binary data types, we have an option like allowing users to store fixed-length or variable length of bytes based on requirements.


1 Answers

There is 'FILESTREAM' storage in SQL Server 2008. It allows storage of and efficient access to BLOB data using a combination of SQL Server 2008 and the NTFS file system.

You can check these out:

FILESTREAM Storage in SQL Server 2008
Saving and Retrieving File Using FileStream SQL Server 2008

EDIT:
Objects smaller than 256K are best stored in a database while objects larger than 1M are best stored in the filesystem.

Performance: Varbinary vs FILESTREAM

http://www.sqlskills.com/BLOGS/PAUL/post/SQL-Server-2008-FILESTREAM-performance.aspx
To BLOB or Not To BLOB: Large Object Storage in a Database or a Filesystem
SQL Server 2008 FILESTREAM performance

like image 135
Aseem Gautam Avatar answered Jan 12 '23 05:01

Aseem Gautam