スポンサーリンク

【PostgreSQL】bitをサイズ削減目的で使用するのは誤り

スポンサーリンク

主題

題名通り、bit()をサイズ削減目的で使用するのは誤りです。以下に記載があるようにbit()を表現するために最低でも6byte必要です。

レコードサイズ削減の目的であれば素直にsmallintを使用するのがいいでしょう。

A bit string value requires 1 byte for each group of 8 bits, plus 5 or 8 bytes overhead depending on the length of the string (but long values may be compressed or moved out-of-line, as explained in Section 8.3 for character strings).

8.10. Bit String Types
8.10.Bit String Types # Bit strings are strings of 1's and 0's. They can be used to store or visualize …

試してみる

PostgreSQLでは、データサイズを調査するのに様々な関数が用意されています。

9.27. System Administration Functions
9.27.System Administration Functions # 9.27.1. Configuration Settings Functions 9.27.2. Server Signaling Functions 9.27.3. Backup Control Functions 9.27.4. Reco...

今回はcolumnのサイズ数が調査可能なpg_column_sizeを使用します。返り値はbyte単位です。

select 'bit(1)', pg_column_size(1::bit(1)) union
select 'bit(2)', pg_column_size(1::bit(2)) union
select 'bit(3)', pg_column_size(1::bit(3)) union
select 'bit(4)', pg_column_size(1::bit(4)) union
select 'bit(8)', pg_column_size(1::bit(8)) union
select 'bool', pg_column_size(true) union
select 'smallint', pg_column_size(1::smallint);

結果

補足データサイズ

PostgreSQLのデータレイアウトは以下に示されています。

68.6. データベースページのレイアウト

ページヘッダが24バイト、タプルごとにヘッダが24バイトが付与されます。これで大まかなデータサイズを見積もることが可能です。

タイトルとURLをコピーしました