nhpip
Hi,
So I’m looking at a project that involves setting special activity flags for users. I won’t go into the reasons why, bit suffice to say that efficiency is pretty important.
Now I have done something similar before where I created a bit-vector. For example:
add_activity = fn(id, activity) -> Bitwise.bor(activity, Bitwise.bsl(1, id)) end
id_active? = fn(id, activity) -> Bitwise.bor(Bitwise.bsl(1, id), activity) == activity end
The numbers do get quite large, but it’s still more efficient than lists or strings. The ability to do bitwise operations really helps performance when searching and updating.
However, I’d like to be able to save the data in PostgreSQL with ecto.
There will be a row per organization per day, but I only need to update the fact a user was active on a given day (so idempotent). It would be nice if we can keep the same efficiency here too (for inserts, updates and search).
Search: It would be nice to be able to say ‘get rows and position numbers that have bit set to 1’.
Update: It would be nice to be ably to apply a bitwise or to update an existing row.
I looked at the following Postgres types:
number: efficient, but no real way to do queriesbit / var bit: In theory awesome. I can probably write a fragment to do nice queries using bitwise operations. But, Postgres can’t seem to handle large numbers here. I need to do ugly chunking operations to, for example, cast from a numeric type tobitor to do boolean operations (it seems like 64 bits are some kind of internal limit).bytea: possible, although searching could be a pain.array: possibly the best in terms of updates and search, although casting could be awkward. Also, may not be that memory efficient ({:array, :boolean}maybe).
I’m leaning towards an array type (an array of booleans). I would, however, like casting to be efficient as well as memory efficient too.
Anyone have any recommendations?
Trending in Questions
Other Trending 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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
dimitarvp
Nothing immediately jumps to mind except maybe support your
{:array, boolean}idea, combined with a library a la ExBin — ExBin v0.4.0.Also that library supports chunking so maybe it won’t be so awkward.
D4no0
Have you thought about using a custom ecto type? seems the perfect use-case to me.
nhpip
Yes to both of you. Although it would be nice to be able to do:
LostKobrakai
Wrap a few of those in fragments / custom macros:
nhpip
Yeah…this is the problem I run into with
bit. Same applies to bit shift operations.https://www.appsloveworld.com/django/100/39/how-can-i-cast-an-long-numeric-integer-into-a-bit-string-in-postgresql?expand_article=1
LostKobrakai
Not sure I see the exact issue. Casting numbers to bit is documented on the linked page:
nhpip
The problem is it doesn’t work with
NUMERICSnhpip
An array of booleans seems to be fine.