char、varchar和text设计

MySQL中char、varchar和text的设计

char、varchar、text常识:html

一、char(n)和varchar(n)中括号中n表明字符的个数,并不表明字节个数,因此当使用了中文的时候(UTF8)意味着能够插入m个中文,可是实际会占用m*3个字节。数据库

二、同时char和varchar最大的区别就在于char无论实际value都会占用n个字符的空间,而varchar只会占用实际字符应该占用的空间+1,而且实际空间+1<=n。app

三、超过char和varchar的n设置后,字符串会被截断。post

四、char的上限为255字节,varchar的上限65535字节,text的上限为65535。性能

五、char在存储的时候会截断尾部的空格,varchar和text不会。测试

六、varchar会使用1-3个字节来存储长度,text不会。大数据

下图能够很是明显的看到结果:ui

Valueurl

CHAR(4)spa

Storage Required

VARCHAR(4)

Storage Required

''

'    '

4 bytes

''

1 byte

'ab'

'ab  '

4 bytes

'ab'

3 bytes

'abcd'

'abcd'

4 bytes

'abcd'

5 bytes

'abcdefgh'

'abcd'

4 bytes

'abcd'

5 bytes

 

 

 

 

 

 

 

 

整体来讲:

一、char,存定长,速度快,存在空间浪费的可能,会处理尾部空格,上限255。

二、varchar,存变长,速度慢,不存在空间浪费,不处理尾部空格,上限65535,可是有存储长度实际65532最大可用。

三、text,存变长大数据,速度慢,不存在空间浪费,不处理尾部空格,上限65535,会用额外空间存放数据长度,顾能够所有使用65535。

 

接下来,咱们说说这个场景的问题:

当varchar(n)后面的n很是大的时候咱们是使用varchar好,仍是text好呢?这是个明显的量变引起质变的问题。咱们从2个方面考虑,第一是空间,第二是性能。

首先从空间方面:

从官方文档中咱们能够得知当varchar大于某些数值的时候,其会自动转换为text,大概规则以下:

    • 大于varchar(255)变为 tinytext
    • 大于varchar(500)变为 text
    • 大于varchar(20000)变为 mediumtext

因此对于过大的内容使用varchar和text没有太多区别。

其次从性能方面:

索引会是影响性能的最关键因素,而对于text来讲,只能添加前缀索引,而且前缀索引最大只能达到1000字节。

而貌似varhcar能够添加所有索引,可是通过测试,其实也不是。因为会进行内部的转换,因此long varchar其实也只能添加1000字节的索引,若是超长了会自动截断。

复制代码
localhost.test>create table test (a varchar(1500));
Query OK, 0 rows affected (0.01 sec)

localhost.test>alter table test add index idx_a(a);
Query OK, 0 rows affected, 2 warnings (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 2

localhost.test>show warnings;
+---------+------+---------------------------------------------------------+
| Level   | Code | Message                                                 |
+---------+------+---------------------------------------------------------+
| Warning | 1071 | Specified key was too long; max key length is 767 bytes |
| Warning | 1071 | Specified key was too long; max key length is 767 bytes |
+---------+------+---------------------------------------------------------+
复制代码

从上面能够明显单看到索引被截断了。而这个767是怎么回事呢?这是因为innodb自身的问题,使用innodb_large_prefix设置。

从索引上看其实long varchar和text也没有太多区别。

 

因此咱们认为当超过255的长度以后,使用varchar和text没有本质区别,只须要考虑一下两个类型的特性便可。(主要考虑text没有默认值的问题)

复制代码
CREATE TABLE `test` (
  `id` int(11) DEFAULT NULL,
  `a` varchar(500) DEFAULT NULL,
  `b` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8

+----------+------------+-----------------------------------+
| Query_ID | Duration   | Query                             |
+----------+------------+-----------------------------------+
|        1 | 0.01513200 | select a from test where id=10000 |
|        2 | 0.01384500 | select b from test where id=10000 |
|        3 | 0.01124300 | select a from test where id=15000 |
|        4 | 0.01971600 | select b from test where id=15000 |
+----------+------------+-----------------------------------+
复制代码

从上面的简单测试看,基本上是没有什么区别的,可是我的推荐使用varchar(10000),毕竟这个还有截断,能够保证字段的最大值可控,若是使用text那么若是code有漏洞颇有可能就写入数据库一个很大的内容,会形成风险。

故,本着short is better原则,仍是使用varchar根据需求来限制最大上限最好。

 

附录:各个字段类型的存储需求

Data Type

Storage Required

TINYINT

1 byte

SMALLINT

2 bytes

MEDIUMINT

3 bytes

INTINTEGER

4 bytes

BIGINT

8 bytes

FLOAT(p)

4 bytes if 0 <= p <= 24, 8 bytes if 25 <= p <= 53

FLOAT

4 bytes

DOUBLE [PRECISION]REAL

8 bytes

DECIMAL(M,D), NUMERIC(M,D)

Varies; see following discussion

BIT(M)

approximately (M+7)/8 bytes

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Data Type

Storage Required Before MySQL 5.6.4

Storage Required as of MySQL 5.6.4

YEAR

1 byte

1 byte

DATE

3 bytes

3 bytes

TIME

3 bytes

3 bytes + fractional seconds storage

DATETIME

8 bytes

5 bytes + fractional seconds storage

TIMESTAMP

4 bytes

4 bytes + fractional seconds storage

 

 

 

 

 

 

 

 

 

 

 

Data Type

Storage Required

CHAR(M)

M × w bytes, 0 <= M <= 255, where w is the number of bytes required for the maximum-length character in the character set

BINARY(M)

M bytes, 0 <= M <= 255

VARCHAR(M), VARBINARY(M)

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

TINYBLOBTINYTEXT

L + 1 bytes, where L < 28

BLOBTEXT

L + 2 bytes, where L < 216

MEDIUMBLOBMEDIUMTEXT

L + 3 bytes, where L < 224

LONGBLOBLONGTEXT

L + 4 bytes, where L < 232

ENUM('value1','value2',...)

1 or 2 bytes, depending on the number of enumeration values (65,535 values maximum)

SET('value1','value2',...)

1, 2, 3, 4, or 8 bytes, depending on the number of set members (64 members maximum)

相关文章
相关标签/搜索