MySQL long text field selection

A certain field needs to store long text type data, with variable length and unclear range.

varcharWhat is the maximum length that can be stored? Under what circumstances is textit better to use?

The following content explores this issue:


alt

<1>. First set the content field to varchar(255), then this field can only store up to 255 characters.


package main

import "fmt"

func main() {

 var str string

 for i := 1; i <= 255; i++ {
  str += "a"
 }

 fmt.Println(str)

 fmt.Println(len(str))

}
输出为:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

255

Write the generated 255-bit str into the content field and save it successfully. It has nothing to do with whether the text is a number, English or Chinese, full-width or half-width. (Since MySQL 5.0)


But if longer data is written, it will start from the 255th bit, and all subsequent data will be discarded.

That is baaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(still 255 bits, discarding the last a) can be written,

and

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac(256 bit), cannot be written to


<2>. varcharThe upper limit of the type is that it can store up to 65535 bytes of data (not characters; that is, 64KB), so its upper limit is related to the character set. If so latin1, it can represent the number of characters of 65532 lengths (variable length fields are additional Use 1 or 2 bytes to record the actual data length and whether it is a NULL flag. If the data table has only one varchar field and the field is DEFAULT NULL, then the maximum length of the varchar field is 65532 bytes, that is, 65535-2 -1=65532 bytes)

When the character set utf-8is , then the maximum number of characters can be stored.

Try to automatically change the content to varchar(21844), and the error is as follows:

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。
alt

This is because it is not only subject to storage restrictions and encoding length restrictions , but also to the MySQL single line length limit . The length of a MySQL line record must not exceed 65535. If the length of each field in the defined table exceeds this value, the above error will be prompted.

因为这张表还有一个int(11)类型的id,故而达不到21844这个长度.


将id字段删掉,只保留content这一个字段,在utf-8下,可成功设置为 varchar(21844),

如改为varchar(21845),则会继续报如上错误.

alt

<3>.如果继续以较大幅度提供后面的值,如 varchar(70000),则不会报错而将自动转为mediumtext类型.

alt

如将该字段字符集(及其相应的排序规则)改为latin1,则可成功设置为varchar(65532)

alt

如再提高至varchar(65533),则会报上面行大小太大的错误.

latin1字符集下,存储英文大小写,数字,都没有问题,但用来存储汉字,则会因为不能识别而被记录为?

alt

<4.>在在utf-8下, 且只有这一个字段,可成功设置为varchar(21844)情况下,经实际亲测,可以容纳21844个字符,超出部分将会被舍弃.

alt
alt



即一般情况下,如果长度小于2万,可用varchar,否则就要用text


alt

参考 & 强烈推荐阅读

MySQL中varchar最大长度是多少?[1]

MySQL性能优化之char、varchar、text的区别[2]

参考资料

[1]

MySQL中varchar最大长度是多少?: https://www.cnblogs.com/gomysql/p/3615897.html

[2]

The difference between char, varchar and text in MySQL performance optimization: https://blog.csdn.net/brycegao321/article/details/78038272

This article is published by mdnice multi-platform

Guess you like

Origin blog.csdn.net/techdashen/article/details/132838173