Skip to content
Advertisement

Update & Select a MySQL table in FIFO queue with non-unique identifiers

I have this table documents based on client’s set of data. Only provided key is documentId, but it’s non-unique (and rowId is mine, not client’s).

This table also shows which documents have been processed and which have not, plus some additional data. isProcessed is a generated column (isProcessed bool as (processed is not null)) for indexing purposes.

Then, when employees process some documents, they pass their documentId's into an app, generating a JSON like this:

Now I need to figure out how to:

a) update documents to set processed and user for first matching unprocessed documents, but no more than that!

That’s 2 x 10022 (rows 6, 7), 1 x 10023 (row 9), 1 x 10024 (row 10), 3 x 10025 (rows 12, 13, 14).

What I’ve got so far is:

Is there a smarter way to approach this than generate multiple update statements?

b) select the updated documents to get their additional information and make a results file to send to the client

Is there a smarter way to select the updated data?

Expected results

a) table documents after updates

b) results file

(I know how to make that JSON, I’m looking for a smart way to select the updated data)

My question is…

In production, documents will have some 300k + rows, and each save run will affect hundreds of rows.

Is there a smarter way to update in bulk & select updated data?

Ideally, I’m looking for a way to pass [10022, 10022, 10023, 10024, 10025, 10025, 10025] onto the update in a way that will respect FIFO order and match only first listed documents, not more. Running hundreds of update statements does not seem ideal.

I’m using MySQL 8.0. The app is written in javascript (typescript) run on Node.js v12. I’m only looking for smarter MySQL solution. For suggestions with some logic in app layer, the answer doesn’t have to be in JS, any pseudocode is appreciated too.

Fiddle w/ table schema here: https://www.db-fiddle.com/f/ecYLwEGpwZpPtec6VwzMBF/1

Thank you very much.

Advertisement

Answer

A way to find the records in table documents which need an update:

This will output:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement