如果超过 100k 条记录,则从 table 中删除记录
Delete records from table, if it exceeds 100k records
我有一个条件,在审计日志中,如果记录超过100k,则删除以前的旧记录,我不想删除所有100k记录,但只想删除旧记录,我想维护最新的 10 万条记录。
下面是我试过的查询,请任何人帮助我,如何准备查询。
DELETE FROM audit_logs where
id not in (SELECT id from audit_logs order by ID DESC LIMIT 100000);
您可以包装到另一个 select 子查询中;
DELETE FROM audit_logs
WHERE id not in (SELECT t1.id
FROM ( SELECT id
FROM audit_logs
ORDER BY ID DESC
LIMIT 100000
) as t1
);
或使用NOT EXISTS
:
DELETE FROM audit_logs a1
WHERE NOT EXISTS ( SELECT *
FROM ( SELECT id
FROM audit_logs a2
ORDER BY ID DESC
LIMIT 100000
) as t1
);
我有一个条件,在审计日志中,如果记录超过100k,则删除以前的旧记录,我不想删除所有100k记录,但只想删除旧记录,我想维护最新的 10 万条记录。
下面是我试过的查询,请任何人帮助我,如何准备查询。
DELETE FROM audit_logs where
id not in (SELECT id from audit_logs order by ID DESC LIMIT 100000);
您可以包装到另一个 select 子查询中;
DELETE FROM audit_logs
WHERE id not in (SELECT t1.id
FROM ( SELECT id
FROM audit_logs
ORDER BY ID DESC
LIMIT 100000
) as t1
);
或使用NOT EXISTS
:
DELETE FROM audit_logs a1
WHERE NOT EXISTS ( SELECT *
FROM ( SELECT id
FROM audit_logs a2
ORDER BY ID DESC
LIMIT 100000
) as t1
);