Search code examples
sqlpostgresqlpostgresql-performance

Query taking a very long time


I have the following query to return userids that are not in the message_log table

select * from likes where userid not in(select to_id from message_log)

I have an index on the userid column in the likes table and an index on the to_id column in the message_log table but the index are not being used according to EXPLAIN. Is something wrong with my query? My query has been running for 20 minutes and still no results.


Solution

  • You can try this

    select * from likes lk where not exists (select 1 from message_log where to_id = lk.userid )