ukutaht
Best way to generate random unisgned 64-bit integers?
Hey all, wanted to run a quick sanity check by you.
I’m building an analytics database and I need to generate random IDs for sessions in the app server. To achieve better compression in the database, I don’t want to use UUID strings there but I’d rather use UInt64 data types.
So I’m left with the question of what’s the best way to generate random UInt64 values? Here’s my current approach:
def random_uint64() do
:crypto.strong_rand_bytes(8) |> :binary.decode_unsigned()
end
Any obvious issues with doing this?
Most Liked
Nicd
Not sure what database you are using, but at least PostgreSQL has a real UUID type that is not stored as a string into the database. Of course that’s 128 bits instead of 64.
If you intend to store 64 bit unsigned integers into the DB, note that PostgreSQL’s bigint type is signed, and so cannot fit all unsigned values.
If you are not using PostgreSQL, just ignore this post. ![]()
ukutaht
Thanks
I’m actually moving from Postgres to Clickhouse, here’s the repo: GitHub - plausible/analytics: Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud. · GitHub
I was using the UUID type in Postgres but after a consultation with some Clickhouse experts they told me that UInt64 is smaller and has enough cardinality for my use-case.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance










