0

I have an sql query:

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'

since person_history contains a lot of records, this query is taking too long.

How can I optimize this code?

Sam Mussmann
  • 5,883
  • 2
  • 29
  • 43

2 Answers2

1

Can't you just do something like this:

select person from table t1
inner join person_history ph
on t1.person = ph.person
where ph.effective_date >= '01-01-2013'
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
1

You needn't exclude the NOT IN as it is already excluded in the WHERE Filter! the SQL will be simple as follows:

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

Or:

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)
Atheer Mostafa
  • 735
  • 3
  • 8