MySQL表字段數(shù)量限制及行大小限制詳情
字段數(shù)量限制 Column Count Limits
MySQL對(duì)于每個(gè)表的字段數(shù)量是嚴(yán)格控制在4096個(gè),但是實(shí)際情況下的限制大小取決于以下因素(也就是基本達(dá)不到4096就被限制了):
- 表的行最大的
row size
會(huì)限制字段數(shù)量,如果當(dāng)前row size
過大就不能加字段了 - The storage requirements of individual columns constrain the number of columns that fit within a given maximum row size.
- 存儲(chǔ)引擎本身也會(huì)對(duì)字段數(shù)量有限制,比如
Innodb
最大字段數(shù)量為1017 - 每張表都有一個(gè)后綴為
.frm
的表定義文件,表定義可能會(huì)影響字段數(shù)量大小
行大小限制 Row Size Limits
row size由以下因素影響:
- MySQL本身對(duì)
row size
有硬性規(guī)定,不能超過65535bytes
,即使存儲(chǔ)引擎允許更大的size. 而BLOB
和TEXT
類型列只占行的9到12個(gè)字節(jié),具體存儲(chǔ)地方不在該行里;但是其他的比如varchar
還是算在行里的,這里要注意 Innodb
存儲(chǔ)引擎對(duì)于每行的大小一般限制為頁(yè)大小的一半:頁(yè)16KB,row size 8KB。另外對(duì)于不定長(zhǎng)類型也有不同: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.簡(jiǎn)單來說就是,對(duì)于像varchar
這種不定長(zhǎng)類型,如果這種類型長(zhǎng)度超過了Innodb
存儲(chǔ)引擎規(guī)定的row size
,那么Innodb會(huì)選擇頁(yè)外存儲(chǔ)直到行大小符合Innodb
存儲(chǔ)引擎規(guī)定的row size
。(但是即使這樣也不能超過65535B,即65535B是包含不定長(zhǎng)列中的內(nèi)容的長(zhǎng)度)- 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個(gè)字符是1個(gè)字節(jié),總共不能超過65535個(gè)字符,超過就報(bào)錯(cuò);而常用的utf8mb4
編碼最多1個(gè)字符占用4個(gè)字節(jié),所以當(dāng)使用utf8mb4
編碼時(shí),最多只能有65535/4=16383
個(gè)字符(實(shí)際測(cè)肯定會(huì)小點(diǎn),因?yàn)檫€有字節(jié)去記錄變長(zhǎng)字段長(zhǎng)度):
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
類型,這個(gè)在row size
中只占用9到12個(gè)字節(jié)。
8126B限制
65535B的限制主要針對(duì)不定長(zhǎng)類型的限制,而定長(zhǎng)類型的限制更為嚴(yán)格,像在Innodb存儲(chǔ)引擎中,只能達(dá)到8KB多也就是頁(yè)大小的一半(可以修改)。
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限制和存儲(chǔ)引擎8126B限制(默認(rèn)Innodb)。
- 針對(duì)不定長(zhǎng)類型,如
varchar
,一般首先收到限制的是MySQL本身65535B的限制。受不到存儲(chǔ)引擎限制是因?yàn)?,不定長(zhǎng)類型如果長(zhǎng)度超過8126B,會(huì)采用頁(yè)外存儲(chǔ),也就是不定長(zhǎng)類型的長(zhǎng)度過長(zhǎng)的話計(jì)入65535B而不計(jì)入8126B(大致可以這么理解)。 - 針對(duì)定長(zhǎng)類型,首先會(huì)受到存儲(chǔ)引擎8126B限制。
到此這篇關(guān)于MySQL表字段數(shù)量限制及行大小限制詳情的文章就介紹到這了,更多相關(guān)MySQL行大小限制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
mysql Access denied for user ‘root’@’localhost’ (using passw
這篇文章主要介紹了mysql Access denied for user ‘root’@’localhost’ (using password: YES)解決方法,本文給出詳細(xì)的解決步驟及操作注釋,需要的朋友可以參考下2015-07-07Mysql中的count()與sum()區(qū)別詳細(xì)介紹
本文將介紹Mysql中的count()與sum()區(qū)別,需要的朋友可以參考下2012-11-11mysql之查詢兩個(gè)時(shí)間段是否有交集的情況
這篇文章主要介紹了mysql之查詢兩個(gè)時(shí)間段是否有交集的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Mysql入門基礎(chǔ) 數(shù)據(jù)庫(kù)創(chuàng)建篇
Mysql入門基礎(chǔ) 數(shù)據(jù)庫(kù)創(chuàng)建篇,剛接觸php與mysql的朋友可以參考下。多寫多測(cè)試。2010-04-04MySQL 5.5.49 大內(nèi)存優(yōu)化配置文件優(yōu)化詳解
最近mysql服務(wù)器升級(jí)到了MySQL 5.5.49版本,性能比mysql 5.0.**肯定效率高了不少,但mysql的默認(rèn)配置文件不合理,這里是針對(duì)大內(nèi)存訪問量大的機(jī)器的配置方案,需要的朋友可以參考下2016-05-05如何將Excel文件導(dǎo)入MySQL數(shù)據(jù)庫(kù)
這篇文章主要為大家詳細(xì)介紹了Excel文件導(dǎo)入MySQL數(shù)據(jù)庫(kù)的具體方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07