HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Does the principle of having an "ever increasing index" also apply for NVARCHAR and VARCHAR?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
indexprinciplethenvarcharapplyhavingvarcharalsoincreasingdoes

Problem

This might sound like an amateur question, but I just need to be 100% sure and I haven't found anything on it online.

It is considered good practice having an "ever increasing index". Now, does that rule also apply to NVARCHAR and VARCHAR?

Let's say I insert "abc", then "def" and then "ghi". Is this considered "increasing"?

Note: I am well aware that it is bad convention to use an NVARCHAR and VARCHAR as index.

Solution

If I understand your question:

It is a good practice to have an ever increased index - that prevents page splits - read about this issue if you are not familiar with it. It is correct also for char values. To make it simple - sql server will fill page after page without having to insert rows in the middle. Think about it as filling phone book - it is much easier to fill it in abc order than randomly insert names.

So the order "abc", then "def" and then "ghi" is ever increasing.

Don't know why do you think that using char columns as indexes is a bad practice - if you need them there you should create them. Yes, creating logic that will enforce ever increasing char insertion is difficult and sometimes impossible, but it still shouldn't prevent you from creating indexes on those columns.

With all that said if you need to enforce ever increasing clustered index you should probably use numbers(identity or sequence)

Context

StackExchange Database Administrators Q#125340, answer score: 2

Revisions (0)

No revisions yet.