Should I limit the size of VARCHAR columns for performance?

6

Normally the maximum size for VARCHAR is 255.

If I create the column with a smaller size, say VARCHAR (50), will I gain performance or save disk space?

    
asked by anonymous 11.06.2015 / 14:30

1 answer

7

The varchar is dynamically allocated, if I have a 30-character var char and write a value with only 10 it will only occupy 10 positions.

This is true for most market sgbds (MSSQL, Oracle, MySql, Postgree, Firabird, MariaDb ...).

Oracle also has varchar2, which supports a much larger number of characters, but works in the same way as varchar.

That said, limiting the size will not bring performance gains but will certainly optimize the space occupied by the table.

EDIT

If I have two varchar fields, one of 255 and one of 15 fields. Filling them with 15 characters at the end of the day will have the same result in terms of allocated space, since sgbd will only allocate 15 characters for each one .

There are some reasons to limit the size of varchar, the most important are:

  • Prevent the bank from growing uncontrollably, eg:

It does not make sense to have a varchar of 255 if I want to save a phone number. This way the bank will complain if the user can include some random value.

  • More accurate growth indicators

Sometimes you design the database and use tools that estimate database growth over time. This is important for large companies and public bodies that rely on bidding for the acquisition of new computing resources.

Do not limit the size of each varchar can make my indicator less accurate.

  • Leave the template clear to the developer

It's interesting that the developer knows how much each field supports so that it also limits the amount of characters typed in each field.

Fonts

varchar is defined in sql ansi ( available here ) , in addition to being addressed in the official documentation of your DBMS. So what I said is there. It does not make much sense to post here because it would be too long.

EDIT 2

The link I posted is just about the changes in the default in 2003 and may not have exactly what you are looking for. These patterns are often paid, so I could not find it for you ... :(

    
11.06.2015 / 14:43