I am in the process of setting up a a multi-server installation where some user data gets synchronized across servers depending on the user's priority rating (a number going from 0 to ...). The synchronization is intended to be done as a lazy background job with users with a higher priority seeing their data synchronized faster.
To that end I have a DBTable
CREATE TABLE IF NOT EXISTS `spiegel` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`priority` tinyint(1) NOT NULL,
`cql` varchar(4096) NOT NULL,
PRIMARY KEY (`id`),
KEY `priority` (`priority`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
where all non-zero priority users have their INSERT/DELETE/UPDATE transactions noted. A cron job periodically (every 10 minutes) runs a PHP script that takes a few (I want to limit this to N entries, say, 100) entries and deals with the process of synchronizing the databases on the other servers.
What I want to do is
- Pickup a maximum N entries ordered by their priority
- Removing those entries from the table
The first bit is ok
SELECT * FROM `spiegel` ORDER BY priority LIMIT 100;
This is where my knowledge of SQL is letting me down. It is not clear to me how I can efficiently remove the "picked up" entries. The best I have been able to do is to create a CSV list of the ids of the removed entries and then use a
DELETE FROM `spiegel` WHERE id in idcsvlist
However, I suspect that this is a long winded way of doing things. I'd be much obliged to anyone who might be able to suggest a better approach.