I have two tables that are joined together.
A has many B
Normally you would do:
select * from a,b where b.a_id = a.id
To get all of the records from a that has a record in b.
How do I get just the records in a that does not have anything in b?
The first approach is
the second approach is
The first approach is very expensive. The second approach is better.
With PostgreSql 9.4, I did the "explain query" function and the first query as a cost of cost=0.00..1982043603.32. Instead the join query as a cost of cost=45946.77..45946.78
For example, I search for all products that are not compatible with no vehicles. I've 100k products and more than 1m compatibilities.
The join query spent about 5 seconds, instead the subquery version has never ended after 3 minutes.
Or like some other people on this thread says:
In case of one join it is pretty fast, but when we are removing records from database which has about 50 milions records and 4 and more joins due to foreign keys, it takes a few minutes to do it. Much faster to use WHERE NOT IN condition like this:
I can also recommended this approach for deleting in case we don't have configured cascade delete. This query takes only a few seconds.
Another approach:
The "exists" approach is useful if there is some other "where" clause you need to attach to the inner query.