Question: I have the following query:

```
select * from table_2 
where 
user_1 not in (select user_id from users) or 
user_2 not in (select user_id from users)

```

The problem is that it takes very long time to execute. The tables have few millions of records. Is there any optimization that I can use for the query?

Answer: 
First, rewrite these as `not exists` . . . this often has better performance:

```
select t2.*
from table_2 t2
where not exists (select 1 from users u where u.user_id = t2.user_1) or 
      not exists (select 1 from users u where u.user_id = t2.user_2);

```

More importantly, create an index on `users(user_id)`, if one does not already exist.
```
SELECT * FROM table_2 t2 
WHERE
NOT  EXISTS( SELECT 1 FROM users u WHERE  u.user_id IN(t2.user_1,t2.user_2) )

```

this should perform better.