Answer the question
In order to leave comments, you need to log in
How to get the last actual row of a table in parallel queries?
There is a table datas:
id | last_data
------------
1 | 25000
2 | 75000
...
From time to time new data is inserted into the table according to the following algorithm: last_data = previous last_data + 50000. To do this, I use
SELECT last_data FROM datas ORDER BY id DESC LIMIT 1
I get the last value, add 50000 and INSERT a new row Answer the question
In order to leave comments, you need to log in
Either a transaction with a table lock, or an atomic query like
INSERT INTO `table` (`last_data`)
WITH `cte` (`max`) AS (
SELECT MAX(`last_data`)
FROM `table`
) SELECT `max` + 50000 FROM `cte`;
There is no point in adding - you need to store only the difference And get the sum when you select the data:
SELECT
id,
diff,
SUM(diff) OVER (ORDER BY id ASC)
FROM t;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question