17.07.2015 Views

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2: Getting Started with <strong>FILESTREAM</strong>A T-<strong>SQL</strong> batch or stored procedure can access <strong>FILESTREAM</strong> columns as if they areregular VARBINARY(MAX) columns. However, accessing large BLOB data stored in a<strong>FILESTREAM</strong> column from T-<strong>SQL</strong> does not perform well, for the reasons below.• One <strong>of</strong> the features that <strong>FILESTREAM</strong> <strong>of</strong>fers is the I/O streaming capability for faster<strong>FILESTREAM</strong> operations. When accessing <strong>FILESTREAM</strong> data through T-<strong>SQL</strong>, thebenefits <strong>of</strong> streaming I/O are not available.• When accessing <strong>FILESTREAM</strong> data using T-<strong>SQL</strong>, <strong>SQL</strong> <strong>Server</strong> memory is usedfor buffering the <strong>FILESTREAM</strong> data. This will add memory overhead to the <strong>SQL</strong><strong>Server</strong> instance.• To T-<strong>SQL</strong>, a <strong>FILESTREAM</strong> column appears as a VARBINARY(MAX) column with a2 GB size limitation. <strong>The</strong>refore, if the data stored in the <strong>FILESTREAM</strong> column islarger than 2 GB, T-<strong>SQL</strong> can only access the first 2 GB <strong>of</strong> it.<strong>The</strong>refore, for best performance and optimum usage <strong>of</strong> the <strong>SQL</strong> <strong>Server</strong> resources,<strong>FILESTREAM</strong> data should not generally be accessed using T-<strong>SQL</strong>. Instead, we shoulduse the streaming APIs exposed by <strong>SQL</strong> <strong>Server</strong>, as described in Chapter 3: Accessing<strong>FILESTREAM</strong> Data from Client Applications.However, as always, there are exceptions to the rule. Most significantly, unless the<strong>FILESTREAM</strong> column is assigned a default value, then a client application has to executea T-<strong>SQL</strong> INSERT statement in order to add a new row into a <strong>FILESTREAM</strong>-enabledtable, rather than use the streaming APIs. <strong>The</strong> reason for this is that streaming accesscan be made only to rows that contain non-NULL BLOB data in the <strong>FILESTREAM</strong>column. So, T-<strong>SQL</strong> INSERT statements on <strong>FILESTREAM</strong>-enabled tables usually storedummy VARBINARY data in the <strong>FILESTREAM</strong> column, or a default value can be set onthe <strong>FILESTREAM</strong> column. For example, we could insert a dummy value, or use a defaultvalue, <strong>of</strong> 0x. This will create a zero-length file, which can then be replaced by actual<strong>FILESTREAM</strong> BLOB data using streaming I/O (or T-<strong>SQL</strong>).71

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!