4

My SQL Server database has many tables with variety of data types being used. The current design has a standard datatype length for columns ex: NVARCHAR like 200, 1000 or MAX (this is a standard we are adopted) depending on what type of data I saved, I use datatype accordingly.

For a NAME column I believe it does not makes sense to make it 200/1000, forget about MAX (I am not saying we did it). So, is it fine to make a field 200/1000 if you know it for sure occupies not more than 75 characters? Does it have any impact on the query performance at all? What other problems/benefits are there for this approach.

Which way is better, use a standard datatype length irrespective of the data stored or is it good to use a specific datatype length for the column?

Any help in this regard is highly appreciated

Thanks!!!

3 Answers 3

1

Generally I tend to use the smaller types whenever I can, but it's not always possible or convenient, and there are certainly trade-offs in each choice. For one, 200 don't seems a disparate amount but if the data is guaranteed to be limited at 75 chars a smaller type is perhaps a better option. I would analyze the pros and cons for using smaller sizes:

Pros

  • Helps prevent errors from user putting a larger text into controls.
  • Also prevents possible malicious users if a bug appears due to long texts.
  • The client application must be ready to handle longer strings, should the full 200 characters are used.
  • Indexes on that column may be smaller, which helps improving performance.

Cons

  • Not "future-proof". Some day, a name might appear longer than 75 characters, forcing an update in the DB and front-end application. User's needs change with time.
  • UI must gracefully respond to the user entering longer texts, either by not accepting or by displaying a message or something.
  • If you ever want to import data from other systems, those might have longer texts that you must handle.
  • Breaking or changing company standards (200/1000/MAX) may not be easy at all, specially if the team had a long time following it.

Of course you must evaluate your exact situation and if it's worth the change or not. From memory and personal experience I had to balance all those things.

Sign up to request clarification or add additional context in comments.

Comments

0

I always try to keep the data type and size realistic. I do think 200 is way too much and why Nvarchar? I think Varchar(50) or 75 as you think is suffice. It does have an impact on application if you were to create class because an application would have to reserve the byte size causing an application to require more memory. That is just a personal opinion, I'm no expert.

1 Comment

NVARCHAR is important if you want to give full unicode support. With VARCHAR you only get the current ANSI code page, which may give issues with certain characters. It depends on the expected usage really.
0

Don't use MAX. If you use nvarchar(max) or varchar(max) SQL Server will treat it like a large text value, and it will take longer to process.

To answer your question. It is better to use varchar(N), and N should be the largest number you are expecting to store. There is no need to make N 1000, or even 200. 99.999% of names are going to be under this. That being said, there will be a difference in query performance by making it 200, but it will be negligible.

In terms of storage size nvarchar is 2 bytes per length, and varchar is 1 byte per length.

If you are expecting names in (unicode) different characters/language use nvarchar(N).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.