better still: explain analyze SELECT DISTINCT BugBranch.branch FROM Bug, BugBranch WHERE BugBranch.branch IN (451217, 451208, 451207, 451206, 310641, 450226, 449792, 449762, 449192, 449190, 444685, 439423, 436128, 421710, 317004, 374978, 404904, 412011, 411115, 245099, 400223, 377965, 373979, 372939, 372691, 362490, 362566, 362564, 361252, 359795, 359423, 357633, 355943, 348553, 322307, 319438, 316198, 316197, 315929, 310090, 308190, 305716, 305524, 296541, 294443, 245102, 275463, 273980, 40682, 34254, 175815, 152799, 115607, 81913, 81670, 62623, 52209, 42949, 39318, 38448, 37654, 12332, 30797, 21766, 13518, 16918, 12486, 10605, 10457, 5670, 9447, 7882, 4233, 3674, 2510, 2469) AND Bug.id = BugBranch.bug AND (Bug.private = FALSE OR exists (SELECT TRUE FROM BugSubscription, TeamParticipation WHERE TeamParticipation.team = BugSubscription.person AND TeamParticipation.person = 67034 and bug.id=BugSubscription.bug)); QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- HashAggregate (cost=12527.12..12527.51 rows=39 width=4) (actual time=186.053..186.064 rows=18 loops=1) -> Nested Loop (cost=0.00..12526.88 rows=97 width=4) (actual time=1.613..186.019 rows=18 loops=1) -> Seq Scan on bugbranch (cost=0.00..9341.04 rows=97 width=8) (actual time=1.585..185.567 rows=18 loops=1) Filter: (branch = ANY ('{451217,451208,451207,451206,310641,450226,449792,449762,449192,449190,444685,439423,436128,421710,317004,374978,404904,412011,411115,245099,400223,377965,373979,372939,372691,362490,362566,362564,361252,359795,359423,357633,355943,348553,322307,319438,316198,316197,315929,310090,308190,305716,305524,296541,294443,245102,275463,273980,40682,34254,175815,152799,115607,81913,81670,62623,52209,42949,39318,38448,37654,12332,30797,21766,13518,16918,12486,10605,10457,5670,9447,7882,4233,3674,2510,2469}'::integer[])) -> Index Scan using bug_pkey on bug (cost=0.00..32.83 rows=1 width=4) (actual time=0.020..0.020 rows=1 loops=18) Index Cond: (bug.id = bugbranch.bug) Filter: ((NOT bug.private) OR (alternatives: SubPlan 1 or hashed SubPlan 2)) SubPlan 1 -> Nested Loop (cost=0.00..26.48 rows=1 width=0) (never executed) -> Index Scan using bugsubscription_bug_idx on bugsubscription (cost=0.00..7.26 rows=3 width=4) (never executed) Index Cond: ($0 = bug) -> Index Scan using teamparticipation_team_key on teamparticipation (cost=0.00..6.39 rows=1 width=4) (never executed) Index Cond: ((public.teamparticipation.team = public.bugsubscription.person) AND (public.teamparticipation.person = 67034)) SubPlan 2 -> Nested Loop (cost=0.00..2954.48 rows=4062 width=4) (never executed) -> Index Scan using teamparticipation_person_idx on teamparticipation (cost=0.00..273.54 rows=106 width=4) (never executed) Index Cond: (person = 67034) -> Index Scan using bugsubscription_person_idx on bugsubscription (cost=0.00..25.17 rows=10 width=8) (never executed) Index Cond: (public.bugsubscription.person = public.teamparticipation.team) Total runtime: 186.279 ms (20 rows)