Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server varbinary(max) and varchar(max) data in a separate table

Tags:

sql-server

using SQL Server 2005 standard edition with SP2

I need to design a table where I will be storing a text file (~200KB) along with filename ,description and datetime.

Should we design a table where varchar(max) and varbinary(max) data should be stored in a separate table or should column of LOB data types be part of the main table?

Per this thread What is the benefit of having varbinary field in a separate 1-1 table?

there is no performance or operational benefits which I agree to some extent however I can see two benefits

  1. store those into a separatable table that can be stored on a separate file group
  2. you can not rebuild index on a table containing lob data type ONLINE

Any suggestions would be appreciated.

like image 817
SQL Learner Avatar asked Jan 02 '12 18:01

SQL Learner


People also ask

What is the difference between varchar Max and NVARCHAR Max?

The key difference between varchar and nvarchar is the way they are stored, varchar is stored as regular 8-bit data(1 byte per character) and nvarchar stores data at 2 bytes per character. Due to this reason, nvarchar can hold upto 4000 characters and it takes double the space as SQL varchar.

Can I use Max on varchar?

CHAR, VARCHAR, and VARCHAR(MAX) CHAR columns should be used for columns that vary little in length. String values that vary significantly in length and are no longer than 8,000 bytes should be stored in a VARCHAR column. If you have huge strings (over 8,000 bytes), then VARCHAR(MAX) should be used.

Why is my varchar Max variable getting truncated?

The reason this happens is that SQL Server doesn't want to store something as VARCHAR(MAX) if none of the variable's components are defined as VARCHAR(MAX).


1 Answers

I would advise against separation. It complicates the design significantly for little or no benefit. As you probably know, SQL Server already stores LOBs on separate allocation units, as described in Table and Index Organization.

Your first concern (separate filegroup allocation for the LOB data) can be addressed explicitly, as Mikael has already pointed out, by appropriately specifying the desired filegroup in the CREATE TABLE statement.

Your second concern is no longer a concern with SQL Server 2012, see Online Index Operations for Indexes containing LOB columns. Even prior to SQL Server 2012 you could reorganize indexes with LOBs without problems (and REORGANIZE is online). Given that a full index rebuild is a very expensive operation (an online rebuild must be done at the table/index level, there is no partition online rebuild options), are you sure you want to complicate the design to accommodate for something that is, on one hand, seldom required, and on the other hand, will be available when you upgrade to SQL 2012?

like image 94
Remus Rusanu Avatar answered Sep 22 '22 08:09

Remus Rusanu