Database table schema schema - varchar (n). Suitable choice N

Based on background C, I may be too anal about this and without too much worry about bits and bytes.

However, I cannot understand how the data is actually stored, and that if I choose N, which is easily factorized to the power of 2, the database will be more efficient in how it packs the data, etc.

Using this β€œlogic”, I have a string field in a table with a variable length of up to 21 characters. I am tempted to use 32 instead of 21 for the reason mentioned above, however now I think I am wasting disk space because there will be space for 11 extra characters that are guaranteed to never be used. Since I plan to store several tens of thousands of lines per day, all this adds up.

Question:

Keeping all of the above in mind, Should I declare varchar (21) or varchar (32) and why?

[change]

The stored data conforms to an external specification and cannot be longer than 21 characters. I use both mySQL and PostgreSQL, but ideally I want the answer to be a database agnostic, as I try not to bind to any particular provider.

+3
source share
7 answers

Let the database implementation perform the optimization. Use the smallest size that makes sense for the application.

, , , . , .

+4

21 , - (, , ), , 21 , . , .

Oh, varchar (x) (x + 2) /, 2 , /.

+2

, . , SQL . SQL Server -.

+1

SQL-, 21 , char (21) varchar (21). ,

  • varchar 2
  • char , , , .
  • varchar

.

+1

varchar(n) , , , n .

0

.

. MSDN:

- , n .

n , n. .

0

What are the business rules in the area you are trying to declare? If he never goes past 21, go straight ahead. But if you are not sure, and the business wants you to have freedom of action, use 32.

Refer to this link

0
source

Source: https://habr.com/ru/post/1746576/


All Articles