The difference between MySQL varchar data type length limit and other character types in detail

 

char

Fixed length, up to 255 characters, the space at the end will be deleted by default

When to choose char type storage?

1 The data length is similar to the value after MD5 encryption of mobile phone number ID

2 Short string relative to varchar can save a storage length space

3 Frequently updated fields will not change in length compared to varchar and will not generate storage fragmentation

 

varchar

Varchar type is different from char type for variable length string

Use one byte to store the length when the character length does not exceed 255

Use two bytes to store the length when it exceeds 255

The sum of varchar in each line must not exceed 65535 bytes. If you want to store longer strings, it is recommended to use text format

Since varchar is variable length and it uses one byte to store the length when it does not exceed 255 characters, does it mean that varchar(4) and varchar(100) are complete when storing four-character content such as "abcd" The same?

  MySQL uses a fixed width for the varchar field in memory when querying instead of the variable length width during storage, especially the invisible temporary table created during query, so it is appropriate to choose the field attribute according to your own business to choose the most suitable and smallest The length of the to improve query speed and reduce the overhead of the database server

 

When to choose varchar column for storage?

1 The maximum length of the string column is much larger than the average length. Give full play to the characteristics of variable length

2 String columns are rarely updated columns. Because a memory page of the InnoDB engine is 16k, frequent updating of variable-length fields may cause memory fragmentation due to the fragmentation of memory pages 

3 Multi-byte character set such as utf-8 

1. Varchar type changes

The maximum length of the varchar type of MySQL database in versions below 4.1 is limited to 255, and the data range can be 0~255 or 1~255 (depending on the database of different versions). In MySQL 5.0 and above, the length of the varchar data type is supported to 65535, which means that 65,532 bytes of data can be stored, and the start and end bits occupy 3 bytes, that is, in 4.1 Or the data in the following versions that need to be stored in a fixed TEXT or BLOB format can be stored in a variable-length varchar, which can effectively reduce the size of the database file.

The varchar type of MySQL database is in the version below 4.1, nvarchar (stores the characters of the Unicode data type) whether it is a character or a Chinese character, it is stored as 2 bytes, and it is generally used for Chinese or other language input. Easy to garbled; varchar: Chinese characters are 2 bytes, other characters are stored as 1 byte, varchar is suitable for inputting English and numbers.

Below version 4.0, varchar(20) refers to 20 bytes. When storing UTF8 Chinese characters, only 6 (3 bytes per Chinese character) can be stored; above version 5.0, varchar(20) refers to 20 characters , Regardless of whether it is storing numbers, letters or UTF8 Chinese characters (each Chinese character is 3 bytes), 20 can be stored, and the maximum size is 65532 bytes; varchar(20) is only 20 bytes in Mysql4, but Mysql5 has different storage sizes according to different encodings. The specific rules are as follows:

a) Storage restrictions

The varchar field stores the actual content separately outside the clustered index. The beginning of the content uses 1 to 2 bytes to indicate the actual length (2 bytes are required when the length exceeds 255), so the maximum length cannot exceed 65535.

b) Encoding length limit

If the character type is gbk, each character occupies a maximum of 2 bytes, and the maximum length cannot exceed 32766;

If the character type is utf8, each character occupies at most 3 bytes, and the maximum length cannot exceed 21845.

If the definition exceeds the above limit, the varchar field will be forcibly converted to text type and a warning will be generated.

c) Line length limit

What causes the varchar length limit in practical applications is the length of a line definition. MySQL requires that the defined length of a row cannot exceed 65535 . If the defined table length exceeds this value, it will prompt

ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBs。

2. The difference between CHAR(M) and VARCHAR(M)

The length of the column defined by CHAR(M) is fixed. The value of M can be between 0 and 255. When saving CHAR values, fill spaces on their right to reach the specified length. When the CHAR value is retrieved, the trailing space is deleted. No case conversion is performed during storage or retrieval. CHAR is very convenient to store fixed-length data. The index on the CHAR field is highly efficient. For example, if you define char(10), then no matter whether the data you store reaches 10 bytes, it will take up 10 bytes of space, which is insufficient Is automatically filled with spaces.

