php VARCHAR 大小限制重要吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4324872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:40:44  来源:igfitidea点击:

Does VARCHAR size limit matter?

phpmysqldatabase-designvarchar

提问by Jane Panda

Possible Duplicate:
Importance of varchar length in MySQL table

可能的重复:
MySQL 表中 varchar 长度的重要性

When using VARCHAR (assuming this is the correct data type for a short string) does the size matter? If I set it to 20 characters, will that take up less space or be faster than 255 characters?

使用 VARCHAR 时(假设这是短字符串的正确数据类型)大小重要吗?如果我将它设置为 20 个字符,那会占用更少的空间还是比 255 个字符快?

采纳答案by symcbean

In general, for a VARCHAR field, the amount of data stored in each field determines its footprint on the disk rather than the maximum size (unlike a CHAR field which always has the same footprint).

通常,对于 VARCHAR 字段,存储在每个字段中的数据量决定了其在磁盘上的占用空间而不是最大大小(与始终具有相同占用空间的 CHAR 字段不同)。

There is an upper limit on the total data stored within all fields of an index of 900 bytes (900 byte index size limit in character length).

索引的所有字段中存储的总数据有一个上限为 900 字节(字符长度为 900 字节索引大小限制)。

The larger you make the field, the more likely people will try to use for purposes other than what you intended - and the greater the screen real-estate required to show the value - so its good practice to try to pick the rightsize, rather than assuming that if you make it as large as possible it will save you having to revisit the design.

您创建的字段越大,人们就越有可能尝试将其用于您预期之外的目的 - 并且显示价值所需的屏幕空间越大 - 因此尝试选择合适的尺寸是一种很好的做法,而不是而不是假设如果你把它做得尽可能大,那么你就不必重新审视设计了。

回答by ajreal

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. Be sure to take this into account when specifying a prefix length for a column that uses a multi-byte character set.

前缀最长可达 1000 字节(InnoDB 表为 767 字节)。请注意,前缀限制以字节为单位,而 CREATE TABLE 语句中的前缀长度被解释为字符数。在为使用多字节字符集的列指定前缀长度时,一定要考虑到这一点。

source : http://dev.mysql.com/doc/refman/5.0/en/column-indexes.html

来源:http: //dev.mysql.com/doc/refman/5.0/en/column-indexes.html

In a latin1 collation, you can only specify up 3 columns of varchar(255).
While can specify up to 50 columns for varchar(20)

在 latin1 归类中,您最多只能指定 3 列varchar(255).
While 最多可以指定 50 列varchar(20)

In-directly, without proper index, it will slow-downquery speed

间接,没有合适的索引,会降低查询速度

In terms of storage, it does not make difference,
as varcharstand for variable-length strings

在存储方面,它没有区别,
作为varchar代表variable-length strings

回答by Pentium10

The actual differences are:

实际的区别是:

  • TINYTEXT and other TEXT fields are stored separately from in-memory row inside MySQL heap, whereas VARCHAR() fields add up to 64k limit (so you can have more than 64k in TINYTEXTs, whereas you won't with VARCHAR).

  • TINYTEXT and other 'blob-like' fields will force SQL layer (MySQL) to use on-disk temporary tables whenever they are used, whereas VARCHAR will be still sorted 'in memory' (though will be converted to CHAR for the full width).

  • InnoDB internally doesn't really care whether it is tinytext or varchar. It is very easy to verify, create two tables, one with VARCHAR(255), another with TINYINT, and insert a record to both. They both will take single 16k page - whereas if overflow pages are used, TINYTEXT table should show up as taking at least 32k in 'SHOW TABLE STATUS'.

  • TINYTEXT 和其他 TEXT 字段与 MySQL 堆内的内存行分开存储,而 VARCHAR() 字段加起来最多为 64k 限制(因此您可以在 TINYTEXT 中拥有超过 64k,而使用 VARCHAR 则不会)。

  • TINYTEXT 和其他“类似 blob”的字段将强制 SQL 层 (MySQL) 在使用时使用磁盘上的临时表,而 VARCHAR 仍将在“内存中”排序(尽管将转换为全宽度的 CHAR) .

  • InnoDB 内部并不真正关心它是 tinytext 还是 varchar。很容易验证,创建两个表,一个使用 VARCHAR(255),另一个使用 TINYINT,并向两者插入一条记录。它们都将占用单个 16k 页 - 而如果使用溢出页,则 TINYTEXT 表应显示为在“显示表状态”中占用至少 32k。

I usually prefer VARCHAR(255) - they don't cause too much of heap fragmentation for single row, and can be treated as single 64k object in memory inside MySQL. On InnoDB size differences are negligible.

我通常更喜欢 VARCHAR(255) - 它们不会对单行造成太多的堆碎片,并且可以被视为 MySQL 内存中的单个 64k 对象。在 InnoDB 上,大小差异可以忽略不计。

回答by Eleazan

In the documentation of MySQL: http://dev.mysql.com/doc/refman/5.0/en/char.html

在 MySQL 的文档中:http: //dev.mysql.com/doc/refman/5.0/en/char.html

You have a table that indicates the bytes of a VARCHAR(4) (vs a CHAR(4)).

您有一个表,指示 VARCHAR(4)(与 CHAR(4))的字节数。

A simple VARCHAR(4) without string, only 1 byte. Then, a simple VARCHAR(255) without string is 1byte. A VARCHAR(4) with 'ab' is 3 bytes, and a VARCHAR(255) with 'ab' is 3 bytes. It's the same, but with the lenght limit :)

一个没有字符串的简单 VARCHAR(4),只有 1 个字节。然后,一个没有字符串的简单 VARCHAR(255) 是 1byte。带有 'ab' 的 VARCHAR(4) 是 3 个字节,带有 'ab' 的 VARCHAR(255) 是 3 个字节。是一样的,但有长度限制:)

回答by Spencer Ruport

This will have no effect on performance. In this case the constraint merely helps ensure data integrity.

这不会影响性能。在这种情况下,约束仅有助于确保数据完整性。

回答by Vladimir Ivanov

Thisanswer should help you.

这个答案应该对你有帮助。

回答by Yeroon

If you set it to 20, it will save only the first 20 characters. So yes, it will take up less space than 255 characters :).

如果将其设置为 20,则只会保存前 20 个字符。所以是的,它占用的空间少于 255 个字符:)。

回答by Gumbo

The required storage space for VARCHARis as follows:

用于所需的存储空间VARCHAR是如下:

VARCHAR(L), VARBINARY(L)L+ 1 bytes if column values require 0 – 255 bytes, L+ 2 bytes if values may require more than 255 bytes

VARCHAR(L), VARBINARY(L)L如果列值需要 0 – 255 个字节,则为 + 1 个字节,L如果值可能需要超过 255 个字节,则为 + 2 个字节

So VARCHARdoes only require the space for the string plus one or two additional bytes for the length of the string.

所以VARCHAR只需要字符串的空间加上字符串长度的一两个额外字节。