欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL表字段數(shù)量限制及行大小限制詳情

 更新時間:2022年07月22日 14:06:05   作者:T.Y.Bao  
這篇文章主要介紹了MySQL表字段數(shù)量限制及行大小限制詳情,表的行最大的row size會限制字段數(shù)量,如果當前row size過大就不能加字段了,更多相關(guān)需要的小伙伴可以參考下面文章詳情

字段數(shù)量限制 Column Count Limits

MySQL對于每個表的字段數(shù)量是嚴格控制在4096個,但是實際情況下的限制大小取決于以下因素(也就是基本達不到4096就被限制了):

  • 表的行最大的row size會限制字段數(shù)量,如果當前row size過大就不能加字段了
  • The storage requirements of individual columns constrain the number of columns that fit within a given maximum row size.
  • 存儲引擎本身也會對字段數(shù)量有限制,比如Innodb最大字段數(shù)量為1017
  • 每張表都有一個后綴為.frm的表定義文件,表定義可能會影響字段數(shù)量大小

行大小限制 Row Size Limits

row size由以下因素影響:

  • MySQL本身對row size有硬性規(guī)定,不能超過65535bytes,即使存儲引擎允許更大的size. 而BLOBTEXT類型列只占行的9到12個字節(jié),具體存儲地方不在該行里;但是其他的比如varchar還是算在行里的,這里要注意
  • Innodb存儲引擎對于每行的大小一般限制為頁大小的一半:頁16KB,row size 8KB。另外對于不定長類型也有不同:If a row containing variable-length columns exceeds the InnoDB maximum row size, InnoDB selects variable-length columns for external off-page storage until the row fits within the InnoDB row size limit. The amount of data stored locally for variable-length columns that are stored off-page differs by row format. For more information, see InnoDB Row Formats.簡單來說就是,對于像varchar這種不定長類型,如果這種類型長度超過了Innodb存儲引擎規(guī)定的row size,那么Innodb會選擇頁外存儲直到行大小符合Innodb存儲引擎規(guī)定的row size。(但是即使這樣也不能超過65535B,即65535B是包含不定長列中的內(nèi)容的長度)
  • Different storage formats use different amounts of page header and trailer data, which affects the amount of storage available for rows. Row Size Limit Examples 65535B限制
mysql> CREATE TABLE t (a VARCHAR(10000), b VARCHAR(10000),
       c VARCHAR(10000), d VARCHAR(10000), e VARCHAR(10000),
       f VARCHAR(10000), g VARCHAR(6000)) ENGINE=InnoDB CHARACTER SET latin1;
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

看,由于Latin1編碼1個字符是1個字節(jié),總共不能超過65535個字符,超過就報錯;而常用的utf8mb4編碼最多1個字符占用4個字節(jié),所以當使用utf8mb4編碼時,最多只能有65535/4=16383個字符(實際測肯定會小點,因為還有字節(jié)去記錄變長字段長度):

test> CREATE TABLE t (a VARCHAR(10000), b VARCHAR(6384)
             ) ENGINE=InnoDB CHARACTER SET utf8mb4
[2022-07-21 15:05:56] [42000][1118] 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

test> CREATE TABLE t (a VARCHAR(10000), b VARCHAR(6383)
             ) ENGINE=InnoDB CHARACTER SET utf8mb4
[2022-07-21 15:09:51] [42000][1118] 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
test> CREATE TABLE t (a VARCHAR(10000), b VARCHAR(6382)
             ) ENGINE=InnoDB CHARACTER SET utf8mb4
[2022-07-21 15:09:58] completed in 33 ms

根據(jù)提示,如果超過row size限制,可以使用TEXT or BLOBs類型,這個在row size中只占用9到12個字節(jié)。

8126B限制

65535B的限制主要針對不定長類型的限制,而定長類型的限制更為嚴格,像在Innodb存儲引擎中,只能達到8KB多也就是頁大小的一半(可以修改)。

innodb_strict_mode is enabled in the following example to ensure that InnoDB returns an error if the defined columns exceed the InnoDB row size limit. When innodb_strict_mode is disabled (the default), creating a table that uses REDUNDANT or COMPACT row format succeeds with a warning if the InnoDB row size limit is exceeded.

mysql> SET SESSION innodb_strict_mode=1;
mysql> CREATE TABLE t4 (
       c1 CHAR(255),c2 CHAR(255),c3 CHAR(255),
       c4 CHAR(255),c5 CHAR(255),c6 CHAR(255),
       c7 CHAR(255),c8 CHAR(255),c9 CHAR(255),
       c10 CHAR(255),c11 CHAR(255),c12 CHAR(255),
       c13 CHAR(255),c14 CHAR(255),c15 CHAR(255),
       c16 CHAR(255),c17 CHAR(255),c18 CHAR(255),
       c19 CHAR(255),c20 CHAR(255),c21 CHAR(255),
       c22 CHAR(255),c23 CHAR(255),c24 CHAR(255),
       c25 CHAR(255),c26 CHAR(255),c27 CHAR(255),
       c28 CHAR(255),c29 CHAR(255),c30 CHAR(255),
       c31 CHAR(255),c32 CHAR(255),c33 CHAR(255)
       ) ENGINE=InnoDB ROW_FORMAT=COMPACT DEFAULT CHARSET latin1;
ERROR 1118 (42000): Row size too large (> 8126). Changing some columns to TEXT or BLOB or using
ROW_FORMAT=DYNAMIC or ROW_FORMAT=COMPRESSED may help. In current row format, BLOB prefix of 768
bytes is stored inline.

Row Limits總結(jié)

Row Limits 一般為 MySQL本身65535B限制和存儲引擎8126B限制(默認Innodb)。

  • 針對不定長類型,如varchar,一般首先收到限制的是MySQL本身65535B的限制。受不到存儲引擎限制是因為,不定長類型如果長度超過8126B,會采用頁外存儲,也就是不定長類型的長度過長的話計入65535B而不計入8126B(大致可以這么理解)。
  • 針對定長類型,首先會受到存儲引擎8126B限制。

到此這篇關(guān)于MySQL表字段數(shù)量限制及行大小限制詳情的文章就介紹到這了,更多相關(guān)MySQL行大小限制內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論