Table Column-Count and Row-Size Limits
There is a hard limit of 4096 columns per table, but the effective maximum may be less for a given table. The exact limit depends on several interacting factors.
- Every table (regardless of storage engine) has a maximum row size of 65,535 bytes. Storage engines may place additional constraints on this limit, reducing the effective maximum row size.
The maximum row size constrains the number (and possibly size) of columns because the total length of all columns cannot exceed this size. For example,
utf8characters require up to three bytes per character, so for aCHAR(255) CHARACTER SET utf8column, the server must allocate 255 × 3 = 765 bytes per value. Consequently, a table cannot contain more than 65,535 / 765 = 85 such columns.Storage for variable-length columns includes length bytes, which are assessed against the row size. For example, a
VARCHAR(255) CHARACTER SET utf8column takes two bytes to store the length of the value, so each value can take up to 767 bytes.BLOBandTEXTcolumns count from one to four plus eight bytes each toward the row-size limit because their contents are stored separately from the rest of the row.Declaring columns
NULLcan reduce the maximum number of columns permitted. ForMyISAMtables,NULLcolumns require additional space in the row to record whether their values areNULL. EachNULLcolumn takes one bit extra, rounded up to the nearest byte. The maximum row length in bytes can be calculated as follows:row length = 1 + (
sum of column lengths) + (number of NULL columns+delete_flag+ 7)/8 + (number of variable-length columns)delete_flagis 1 for tables with static row format. Static tables use a bit in the row record for a flag that indicates whether the row has been deleted.delete_flagis 0 for dynamic tables because the flag is stored in the dynamic row header. For information aboutMyISAMtable formats, see , "MyISAMTable Storage Formats".These calculations do not apply for
InnoDBtables. Storage size is the same forNULLandNOT NULLcolumns.The following statement to create table
t1succeeds because the columns require 32,765 + 2 bytes and 32,766 + 2 bytes, which falls within the maximum row size of 65,535 bytes:mysql>
CREATE TABLE t1->(c1 VARCHAR(32765) NOT NULL, c2 VARCHAR(32766) NOT NULL)->ENGINE = MyISAM CHARACTER SET latin1;Query OK, 0 rows affected (0.02 sec)The following statement to create table
t2fails because the columns areNULLandMyISAMrequires additional space that causes the row size to exceed 65,535 bytes:mysql>
CREATE TABLE t2->(c1 VARCHAR(32765) NULL, c2 VARCHAR(32766) NULL)->ENGINE = MyISAM CHARACTER SET latin1;ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBsThe following statement to create table
t3fails because although the column length is within the maximum length of 65,535 bytes, two additional bytes are required to record the length, which causes the row size to exceed 65,535 bytes:mysql>
CREATE TABLE t3->(c1 VARCHAR(65535) NOT NULL)->ENGINE = MyISAM CHARACTER SET latin1;ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. You have to change some columns to TEXT or BLOBsReducing the column length to 65,533 or less permits the statement to succeed.
- Each table has an
.frmfile that contains the table definition. The server uses the following expression to check some of the table information stored in the file against an upper limit of 64KB:if (info_length+(ulong) create_fields.elements*FCOMP+288+ n_length+int_length+com_length > 65535L || int_count > 255)
The portion of the information stored in the
.frmfile that is checked against the expression cannot grow beyond the 64KB limit, so if the table definition reaches this size, no more columns can be added.The relevant factors in the expression are:
info_lengthis space needed for "screens." This is related to MySQL's Unireg heritage.create_fields.elementsis the number of columns.FCOMPis 17.n_lengthis the total length of all column names, including one byte per name as a separator.int_lengthis related to the list of values forENUMandSETcolumns.com_lengthis the total length of column and table comments.
Thus, using long column names can reduce the maximum number of columns, as can the inclusion of
ENUMorSETcolumns, or use of column, index, or table comments. - Individual storage engines might impose additional restrictions that limit table column count. Examples:
InnoDBpermits up to 1000 columns.InnoDBrestricts row size to something less than half a database page (approximately 8000 bytes), not includingVARBINARY,VARCHAR,BLOB, orTEXTcolumns.- Different
InnoDBstorage formats (COMPRESSED,REDUNDANT) use different amounts of page header and trailer data, which affects the amount of storage available for rows.
Retornar