MySQL表字段數(shù)量限制及行大小限制詳情
字段數(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. 而BLOB
和TEXT
類型列只占行的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)文章
mysql Access denied for user ‘root’@’localhost’ (using passw
這篇文章主要介紹了mysql Access denied for user ‘root’@’localhost’ (using password: YES)解決方法,本文給出詳細的解決步驟及操作注釋,需要的朋友可以參考下2015-07-07Mysql中的count()與sum()區(qū)別詳細介紹
本文將介紹Mysql中的count()與sum()區(qū)別,需要的朋友可以參考下2012-11-11Mysql入門基礎(chǔ) 數(shù)據(jù)庫創(chuàng)建篇
Mysql入門基礎(chǔ) 數(shù)據(jù)庫創(chuàng)建篇,剛接觸php與mysql的朋友可以參考下。多寫多測試。2010-04-04MySQL 5.5.49 大內(nèi)存優(yōu)化配置文件優(yōu)化詳解
最近mysql服務(wù)器升級到了MySQL 5.5.49版本,性能比mysql 5.0.**肯定效率高了不少,但mysql的默認配置文件不合理,這里是針對大內(nèi)存訪問量大的機器的配置方案,需要的朋友可以參考下2016-05-05如何將Excel文件導(dǎo)入MySQL數(shù)據(jù)庫
這篇文章主要為大家詳細介紹了Excel文件導(dǎo)入MySQL數(shù)據(jù)庫的具體方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07