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: 
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

```
This query will return all rows where user\_1 and/or user\_2 are not in the users table

```
select * from table_2 t1
where (select count(*) from users u where user_id in(t1.user_1,t1.user_2)) < 2

```