bvt123 / SCH

GNU General Public License v3.0
1 stars 4 forks source link

snowFlakeID #6

Open bvt123 opened 9 months ago

bvt123 commented 9 months ago

The scheduler uses a tuple(now64(),rowNumberInAllBlocks) to build a unique ID for coming events. It's UInt64+UInt32. It could be done better by using SnowFlakeId (Int64). 22 bits is enough for operating 4M row blocks. for 1M blocks, we have space for 4 different cluster nodes.

create function toSnowflake64 as (dt,ch) ->
  bitOr(dateTime64ToSnowflake(dt),
   bitAnd(bitAnd(ch,0x3FFFFF)+
      bitAnd(bitShiftRight(ch, 20),0x3FFFFF)+
      bitAnd(bitShiftRight(ch, 40),0x3FFFFF),
      0x3FFFFF) 
  );

with rowNumberInAllBlocks() as rn,
  now64() as dt
SELECT hex(toSnowflake64(dt,rn) as sn),
  snowflakeToDateTime64(sn) 
from numbers(10);