Forum Moderators: phranque
SUM_OVER_ALL_KEYS(max_length_of_key + sizeof(char*) * 2)
+ ALIGN(length_of_row+1, sizeof(char*))
But I'm not sure I understand how to use it. It seems to me that what I need are:
A. get the size of all of the keys I intend to use from the tables (simply use MYSQL to get these)
B. max key length - not sure how to get this
C. sizeof(char*) - not sure what to do with this
D. length of row (max?) - again where to get this
E. Align - not a clue what this is for
First, the formula doesn't give the size requirement for a row, it gives the overhead for a row, so the real formula is
length_of_row
+ SUM_OVER_ALL_KEYS(max_length_of_key) + sizeof(char*) * 2
+ ALIGN(length_of_row+1, sizeof(char*))
All records in a HEAP table is of fixed size, so the length_of_row is the same for all rows.
It appears (I haven't read the source code) that records are organised as a linked list, with a pointer to the row data. The data is aligned on a four (or eight) byte boundary, possibly leaving a gap.
This means each record takes up:
the primary key (max size allocated for all)
pointer to where data is stored (four or eight bytes)
pointer to next record (four or eight bytes)
length_of_row (that's your data - all row are the same size)
alignment overhead for row (0-3 bytes or 0-7 bytes)
Lets say the largest primary key is 27 bytes, and the row length is 169 bytes, and we use a 32bit computer, then each row takes up
27 (primary key key)
+ 4 (row data pointer)
+ 4 (next row pointer)
+ 169 (row data)
+ 3 (alignment to make 169+3=172 a multiplum of 4)
= 207 bytes.
That's my guess, anyway.
René.
A. get the size of all of the keys I intend to use from the tables (simply use MYSQL to get these)
You know the type of the primary key.
If your key is an int, it is four bytes.
If your key is a char(32), then it is 32 bytes.
The size requirement for each datatype is described in the MySQL manual. See [mysql.com...] and click through to the explanation of the different column types.
B. max key length - not sure how to get this
Same as A.
C. sizeof(char*) - not sure what to do with this
Depends on the CPU. It is programmer's talk, meaning the memory requirement for a pointer to some data in memory. It is usually four bytes on a 32bit maching, and 8 bytes on a 64bit machine.
D. length of row (max?) - again where to get this
You know the type of each field from the MySQL manual. Just add them together.
E. Align - not a clue what this is for
Most hardware requires data to be aligned on a 4 or 8 byte boundary, so you cannot just store 17 bytes, you have to use 20 or 24 bytes. The overhead is for this alignment.