There is a table of users in which the history of changes in their state should be entered daily:
CREATE TABLE IF NOT EXISTS `users` ( `user_id` BIGINT UNSIGNED NOT NULL , `day` INTEGER UNSIGNED NOT NULL , `action` TINYINT(1) UNSIGNED NOT NULL , PRIMARY KEY(`user_id`,`day`) , INDEX(`day`) ) The day column is the day number, the action column is an event about whether the user is added (value, for example, "1") or has been deleted (value "0").
The problem is that there are a lot of users (from 1 million) and it is required to synchronize with the received data as quickly and economically as possible in terms of memory consumption.
The incoming data (from the network) by users can be accumulated in a separate table, and then synchronized. Actually, I did, but due to the fact that this is a history table, and not, say, just the current state, synchronization requests become very resource-intensive. For example, one has to take into account that each user can be deleted (the value in action is "0"), and a day later it is added again (value "1"). And such "swings" (added-deleted-added) can occur day after day.
It may make sense to create a separate table before synchronization, where to drop all users with their current state, i.e. create a snapshot of their current state. Synchronize incoming data with this temporary table and only then change users who have changed their state to overwrite in the target table. But I, unfortunately, have no confidence that this approach will work faster, because one way or another, you still need to read the target table completely in order to make a copy of the latter at the time of state synchronization.
Perhaps there is a solution more interesting, rather than what I was able to come up with. Please prompt.
Update
The data received for synchronization is a list of user IDs that are currently available.
Update 2
The list of users comes from day to day almost the same, but with changes for a certain number of users. Parts of them that were previously will not, but there are new ones. It is this difference in composition that needs to be determined by appending lines to the history table with changes for each user, if any. If the incoming data is the same as in the target table (the user lists are equal), then the target table does not change at all.
PRIMARY KEY: bothuser_idandday. That is, it is important to save all state changes and when they occurred. - alexis031182