Skip to content

Instantly share code, notes, and snippets.

@den-crane
Last active June 2, 2024 22:52
Show Gist options
  • Save den-crane/4676ad82e7c6db0db4866ddbd111159b to your computer and use it in GitHub Desktop.
Save den-crane/4676ad82e7c6db0db4866ddbd111159b to your computer and use it in GitHub Desktop.
uuid4, uuid7, SnowflakeID, ULID compression rate

https://fiddle.clickhouse.com/35bf07da-101e-4e55-a408-1a597099f99b

CREATE TABLE t(
  int64 UInt64 codec(Delta, ZSTD(3)),
  ulid FixedString(26) default generateULID() codec(ZSTD(3)),
  uuid4 UUID default generateUUIDv4() codec(ZSTD(3)),
  Snowflake UInt64 default generateSnowflakeID()  codec(Delta,ZSTD(3)),
  uuid7 UUID default generateUUIDv7() codec(ZSTD(3))
)ENGINE = MergeTree() order by int64;

insert into t (int64)
SELECT rowNumberInAllBlocks() as int64 FROM numbers(2000000);

insert into t (int64)
SELECT rowNumberInAllBlocks()+2000000 as int64 FROM numbers(2000000);

insert into t (int64)
SELECT rowNumberInAllBlocks()+4000000 as int64 FROM numbers(2000000);

select count(*) cnt, 
  uniqExact(int64) qe, 
  uniqExact(ulid) uulid, 
  uniqExact(uuid4) ueUUID, 
  uniqExact(Snowflake) uSnowflake, 
  uniqExact(uuid7) uuuid7 
from t format PrettyCompactMonoBlock;

   +-----cnt-+------qe-+---uulid-+--ueUUID-+-uSnowflake-+--uuuid7-+
1. | 6000000 | 6000000 | 6000000 | 6000000 |    6000000 | 6000000 |
   +---------+---------+---------+---------+------------+---------+
   

SELECT column, any(type) type,
       formatReadableSize(sum(column_data_compressed_bytes) as s) size
FROM system.parts_columns
WHERE table = 't' AND active
GROUP BY column
ORDER BY s format PrettyCompactMonoBlock;

   +-column----+-type------------+-size------+
1. | int64     | UInt64          | 54.52 KiB |
2. | Snowflake | UInt64          | 68.38 KiB |
3. | ulid      | FixedString(26) | 2.51 MiB  |
4. | uuid7     | UUID            | 28.78 MiB |
5. | uuid4     | UUID            | 91.58 MiB |
   +-----------+-----------------+-----------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment