SQL Server Central is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 

Worst Practice - Defining Rows that Exceed The Max Length

By Andy Warren, 2003/12/08

Total article views: 8347 | Views in the last 30 days: 35

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)

 

By Andy Warren, 2003/12/08

Total article views: 8347 | Views in the last 30 days: 35
Your response
 
 
Related tags
 
Already registered?  

Free registration required

To read the rest of this article, and access thousands of other articles, we ask you to register on the site and subscribe to our newsletters.

Register

E-mail address:
Password:
Password (confirm):

  

Subscriptions

We ask you to register on the site and subscribe to our newsletters. Subscribing to our newsletters gets you:

  • ALL of our content (thousands of articles, scripts, and forum postings)
  • A daily newsletter (example)
  • A weekly news round up (example)
  • The opportunity to ask and answer questions in our forums
  • A daily Question of the Day to test and help you increase your knowledge of SQL Server.

We ask that you give the newsletter a try for a week. Over 200,000 SQL Server Professionals a day find it entertaining and useful. If not, you are welcome to unsubscribe at anytime.

Steve Jones
Editor, SQLServerCentral.com