数据库
首页 > 数据库> > 在MySQL中,当只需要TEXT时,LONGTEXT是否会浪费空间?

在MySQL中,当只需要TEXT时,LONGTEXT是否会浪费空间?

作者:互联网

我有一个列,其数据类型为LONGTEXT.而TEXT数据类型足以满足95%的值.我需要LONGTEXT仅占5%的价值.

现在我想知道,在LONGTEXT中存储一个小值会浪费很多空间吗?那是优化吗?

解决方法:

让我们看看MySQL文档.

TEXT[(M)] [CHARACTER SET charset_name] [COLLATE collation_name]

A TEXT column with a maximum length of 65,535 (2^16 − 1) characters.
The effective maximum length is less if the value contains multibyte
characters. Each TEXT value is stored using a 2-byte length prefix
that indicates the number of bytes in the value.

LONGTEXT [CHARACTER SET charset_name] [COLLATE collation_name]

A TEXT column with a maximum length of 4,294,967,295 or 4GB (2^32 − 1)
characters. The effective maximum length is less if the value contains
multibyte characters. The effective maximum length of LONGTEXT columns
also depends on the configured maximum packet size in the
client/server protocol and available memory. Each LONGTEXT value is
stored using a 4-byte length prefix that indicates the number of bytes
in the value.

MEDIUMTEXT [CHARACTER SET charset_name] [COLLATE collation_name]

A TEXT column with a maximum length of 16,777,215 (2^24 − 1)
characters. The effective maximum length is less if the value contains
multibyte characters. Each MEDIUMTEXT value is stored using a 3-byte
length prefix that indicates the number of bytes in the value.

所以前缀的区别是2个字节.

标签:optimization,mysql,sqldatatypes
来源: https://codeday.me/bug/20190628/1313349.html