Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the optimum varchar sizes for MySQL?

Tags:

mysql

varchar

How does MySQL store a varchar field? Can I assume that the following pattern represents sensible storage sizes :

1,2,4,8,16,32,64,128,255 (max)

A clarification via example. Lets say I have a varchar field of 20 characters. Does MySQL when creating this field, basically reserve space for 32 bytes(not sure if they are bytes or not) but only allow 20 to be entered?

I guess I am worried about optimising disk space for a massive table.

like image 616
ae. Avatar asked Jul 20 '09 04:07

ae.


People also ask

What size should a VARCHAR be?

The size of the maximum size (m) parameter of a VARCHAR column can range from 1 to 255 bytes. If you are placing an index on a VARCHAR column, the maximum size is 254 bytes. You can store character strings that are shorter, but not longer, than the m value that you specify.

Does VARCHAR size matter MySQL?

Yes, is matter when you indexing multiple columns. Prefixes can be up to 1000 bytes long (767 bytes for InnoDB tables). Note that prefix limits are measured in bytes, whereas the prefix length in CREATE TABLE statements is interpreted as number of characters.

What does VARCHAR 20 mean in MySQL?

@NIMISHAN (20) is the length of string you will be inserting in the table. suppose social security number is of 9 digit, therefore its length is 9 and it can be easily put in column of datatype VARCHAR(20), but it will cause error in VARCHAR(2)

Does VARCHAR size affect performance?

DATALENGTH() returns the number of bytes it uses. oh, then you and John are both saying that if varchar datatype size is much bigger than actual length of data, this will not affect query performance or indexing. Declared varchar column length will not affect the physical (on-disk) or data cache storage.


1 Answers

To answer the question, on disk MySql uses 1 + the size that is used in the field to store the data (so if the column was declared varchar(45), and the field was "FooBar" it would use 7 bytes on disk, unless of course you where using a multibyte character set, where it would be using 14 bytes). So, however you declare your columns, it wont make a difference on the storage end (you stated you are worried about disk optimization for a massive table). However, it does make a difference in queries, as VARCHAR's are converted to CHAR's when MySql makes a temporary table (SORT, ORDER, etc) and the more records you can fit into a single page, the less memory and faster your table scans will be.

like image 197
Kris Erickson Avatar answered Sep 22 '22 07:09

Kris Erickson