tirana
Let’s say:
- I have 1 millions of records in a DB and I want to process them daily via something like Oban.
- there’s an Oban job which is executed hourly
- an Oban job is able to process around 70k-200k hourly
- it can’t be known in advance how many it’ll process each tiime
The key here is to process the records evenly, that is, simply running one job per 24 hours to have it process all the 1 mln of records at once, one by one, won’t do for me.
How would I split the records into parts and also memorize how many of them have been processed so far, daily? Meaning, in an easy manner.
I could create a DB table with a counter → id, date_time, counter
But it appears to be an overkill.
Won’t there be a better way?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
Then why not using something similar, but in memory like
:etstable?benwilson512
Great question! The basic principle we’re gonna use here is remainder math. At a basic level, you could spawn two jobs, one of which handled records that had even numbered ids, and another job that handled odd numbered ids. If you want to do more than just two jobs you can extend this concept by using the modulus operator
%.If you want to do this on a cron, my recommendation is to basically do a two phase job enqueuing process.
Phase 1: A regular Oban job that is configured to run hourly. That job spawns say 10 jobs to do the actual work, and it gives each job a number between 1 and 10 in the args, as well as the total number (for easier config)
Phase 2: The partition job runs, and it takes the arg given to it and queries your table of records.
And there you go! If a job is given say partition 7 then it will get all rows where
schema.id % 10 = 7which will be roughly 1 10th of the rows.The main upside to this approach is that you have a ton of control over the transactional characteristics because all of the relevant info ends up in the DB. It also handles node failure or cluster size changes reliably due to Oban unique jobs.
Eiji
Is it a good practice? I have no idea about
oban, but in worst case it could take even100%of rows as long as there is possibility to callRepo.delete/1onSomeSchemaand 90% rows are deleted. Of course nobody expects deletion of 90% rows in real case - I wrote that just to show the problem by providing an exaggerated example.What may be worth to mention is that all rows matching
schema.id % 10 = 7could be in worst case all first or all last rows, sosortcall with such code may have an unexpected behaviour.If possible I would advice to use
ROW_NUMBER()function which should solve be better solution forPostgreSQLdatabase. Regardless of how your data looks, how much is deleted and finally how you sort them it would always work exactly the same way. For more information please take a look at:dimitarvp
I’d probably go for a single Oban job that fetches records via
Repo.streamand then multiplex the record batches to other Oban jobs or just use good oldTask.async_streambut I admit I am not aware of the possible complexities or gotchas of using sub-workers and such.benwilson512
ROW_NUMBER() isn’t stable across different queries though. If a single row is deleted between process A doing the query and process B doing the query then the whole count is off and it won’t partition properly. The value of the modulus approach is that you’re using a value intrinsic to the row, which ensures that all parties can agree on which partition it belongs to just by looking at it knowing the partition counts.
I’m not really sure I follow your argument with respect to the modulus logic. It doesn’t really have an issue with deleted rows, unless for some reason all rows with a specific modulus value were specifically deleted, which seems hard to imagine in a regular use case. If you’re using auto incrementing IDs, then the probability distribution of modulus values is equal. So unless there is a bias in deletes towards specific modulus results I don’t really see what you’re saying.
Concrete example:
We generate a million auto incrementing keys, partition them modulo 10, and we see how many go to each partition. As expected it’s perfectly even. Let’s delete the first 90%:
Still even, as expected. Let’s delete a random 90%:
Still basically even. To get a non even result you have to have some sort of delete pattern that is more common in rows with ids ending with
9than rows ending with7and that just seems very rare to me, and is in any case easy to check in your own dataset to see if it’s happening.Eiji
Oh, did I missed that information somewhere? Could you please share more about it? It would be a good resource to learn.
benwilson512
From the link you provided:
The key words there are
result set. It’s the row_number within the set of things returned by a given query. Suppose there are 5 total rows with ids[1,2,3,4,5]. If Pid 1 queries all rows, sorts by id, and returns both the ID and row number it’s basically like doing:If ID number 2 is deleted between when the first pid runs this query, and the second pid runs this query, pid 2 will get a totally different row number for ids 3,4,5
Eiji
Ah, right - my bad. I was focused on count of rows in each group and therefore suggested a solution where said count is not different by more than one comparing to count in other groups. That part was good standalone, however I completely forgot that in such case we cannot accept changing count of rows in each group when one or more rows were deleted, because some rows who changed groups may not be processed at all.
tirana
Yes, it’s one of the solutions.
tirana
Ok. That would work.
However, what if
a) ID-s were not more or less sequential? So much so that that the ratio odd/even was 60%..40%? Or even 70%…30%?
b) ID-s weren’t integers but GUID-s?