好的,所以这里已经有几个帖子了,网上的帖子更少了。我实际上已经尝试了其中的每一个,但都无法正常工作。希望这里的人可以同情我:)
这是我正在使用的数据。我想删除所有这些记录。
SELECT
part_desc, count(*) as rec_num
FROM ag_master
GROUP BY part_desc HAVING COUNT(*) > 1000;
+--------------------------------------+---------+
| part_desc | rec_num |
+--------------------------------------+---------+
| SILICON DELAY LINE, TRUE OUTPUT | 1092 |
| LOADABLE PLD | 1401 |
| 8-BIT, FLASH, 8 MHz, MICROCONTROLLER | 1411 |
| FPGA | 1997 |
| 8-BIT, MROM, 8 MHz, MICROCONTROLLER | 3425 |
+--------------------------------------+---------+
5 rows in set (0.00 sec)
我找到的最接近的代码如下所示。语法检查正常并运行,但它似乎只是挂起数据库。我让它运行了长达 10 分钟,但什么也没发生,所以我中止了它。
DELETE
FROM ag_master
WHERE part_id IN (
SELECT part_id
FROM ag_master
GROUP BY part_desc
HAVING COUNT(*) > 1000
);
这是 tmp 表上的解释计划
mysql> EXPLAIN SELECT * FROM ag_master WHERE part_desc IN (SELECT part_desc FROM tmp);
+----+--------------------+-----------+--------+---------------+------+---------+------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+--------------------+-----------+--------+---------------+------+---------+------+--------+-------------+
| 1 | PRIMARY | ag_master | ALL | NULL | NULL | NULL | NULL | 177266 | Using where |
| 2 | DEPENDENT SUBQUERY | tmp | system | NULL | NULL | NULL | NULL | 1 | |
+----+--------------------+-----------+--------+---------------+------+---------+------+--------+-------------+
2 rows in set (0.00 sec)