Mysql – Varchar storage and performance

MySQLvarchar

I have a simple query, It might be answered before but I didn't find the precise answer for the same.

My question is:

I created a table using JPA through entity. Now I shows string fields as varchar(255). Now I'm wondering that what will happen when I'll save some data in that field of size let say 10? Will it save that data in 10 bytes or it will use 255 bytes?

Any clarification will be appreciated.

Best Answer

I thought in MySql up-to 255 chars with a varchar it is the number of characters you store plus 1 for length. So if you put a value that's 10 characters long, that'll be 11 bytes on disk.

UPDATE Found a link to the documentation to back that up. First table demonstrates the difference between char and varchar: https://dev.mysql.com/doc/refman/8.0/en/char.html