Question

I usually use maximum chars possible for varchar fields, so in most cases I set 255 but only using 16 chars in columns...

does this decreases performance for my database?

Was it helpful?

Solution 2

Whenever you're talking about "performance" you can only find out one way: Benchmarking.

In theoretical terms there's no difference between VARCHAR(20) and VARCHAR(255) if they're both populated with the same data. Keep in mind if you get your length wrong you will have massive truncation problems and MySQL does not warn you before it starts chopping data to fit.

I try to avoid setting limits on VARCHAR columns unless the data would be completely invalid if it was longer. For instance, two-character ISO country codes can be stored in VARCHAR(2) because longer strings are meaningless. For other things, especially names or phone numbers, limiting the length is potentially and probably harmful.

Still, you will want to test any schema you create to be sure it meets your performance requirements. I expect you'd have a hard time detecting any difference at all between VARCHAR(25) and VARCHAR(16).

OTHER TIPS

When it comes to storage, a VARCHAR(255) column will take up 1 byte to store the length of the actual value plus the bytes required to store the actual value.

For a latin1 VARCHAR(255) column, that's at most 256 bytes. For a UTF8 column, where each character can take up to 3 bytes (though rarely), the maximum size is 766 bytes. As we know the maximum index length for a single column in bytes in InnoDB is 767 bytes, hence perhaps the reason some declare 255 as the maximum supported column length.

So, again, when storing the value, it only takes up as much room as is actually needed.

However, if the column is indexed, the index automatically allocates the maximum possible size so that each node in the index has enough room to store any possible value. When searching through an index, MySQL loads the nodes in specific byte size chunks at a time. Large nodes means less nodes per read, which means it takes longer to search the index.

MySQL will also use the maximum size when storing the values in a temp table for sorting.

So, even if you aren't using indexes, but are ever performing a query that can't utilize an index for sorting, you will get a performance hit.

Therefore, if performance is your goal, setting any VARCHAR column to 255 characters should not be a rule of thumb. Instead, you should use the minimum required.

There may be edge cases where you'd rather suffer the performance every day so that you never have to lock a table completely to increase the size of a column, but I don't think that's the norm.

One possible exception is if you are joining on a VARCHAR column between two tables. MySQL says:

MySQL can use indexes on columns more efficiently if they are declared as the same type and size.

In that case, you might use the max size between the two.

There are two ways in which this will decrease performance.

  • if you're loading those columns many many times, performing a join on the column, or other such thing that means they need to be accessed a large number of times. The number of times depends on your machine, but think on the order of millions.
  • if you're always filling the field (using 20 chars in a varchar(20), then the length checks are adding a little overhead whenever you perform an insert.

The best way to determine this though is to benchmark your database though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top