MySQL's varchar vs. text

Source: Internet
Author: User

varchar and text is the MySQL character storage dispute more than the field, the big print paragraph with that better, let us compare, and then choose by ourselves.

Size comparison
Varchar:varchar satisfies the maximum row limit in MySQL, which is 65535 (16k) bytes, using uft-8 in MySQL (utf-8 in MySQL and our normal encoding utf-8 different) character set takes up three bytes,
① use utf-8 character encoding set varchar maximum length is (65535-2)/3=21844 characters (more than 255 characters will have 2 bytes of extra space overhead, so minus 2, if 255 or less, minus 1).
② uses the UTF-8MB4 character set (the UTF-8MB4 character set in MySQL is the utf-8 character set that we typically use), and MySQL uses the UTF-8MB4 character set to use a character of 4 bytes, so the maximum varchar length is (65535-2)/4= 16,383 characters (more than 255 characters will have 2 bytes of extra space overhead, so minus 2, minus 255 if it is below 1).
TEXT: Maximum limit is 64k,
Use the Utf-8 character set, (262144-2)/3=87381 characters.
Use the UTF-8MB4 character set, (262144-2)/4=65535 characters.
Mediumtext: Maximum length limit of 16M (16m-3)/3 = 5,592,405 characters
Longtext: Maximum length limit 4G
----------------------------------------
Additional Space Overhead Description:
varchar less than 255byte 1byte overhead
varchar greater than 255byte 2byte overhead
Tinytext 0-255 1 byte overhead
Text 0-65535 byte 2 byte overhead
Mediumtext 0-16m 3 byte overhead
Longtext 0-4GB 4byte Overhead
From the processing pattern to say that varchar is greater than 768 bytes, the actual storage and the text difference is not too large. Basically think the same. Also from the 8000byte this point to explain: for Varcahr, text if the line does not exceed 8000byte (about the number, InnoDB data page half), overflow will not be saved to another page.
----------------------------------------
Difference points:
The text field, MySQL does not allow a default value. Indexing must give the prefix index length.
varchar allows default values, no limit on index length,
However, the default length of the InnoDB Engine Single field index is 767 bytes and MyISAM is 1000 bytes. For example, if the character encoding is UTF8, the maximum length of a varchar index is 256 characters. Exceeding the limit causes the index creation to be unsuccessful. Instead, you need to create a prefix index. Setting innodb_large_prefix=1 can increase the limit, allowing the index to use dynamic compression, but the row_format of the table must be compressed or dynamic. You can make the index column longer than 767bytes. However, the total length cannot be greater than 3072 bytes.
----------------------------------------
Summarize:
Depending on the implementation of the storage: you can consider using varchar instead of Tinytext
If you need a non-empty default value, you must use the varchar
If the stored data is greater than 64K, it must be used to Mediumtext, Longtext
varchar (255+) and text are the same in the storage mechanism
It is important to note that varchar (255) is not just 255byte, it may actually occupy more.


Thank Wu Bingxi Teacher for guidance

Reference article address: http://wubx.net/varchar-vs-text/


MySQL's varchar vs. text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.