每次设置新的SQL表或向现有表中添加新的varchar
列时,我都想知道一件事:length
的最佳值是什么.
So, lets say, you have a column called name
of type varchar
. So, you have to choose the length. I cannot think of a name > 20 chars, but you will never know. But instead of using 20, I always round up to the next 2^n number. In this case, I would choose 32 as the length. I do that, because from an computer scientist point of view, a number 2^n looks more even
to me than other numbers and I'm just assuming that the architecture underneath can handle those numbers slightly better than others.
另一方面,当您 Select 创建varchar列时,MSSQL server会将默认长度值设置为50.这让我想起了它.为什么是50?它只是一个随机数,还是基于平均列长度,还是什么?
不同的SQL Server实现(如MySQL、MSSQL、Postgres等)也可能是(或者可能是)具有不同的最佳列长度值.