The BLOB and TEXT Types

官網參考:https://dev.mysql.com/doc/refman/5.7/en/blob.htmlhtml

字符串類型對應的存儲需求

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. 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、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進行設置,客戶端和服務器端都須要設置
相關文章
相關標籤/搜索