zoukankan      html  css  js  c++  java
  • The BLOB and TEXT Types

    官网参考:https://dev.mysql.com/doc/refman/5.7/en/blob.html

    字符串类型对应的存储需求

    Data TypeStorage 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. See Section 14.8.1.2, “The Physical Row Structure of an InnoDB Table” for information about CHAR data type storage requirements for InnoDB tables.
    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
    TINYBLOB, TINYTEXT L + 1 bytes, where L < 28
    BLOB, TEXT L + 2 bytes, where L < 216
    MEDIUMBLOB, MEDIUMTEXT L + 3 bytes, where L < 224
    LONGBLOB, LONGTEXT 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)

    关于row size 参考官方文档

    • varchar允许的最大字符长度是65,535,超过这个限制就需要考虑text和blob
    • varchar(N),N 代表允许的中文字的个数(字符集为utf8的一个中文字占用三个字符)也表示英文字符的个数,
    mysql> desc varch
        -> ;
    +-------+------------+------+-----+---------+-------+
    | Field | Type       | Null | Key | Default | Extra |
    +-------+------------+------+-----+---------+-------+
    | name  | varchar(3) | YES  |     | NULL    |       |
    +-------+------------+------+-----+---------+-------+
    1 row in set (0.00 sec)
    
    mysql> insert into varch values('hell');
    ERROR 1406 (22001): Data too long for column 'name' at row 1
    mysql> insert into varch values('hel');
    Query OK, 1 row affected (0.00 sec)
    
    mysql> select * from varch;
    +-----------+
    | name      |
    +-----------+
    | 新中国    |
    | hel       |
    +-----------+
    2 rows in set (0.00 sec)
    • N>255就会占用2个额外的字符存储长度,所以N最大为(65535-2)/3
    mysql> create table varch(name varchar(21845));
    ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
    mysql> create table varch(name varchar(21844));
    Query OK, 0 rows affected (0.11 sec)
    • 对于innodb,row size 的限制是innodb_page_size的设置的一半(如果设置不超过32KB,默认是16KB),对于text 和blob 没有限制,只会占用row size 的9-12字节(byte)
    mysql>  create table varch(col1 varchar(10920),col2 varchar(10920),col3 text);          
    Query OK, 0 rows affected (0.12 sec)
    # 10920 计算公式
    mysql> select (65535-(2+2+9))/3/2; 
    +---------------------+
    | (65535-(2+2+9))/3/2 |
    +---------------------+
    |      10920.33333333 |
    +---------------------+
    # 括号里的2、2、9是字段的长度存储所需大小
    • text相当于longvarchar,BLOB值被视为二进制字符串(字节串)。 它们具有二进制字符集和排序规则,比较和排序基于列值中字节的数值。 TEXT值被视为非二进制字符串(字符串)。 它们具有除二进制之外的字符集,并且基于字符集的排序来对值进行排序和比较。blob相当于binaryvarchar

    注意点

    • 创建索引时必须制定前缀
    • 非strict sql mode模式下,如果长度超过限制,则会自动进行截断
    • 不支持default值

    text 和 blob 类型的字段可能会非常的长,所以就可能遇到以下限制

    • 进行分组或排序时只会对max_sort_length 指定的长度进行,次变量默认值为1024,可以进行session级别的调整
    • 由于memory 存储引擎不支持这两种类型,所以包含此字段的查询如果用到临时表,则表会在磁盘上创建,从而会造成性能的消耗,所以避免使用 SELECT *
    • server和client传输此类型的数据时可能会超过通信包的长度限制,通过max_allowed_packet进行设置,客户端和服务器端都需要设置
  • 相关阅读:
    Linux服务器使用SSH的命令
    linux c 查看其它程序是否启动。没有则启动他
    libnfc安装在ubuntu
    Linux让应用只在特定桌面环境下自动启动
    Linux服务器守护进程+自动启动+服务配置笔记
    ps 指令詳解
    http://blog.sina.com.cn/s/blog_57421ff80100c7nn.html
    Can't start MySQL5.5 on Ubuntu 12.04 “dpkg: dependency problems”
    chsh命令用于修改你的登录shell
    linux ssh客户端密钥转发
  • 原文地址:https://www.cnblogs.com/Bccd/p/7244666.html
Copyright © 2011-2022 走看看