Worst Practice - Defining Rows that Exceed The Max Length

,

SQL unfortunately let's you define a row that exceeds the maximum allowed

length of about 8060 characters. I see it occasionally in the forums where

someone has a table full of varchar(8000) columns. Hurts just to think about it.

The logic is usually something along the lines of "we'll never hit the max size,

we don't fully populate the column or we don't populate all the columns". Etc.

First and most important, why design something that may break someday? By

relying on convention instead of good table design, somewhere down the line some

other developer may well change things and break the self imposed rule that kept

it all working.

Second, to get best performance you want narrow tables so you can get a lot

of rows on a single data page. Stuffing 4-5k or more of text in a row isn't a

good way do that even if you keep the max length of the row under the allowable

limit.

This is what TEXT COLUMNS are designed for! Instead of storing the data in

the row, you store a 16 byte pointer. Not only does it keep the row size down,

it allows you to exceed the 8k limit, up to about 2g. SQL2K even offers the

addition of the text in row option, so that if most of your data falls under a

given threshold (maybe 1-200 bytes), you can keep the smaller values in the

table and push the larger ones out into the text blob. This gives you the

ability to handle those large values if/when they occur but get the performance

of having the data in the row.

If you've got a table that has columns Doc1 varchar(8000), Doc2

varchar(8000), etc, consider changing it to DocType int, DocValue text. This

isn't always a straight port, but when you see repeating entities, it usually

indicates....bad design!

Perhaps in the next version or two we'll get some switches that let us

prevent anyone from designed a row that would exceed the limit. Until then, it's

up to you to keep your users from doing the worst (abusing the row length) and

hopefully doing the best (using text columns appropriately and avoiding

repeating values).

Other stuff you may find interesting:

What

is the Maximum Page Size in SQL Server 2000?

Worst Practices - Part 1 of a Very Long Series! (Both Steve and I have other

WP articles here on the site as well)

 

Rate

5 (1)

Share

Share

Rate

5 (1)