我有一个类型 2 数据的数据库表,我想查找自上次与它同步以来已删除的记录。它有date_from
和date_to
列,原始数据有一个 ID 列object_id
。 date_to<>null
表示它现在不存在,所以如果没有其他具有相同object_id
和的记录date_to=null
,则它已被删除。
我相信一个天真的实现会是这样的:
select * from data_t2 a
where a.date_to > last_sync_date and a.date_to < current_date()
and not exists (select * from data_t2 b
where b.date_to is null and b.object_id = a.object_id);
但显然这将是非常昂贵的。
我是否缺少一种明显更有效的方法?我怀疑没有(或者更确切地说,我应该假设删除的记录相对较少,并在 RDBMS 之外进行一些计算),但我想我会问以防万一。
谢谢!