We had a bug in our code that cached the wrong values to the last_order_id
(expected_previous_order) column. The query I wrote properly finds the correct last order id but is too slow for our data set.
I want to select this data into another table but I cannot because the query will take too long
I have setup a simple example here with a smaller data set. Original table has about 170k rows.
In the example:
- original_artwork_id is how these rows are grouped.
- order_id is the current rows order id
- actual_previous_order is the corrected last order id
- expected_previous_order is the currently stored last order id. This is the wrong value as it does not actually reference the last order id
EXPLAIN Results
EDIT
Every time a reorder is placed a new entry is placed into the order_artwork
table with a reference to the original_artwork_id
and last_order_id
.
The reference in the current data set to the last_order_id
is wrong.
I need to update all records to properly indicate the last order id.
I am doing this by trying to find each artwork and joining it with the previous entry of the same original_artwork_id
. Then I can pull the order_id from the last entry to update the current entries last_order_id
Join the current row with the previous row created before the current row with the same original_artwork_id or the current row original_artwork_id = the previous rows id