What comes to my mind is to use the generation number for the table. Give the TODO table a column called generation, and (mentally) relate to it a global and constant variable number of generations. (It is probably best to put this in a table in the database).
Each time an update or insert is performed, the generation number is incremented and placed in the generation number column for the updated or inserted record.
When records are first read from the database and placed on a web page, the current generation number is also retrieved and cached locally (i.e., placed in a JavaScript variable). Subsequent reads with AJAX use the where clause, which filters only records with a generation number greater than the locally cached generation number. This ensures that only new updates and deletes are received. Then the locally cached generation number is updated.
Removing is a little trickier. One way to do this is to delete buried records for a limited period, say, after 21 minutes. You have a bit of a column labeled “Tombstone” and a column of “datetime”. When a record is deleted, the column has the generation number set in the same way as for insertion or deletion, the tombstone bit is set, and the datetime is set to the current time using GetDate (). An AJAX client request can then select entries with the tombstone flag set and remove them from the list on the client side. If the client is encoded in such a way that after every 20 minutes a full page is refreshed or an ajax call that receives each record, then the database task can be executed every minute and clear (i.e. delete) tombstones older than 20 minutes.
EDIT: you do not need to save the generation number separately, you can use the sql MAX function above the generation column.
source share