3 ms·
>> much more space is wasted on column (field) alignment in records >Hmmm... I don't think there is much alignment going on within the data pages. Depending o
by dexen 7y ago
>> much more space is wasted on column (field) alignment in records
>Hmmm... I don't think there is much alignment going on within the data pages.
Depending on the RDBM engine, the atom fields (INTs, FLOATs, etc.) in storage are aligned to multiplies of their sizes. This also applies to fields that are stored as pointers to external data - TEXT, BLOB, etc. get stored like that; either in all cases, or when content length exceeds a threshold.
Not sure why that's done, but probably to help the CPU handle data read/update at full speed and with atomic operation. Or perhaps just to make the format portable to platforms that expect memory accesses to be aligned.
Example: given a record of (INT8, INT64), common RDBMs will waste 7 bytes - padding the first field (INT8) so that the second field (INT64) is aligned to its natural alignment. Thus the physical order of columns matters. And thus my remark on "exceedingly careful". Example for PostgreSQL: typalign [0]
MySQL and PostgreSQL diverge here: when you re-order columns, MySQL normally adjusts only the map of fields, keeping the physical structure unchanged, while PostgreSQL doesn't[1] support such quick change; you either add the new column(s) at the end of record, or re-write the whole table from scratch. This may seem more cumbersome, but gives explicit control over the record's physical format.
[0] https://www.postgresql.org/docs/current/catalog-pg-type.html https://www.postgresql.org/docs/current/catalog-pg-type.html
[1] last time I checked was 9.x, may have changed since then.