0

我有一个 sql 查询:

select person from table t1
inner join person_history ph
on t1.person = ph.person
and t1.person not in (select person from person_history 
                      where effective_date < '01-01-2013')
and ph.person.effective_date > '01-01-2013'

由于person_history包含大量记录,此查询耗时过长。

如何优化此代码?

4

2 回答 2

1

您无需排除 NOT IN,因为它已在 WHERE 过滤器中排除!SQL 将很简单,如下所示:

select person from table t1
inner join person_history ph on t1.person=ph.person
where effective_date > '01-01-2013'

或者:

select person from table t1
WHERE person IN(select ph.person from person_history ph
where effective_date > '01-01-2013' and t1.person=ph.person)
于 2012-12-26T21:25:04.153 回答
1

你不能做这样的事情:

select person from table t1
inner join person_history ph
on t1.person = ph.person
where ph.effective_date >= '01-01-2013'
于 2012-12-26T20:54:36.780 回答