考虑下表:
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;
我要做的是删除n
条记录,其中n
是条件少于20条的记录总数。表格相当大(超过1米记录) )。
条件是一个复杂的条件,涉及频繁更改数据的相对日期查询,并且还涉及其他几个表的连接,因此首先选择与条件匹配的数量,然后删除20个isn&# 39;据我所知,上班。
条件如下:
prize_details_id
为x
(需要针对几个不同的ID运行相同的查询)available_at
为空prize_id
不存在于另一个表status
是1
tier
(位于prize_detail
表格上方)位于y
答案 0 :(得分:2)
也许是这样的:
答案 1 :(得分:1)
可能的暗示:
mysql>
mysql> CREATE TABLE test (
-> id SMALLINT unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY
-> ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO test () VALUES
-> (),(),(),(),(),(),(),(),(),(),
-> (),(),(),(),(),(),(),(),(),(),
-> (),(),(),(),(),(),(),(),(),();
Query OK, 30 rows affected (0.01 sec)
Records: 30 Duplicates: 0 Warnings: 0
mysql> DELETE FROM t USING
-> test t JOIN (
-> SELECT id
-> FROM (
-> SELECT id
-> FROM test
-> ORDER BY id DESC
-> LIMIT 20, 1000
-> ) as ids) as ids ON t.id = ids.id;
Query OK, 10 rows affected (0.01 sec)
mysql> SELECT * FROM test;
+----+
| id |
+----+
| 11 |
| 12 |
| 13 |
| 14 |
| 15 |
| 16 |
| 17 |
| 18 |
| 19 |
| 20 |
| 21 |
| 22 |
| 23 |
| 24 |
| 25 |
| 26 |
| 27 |
| 28 |
| 29 |
| 30 |
+----+
20 rows in set (0.00 sec)
答案 2 :(得分:0)
DELETE FROM prize
WHERE
id IN(--Some query--) AND
id NOT IN(SELECT id FROM --Some query-- ORDER BY --some column-- LIMIT 20 )
订单通过标识“最后”20行