This is my first S/O question! I'm stumped on what seems to be a basic query; given a list of values for various rows, return only the values that have changed. I am building a live-updating feature for a ordering system and I need to update rows on the client browser that have changed since the last (5 second) polling interval. Since I'm dealing with thousands of rows I need to only update what's changed.

Here's what my table looks like:
id, order_number, department

My client sends a list of two values (x1000+ items):
order_number, department

'department' is the value that I'm looking to see if a change has occurred.

I need a way to query the DB once with the entire list from the client and have MySQL return a list of only the changed items. Given a list of 1500 orders, it's likely that only one will ever be changed at a time, so I think for efficiency it makes sense to have the DB engine do this for me rather than looping through the list and doing a query each time. I tried doing it with a temp table and while it kind of worked, I found that the DROP TABLE at the end precluded my query results from being returned:

    `order_number` varchar(128) DEFAULT NULL,
    `department` int(11) DEFAULT '1'
INSERT INTO orderlist (order_id, department) VALUES ('180425','2');
INSERT INTO orderlist (order_id, department) VALUES ('180426','2');
INSERT INTO orderlist (order_id, department) VALUES ('180427','3');
SELECT orders.order_number, orders.department
FROM orders
JOIN orderlist USING(order_number)
WHERE orderlist.department != orders.department;
DROP TABLE orderlist;

1 Answer 1


One solution could be to add a last_updated timestamp on update current_timestamp column to your orders table. So when an update happens, this column is automatically updated by MySQL to contain the current time.

When you query the database, use the current time minus 5 seconds to compare against the last_updated column i.e.

where last_updated > now()-5

This will give you only the orders updated in the last 5 seconds.

Make sure you add an index on the new last_updated column.

  • I can't believe I didn't consider that! I already have those fields so that is easily done. I'd still really like to know the answer to my question as I could see it being useful info, but I think your method will solve the problem I actually came here to solve. Thanks!
    – Brian
    Aug 2, 2017 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.