SQL Server - Warning! The maximum key length is 900 bytes……

Who is online?  379 guests and 0 members
home  »  blogs  »  prince rastogi  »  SQL Server - Warning! The maximum key length is 900 bytes……
  Rate This Blog Entry:  register  or  login

Author

princerastogi prince rastogi (Member since: 2/27/2012 5:51:06 PM)

View prince rastogi 's profile

Comments (3)

raunak.jhawar
8/8/2012 4:58:55 PM Raunak Jhawar said:

Perhaps, if someone from the team can write more on covering index. That would be just great to understand this content in greater detail.

by
princerastogi
8/8/2012 5:16:20 PM prince rastogi said:

Hi Raunak,

Actually here i just want to explain the warning, in my next blog i will explain Covering Indexes. 

by
marc_jellinek
8/10/2012 2:16:59 PM Marc Jellinek said:

CREATE TABLE [dbo].[Cities] ( [CityId] [int] IDENTITY, [CityName] [varchar](400) ) CREATE TABLE [dbo].[xtTest] ( [id] [int] NOT NULL, [name] [varchar](50) NOT NULL, [fkCityId] [int] NOT NULL, [description] [varchar](500) NOT NULL, CONSTRAINT [FK_dbo_xtTest___fkCityId] FOREIGN KEY ([fkCityId]) REFERENCES [dbo].[Cities]([CityId]) ) CREATE VIEW [dbo].[vxtText] ( SELECT [xt].[id], [xt].[name], [c].[CityName] as [City], [xt].[description] FROM [dbo].[xtTest] [xt] INNER JOIN [dbo].[Cities] [c] ON [c].[CityId] = [xt].[fkCityId] )

How about teaching basic normalization?

By breaking out City into a separate table, you gain the following benefits:

  1. Data Quality.  If a city doesn't exist in [dbo].[Cities], it can't be referenced in [dbo].[xtTest].  This way you aren't storing typos and mistakes and wind up with several misspellings of the same city.
  2. Storage Efficiency.  A city name takes up space exactly once.  After that, each reference to the City takes up the space required to store an [int] (4 bytes)
  3. Index efficience.  Any time a column refering to a city is referenced, the index entry takes up 4 bytes, not the length of the name of the city
  4. Processing Efficiency.  An [int] is a native processor type, it doesn't require any pre-processing to be evaluated.  String types must first be cast to a byte array of integers; something that happens fairly quickly, but not as fast as a processor evaluating an integer directly.

You need to do just a few more things to make this as fast as a native table:

Create an index on the view.  That gives the view its own clustered index.  All values are read from the clustered index, just like a native table.

Create an INSTEAD OF trigger on the view that redirects INSERT, UPDATE and DELETE statements on the view.  This way the view looks and acts exactly the same as the table you originally described.  This is easy to generate directly out of system tables like sys.foreign_keys and sys.foreign_key_columns.

The warning is exactly that... a warning that you have likely done something wrong.  In this case, it was the table design, which led to the requirement of an index which exceeds SQL Server's ability to handle.

When you cover INCLUDES be sure to cover the storage impact of having a table filled with varchar columns and an index that includes all of them.  While the INCLUDEd columns are not counted towards the 900 byte limit, the INCLUDEd columns are still included in the index storage... which means you are storing the values twice.  You've now doubled your storage requirement.

by

Leave a comment


 
Type the characters:
 *
 
   

Training on Microsoft Products & Technologies

Email Subscriptions

   Get the Most Recent Blogs in your inbox
Blogs RSS Feed

prince rastogi's latest blog posts

Blogs RSS Feed

Latest community blog posts

  • A few days back I was resolving a memory issue which caused sql server to stop responding. I was able to figure out the issue by looking into DBCC MemoryStatus output. It was the full text search whic...
  • This is an update to the DB-Migrate ( http://www.sqlservergeeks.com/blogs/AhmadOsama/personal/797/db-migrate-a-powershell-module-to-migrate-databases ) powershell module to migrate database between sq...
  • This blog is part of the series The TSQL Classes: Stored Procedure The links to previous post are given below. http://sqlservergeeks.com/blogs/AhmadOsama/personal/762/the-tsql-classes-stored-procedure...
  • Reporting With SSRS Part 1 : In this serires of blogs, i will try to introduce you with some of the key features of the tool which will help you get on board to become a proficient report developer.If...
  • Hi Folks, I am sure that all of you would be aware about the role of Identity column in a table, i.e., it is column which keeps on incrementing without supplying the value explicitly during insertion....
  • Recently I wrote powershell scripts to move databases, logins and SQL Agent Jobs between instances. Another one I wrote was to fix orphan users. I have now combined all of these different functions in...
  • Hi SQL Geeks, Here are the blog posts by Ahmad Osama for the month of April. We would expect more of his informative blogs this month. Happy Learning! If you like our blogs do like us on Facebook at h...
  • This blog is part of the series The TSQL Classes: Stored Procedure The link to previous post are given below. http://sqlservergeeks.com/blogs/AhmadOsama/personal/762/the-tsql-classes-stored-procedures...
  • As DBAs we are always challenged of unexpected size growth of log or data files and need to shrink files based on available free space in each file. Most of the times we are stuck and do a lot of scri...
  • One of the major migration activities is to fix orphan users. Though, it can be easily fixed by sp_change_users_login procedure, however what if you need to do this for multiple servers. A powershell ...
  • This blog is part of the series The TSQL Classes: Stored Procedure. http://www.sqlservergeeks.com/blogs/AhmadOsama/personal/764/the-tsql-classes-stored-procedures-nested-proc-recursion-and-plan-cachin...