The length of the column defined by VARCHAR(M) is a variable-length character string. The value of M can be between 0 and 65535. (The maximum effective length of VARCHAR is determined by the maximum row size and the character set used. The overall maximum length is 65,532 words Section). When VARCHAR value is saved, only the required number of characters is saved, and one byte is added to record the length (if the declared length of the column exceeds 255, then two bytes are used). VARCHAR values ​​are not filled when they are saved. When the value is saved and retrieved, the trailing space is still retained, which conforms to standard SQL. Varchar stores variable-length data, but the storage efficiency is not as high as CHAR. If the possible value of a field is of variable length, we only know that it cannot exceed 10 characters, and it is most cost-effective to define it as VARCHAR(10). The actual length of the VARCHAR type is the actual length of its value + 1. Why "+1"? This one byte is used to store the actual length used. In terms of space, varchar is appropriate; in terms of efficiency, char is appropriate. The key is to find a trade-off based on the actual situation.

The biggest difference between CHAR and VARCHAR is that one is a fixed length and the other is a variable length. Since it is a variable length, the actual string is actually stored plus one byte to record the length of the string (if it exceeds 255, two bytes are required). If the value assigned to a CHAR or VARCHAR column exceeds the maximum length of the column, the value is clipped to make it fit. If the trimmed character is not a space, a warning will be generated. If you crop non-space characters, it will cause an error (not a warning) and disable the insertion of values ​​by using strict SQL mode.

3. The difference between VARCHAR and TEXT and BlOB types

VARCHAR, BLOB and TEXT types are variable-length types, and their storage requirements depend on the actual length of the column value (indicated by L in the previous table), rather than the maximum possible size of the type. For example, a VARCHAR(10) column can store a string with a maximum length of 10 characters. The actual storage needs to be the length of the string, plus 1 byte to record the length of the string. For the string'abcd', L is 4 and the storage requirement is 5 bytes.

The BLOB and TEXT types require 1, 2, 3, or 4 bytes to record the length of the column value, depending on the maximum possible length of the type. VARCHAR needs to define the size, with a maximum limit of 65535 bytes; TEXT does not need it. If you assign a value that exceeds the maximum length of the column type to a BLOB or TEXT column, the value is truncated to fit it.

A BLOB is a large binary object that can hold a variable amount of data. The four BLOB types TINYBLOB, BLOB, MEDIUMBLOB and LONGBLOB differ only in the maximum length of values ​​they can store.

BLOB can store pictures, TEXT cannot, TEXT can only store plain text files. The four TEXT types TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT correspond to the four BLOB types and have the same maximum length and storage requirements. The only difference between BLOB and TEXT types is that the sorting and comparison of BLOB values ​​are performed in a case-sensitive manner, while TEXT values ​​are case-insensitive. In other words, a TEXT is a case-insensitive BLOB.

4. Summarize the difference between char, varchar and text

The difference in length, char range is 0 ~ 255, varchar is the longest 64k, but note that 64k here is the length of the entire row, take into account other columns, and if there is not null, it will occupy one bit, right Different character sets have different effective lengths, such as utf8, up to 21845, and other columns must be removed, but varchar is sufficient for storage in general. If you encounter large text, consider using text, which can reach up to 4G.

The efficiency is basically char>varchar>text, but if you are using the Innodb engine, it is recommended to use varchar instead of char

Char and varchar can have default values, and text cannot specify default values

It is still necessary for the database to choose the appropriate data type for storage, which has a certain impact on performance. Here are two pieces of system records. For int type, if you do not need to access negative values, it is best to add unsigned; for fields that often appear in where statements, consider adding indexes, and shaping is especially suitable for adding indexes.

Guess you like

Origin blog.csdn.net/JineD/article/details/111875719