Consider the following table:
CREATE TABLE `prize` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`prize_details_id` bigint(20) NOT NULL,
`status` tinyint(4) DEFAULT '0',
`available_at` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `prize_details_id_idx` (`prize_details_id`),
KEY `status_idx` (`status`),
KEY `available_at_idx` (`available_at`),
CONSTRAINT `prize_prize_details_id_prize_detail_id` FOREIGN KEY (`prize_details_id`) REFERENCES `prize_detail` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1611419 DEFAULT CHARSET=latin1;
What I'd like to do is delete n
records, where n
is the total number of records that match a condition less 20. The table is reasonably big (greater than 1m records).
The condition is a complex one that involves a relative date query on frequently changing data, and involves a join on a couple of other tables too, so first selecting how many there are that match the condition and then deleting twenty fewer isn't going to work as far as I know.
The condition is all of the following:
prize_details_id
isx
(need to run the same query for a few different IDs)available_at
is nullprize_id
doesn't exist in another tablestatus
is1
tier
(on theprize_detail
table) is abovey