I\'m doing the following two queries quite frequently on a table that essentially gathers up logging information. Both select distinct values from a huge number of rows but
On PostgreSQL 9.3, starting from the answer from Denis:
select bundles.bundle_id
from bundles
where exists (
select 1 from audit_records
where audit_records.bundle_id = bundles.bundle_id
);
just by adding a 'limit 1' to the subquery, I got a 60x speedup (for my use case, with 8 million records, a composite index and 10k combinations), going from 1800ms to 30ms:
select bundles.bundle_id
from bundles
where exists (
select 1 from audit_records
where audit_records.bundle_id = bundles.bundle_id limit 1
);