Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nvarchar or varchar what is better use multiply of 2 or rounded full numbers?

My question is what is better to use in generating columns in SQL. Should the size of nvarchar (varchar) be multiply of 2 (32, 64, 128) or it's doesn't matter and we can use fully numbers example '100', '50' ?

Thank You very much for answers with reasons

Greeting's to all

like image 286
szkra Avatar asked Sep 03 '09 17:09

szkra


People also ask

Which is better NVARCHAR or VARCHAR?

The key difference between varchar and nvarchar is the way they are stored, varchar is stored as regular 8-bit data(1 byte per character) and nvarchar stores data at 2 bytes per character. Due to this reason, nvarchar can hold upto 4000 characters and it takes double the space as SQL varchar.

Is NVARCHAR faster than VARCHAR?

Each character of an nvarchar column requires 2 bytes of storage whereas a varchar column requires 1 byte per character. Potentially, varchar will be quicker but that may well mean that you cannot store the data that you need.

When should you use VARCHAR as a datatype?

5. We should use the CHAR datatype when we expect the data values in a column are of the same length. We should use the VARCHAR datatype when we expect the data values in a column are of variable length.

When might you use the attribute VARCHAR as opposed to NVARCHAR?

There is a subtle difference between these two character data types in SQL Server, while both supports variable length, the VARCHAR data type is used to store non-Unicode characters while NVARCHAR is used to store Unicode characters.


2 Answers

Doesn't make any difference. Use the size appropiate for your data.

For instance SQL Server, if you look at the Anatomy of a Record you'll see that your size translates into record offsets that are dependent on the previous record in the table, null values and other factors, specially with row compression and page compression taken into account. By the time the field is accessed, any resemblance with the original declare size relation, vis-a-vis powers of 2 or powers of 10, is long gone. Also various elements higher on a query execution stack like join operators or sort operators or whatever, also would no benefit from powers of 2 sizes (I have no 'proof' linkes, but is OK if you take my word for it...). Neither does the TDS protocol when marshaling data back to client. And I see little benefit in the client too.

like image 189
Remus Rusanu Avatar answered Oct 14 '22 05:10

Remus Rusanu


There's no reason to use multiples of 2. Set the field to match the estimated size of your data.

One number that is worth mentioning though is 255. Some database systems have a maximum varchar type of 255, though this is becoming rarer. I'm thinking mainly here of what are now very old versions of MySql. And so sometimes developers will set the column size at 255 or lower to ensure more portability.

like image 33
Joel Coehoorn Avatar answered Oct 14 '22 04:10

Joel Coehoorn