The physical row structure of an InnoDB table
depends on the row format specified when the table is created.
InnoDB uses the COMPACT
format by default, but the REDUNDANT format is
available to retain compatibility with older versions of MySQL. To
check the row format of an InnoDB table, use
SHOW TABLE STATUS. For example:
mysql> SHOW TABLE STATUS IN test1\G
*************************** 1. row ***************************
Name: t1
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 16384
Data_free: 0
Auto_increment: 1
Create_time: 2014-10-31 16:02:01
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
Redundant Row Format Characteristics
Rows in InnoDB tables that use
REDUNDANT row format have the following
characteristics:
Each index record contains a 6-byte header. The header is used to link together consecutive records, and also in row-level locking.
Records in the clustered index contain fields for all user-defined columns. In addition, there is a 6-byte transaction ID field and a 7-byte roll pointer field.
If no primary key was defined for a table, each clustered index record also contains a 6-byte row ID field.
Each secondary index record also contains all the primary key fields defined for the clustered index key that are not in the secondary index.
A record contains a pointer to each field of the record. If the total length of the fields in a record is less than 128 bytes, the pointer is one byte; otherwise, two bytes. The array of these pointers is called the record directory. The area where these pointers point is called the data part of the record.
Internally,
InnoDBstores fixed-length character columns such asCHAR(10)in a fixed-length format.InnoDBdoes not truncate trailing spaces fromVARCHARcolumns.InnoDBencodes fixed-length fields greater than or equal to 768 bytes in length as variable-length fields, which can be stored off-page. For example, aCHAR(255)column can exceed 768 bytes if the maximum byte length of the character set is greater than 3, as it is withutf8mb4.An SQL
NULLvalue reserves one or two bytes in the record directory. Besides that, an SQLNULLvalue reserves zero bytes in the data part of the record if stored in a variable length column. In a fixed-length column, it reserves the fixed length of the column in the data part of the record. Reserving the fixed space forNULLvalues enables an update of the column fromNULLto a non-NULLvalue to be done in place without causing fragmentation of the index page.
COMPACT Row Format Characteristics
The COMPACT row format decreases row storage
space by about 20% compared to the REDUNDANT
format at the cost of increasing CPU use for some operations. If
your workload is a typical one that is limited by cache hit rates
and disk speed, COMPACT format is likely to be
faster. If the workload is a rare case that is limited by CPU
speed, compact format might be slower.
Rows in InnoDB tables that use
COMPACT row format have the following
characteristics:
Each index record contains a 5-byte header that may be preceded by a variable-length header. The header is used to link together consecutive records, and also in row-level locking.
The variable-length part of the record header contains a bit vector for indicating
NULLcolumns. If the number of columns in the index that can beNULLisN, the bit vector occupiesCEILING(bytes. (For example, if there are anywhere from 9 to 15 columns that can beN/8)NULL, the bit vector uses two bytes.) Columns that areNULLdo not occupy space other than the bit in this vector. The variable-length part of the header also contains the lengths of variable-length columns. Each length takes one or two bytes, depending on the maximum length of the column. If all columns in the index areNOT NULLand have a fixed length, the record header has no variable-length part.For each non-
NULLvariable-length field, the record header contains the length of the column in one or two bytes. Two bytes are only needed if part of the column is stored externally in overflow pages or the maximum length exceeds 255 bytes and the actual length exceeds 127 bytes. For an externally stored column, the 2-byte length indicates the length of the internally stored part plus the 20-byte pointer to the externally stored part. The internal part is 768 bytes, so the length is 768+20. The 20-byte pointer stores the true length of the column.The record header is followed by the data contents of the non-
NULLcolumns.Records in the clustered index contain fields for all user-defined columns. In addition, there is a 6-byte transaction ID field and a 7-byte roll pointer field.
If no primary key was defined for a table, each clustered index record also contains a 6-byte row ID field.
Each secondary index record also contains all the primary key fields defined for the clustered index key that are not in the secondary index. If any of these primary key fields are variable length, the record header for each secondary index has a variable-length part to record their lengths, even if the secondary index is defined on fixed-length columns.
Internally, for nonvariable-length character sets,
InnoDBstores fixed-length character columns such asCHAR(10)in a fixed-length format.InnoDBdoes not truncate trailing spaces fromVARCHARcolumns.Internally, for variable-length character sets such as
utf8mb3andutf8mb4,InnoDBattempts to storeCHAR(inN)Nbytes by trimming trailing spaces. If the byte length of aCHAR(column value exceedsN)Nbytes,InnoDBtrims trailing spaces to a minimum of the column value byte length. The maximum length of aCHAR(column is the maximum character byte length ×N)N.InnoDBreserves a minimum ofNbytes forCHAR(. Reserving the minimum spaceN)Nin many cases enables column updates to be done in place without causing fragmentation of the index page. By comparison, forROW_FORMAT=REDUNDANT,CHAR(columns occupy the maximum character byte length ×N)N.InnoDBencodes fixed-length fields greater than or equal to 768 bytes in length as variable-length fields, which can be stored off-page. For example, aCHAR(255)column can exceed 768 bytes if the maximum byte length of the character set is greater than 3, as it is withutf8mb4.ROW_FORMAT=DYNAMICandROW_FORMAT=COMPRESSEDhandleCHARstorage in the same way asROW_FORMAT=COMPACT.
DYNAMIC and COMPRESSED Row Formats
DYNAMIC and COMPRESSED row
formats are variations of the COMPACT row
format. For information about these row formats, see
Section 14.14.3, “DYNAMIC and COMPRESSED Row Formats”.