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.
You should try this:

```
select * from table_2
LEFT JOIN `users` AS u ON u.user_id = table_2.user_1
LEFT JOIN `users` AS u2 ON u2.user_id = table_2.user_2
WHERE u.user_id is NULL and u2.user_id is NULL

```