EXPLAIN (costs on,verbose on,analyze on,buffers off) SELECT id FROM user_search.users u WHERE NOT EXISTS (SELECT r.other_user_id FROM rel_8192_60.relationships r WHERE r.user_id = 36012092 AND r.other_user_id = u.id AND ( r.state != 'default' OR r.other_state = 'disliked')) AND status = 'default' AND id NOT IN (SELECT UNNEST(q.*) FROM user_search.select_contact_user_ids(36012092) AS q) AND last_activity > current_timestamp at time zone 'UTC' - INTERVAL '5 day' AND (looking_for_gender = 'both' OR looking_for_gender = 'female'::user_search.gender) AND ('male'::user_search.gender = 'both' OR gender = 'male'::user_search.gender) AND latest_location IS NOT NULL AND birthdate BETWEEN DATE(current_timestamp at time zone 'UTC' - (26 + 1|| 'years')::interval + '1 day'::interval) AND DATE(current_timestamp at time zone 'UTC' - (20 || 'years')::interval) AND int4range(search_min_age,search_max_age) @> date_part('year', age('1995-03-16'::date))::int AND search_radius > ST_Distance('0101000000DFE00B93A9425E409BE61DA7E8283E40'::geography, latest_location::geography) ORDER BY latest_location::geometry <-> '0101000000DFE00B93A9425E409BE61DA7E8283E40'::geometry ASC LIMIT 1000; ---- ============================================= QUERY PLAN ---- ============================================= ------------------------------------------------------------------------------------------------- Limit (cost=2.04..2349.31 rows=80 width=12) (actual time=227.526..865.751 rows=1000 loops=1) Output: u.id, ((u.latest_location <-> '0101000000DFE00B93A9425E409BE61DA7E8283E40'::geometry)) -> Nested Loop Anti Join (cost=2.04..2349.31 rows=80 width=12) (actual time=227.524..865.585 rows=1000 loops=1) Output: u.id, (u.latest_location <-> '0101000000DFE00B93A9425E409BE61DA7E8283E40'::geometry) -> Index Scan using dba_suggested_users_basic_idx on user_search.users u (cost=1.48..2123.68 rows=80 width=36) (actual time=175.352..734.580 rows=40263 loops=1) Output: u.id, u.latest_location Index Cond: ((int4range(u.search_min_age, u.search_max_age) @> (date_part('year'::text, age((('now'::cstring)::date)::timestamp with time zone, ('1995-03-16'::date)::timestamp with time zone)))::in teger) AND (u.birthdate >= date(((timezone('UTC'::text, now()) - ('27years'::cstring)::interval) + '1 day'::interval))) AND (u.birthdate <= date((timezone('UTC'::text, now()) - ('20years'::cstring)::interval))) A ND (u.last_activity > (timezone('UTC'::text, now()) - '5 days'::interval))) Order By: (u.latest_location <-> '0101000000DFE00B93A9425E409BE61DA7E8283E40'::geometry) Filter: ((NOT (hashed SubPlan 1)) AND (u.gender = 'male'::user_search.gender) AND ((u.looking_for_gender = 'both'::user_search.gender) OR (u.looking_for_gender = 'female'::user_search.gender)) AND ((u.search_radius)::double precision > _st_distance('0101000020E6100000DFE00B93A9425E409BE61DA7E8283E40'::geography, (u.latest_location)::geography, '0'::double precision, true))) Rows Removed by Filter: 55131 SubPlan 1 -> Function Scan on user_search.select_contact_user_ids q (cost=0.25..0.76 rows=100 width=4) (actual time=0.235..0.237 rows=3 loops=1) Output: unnest(q.q) Function Call: user_search.select_contact_user_ids(36012092) -> Index Scan using relationships_user_id_other_user_id_idx on rel_8192_60.relationships r (cost=0.56..2.79 rows=1 width=4) (actual time=0.003..0.003 rows=1 loops=40263) Output: r.other_user_id Index Cond: ((r.user_id = 36012092) AND (r.other_user_id = u.id)) Filter: ((r.state <> 'default'::rel_8192_60.relationship_state) OR (r.other_state = 'disliked'::rel_8192_60.relationship_state)) Planning time: 0.956 ms Execution time: 866.176 ms (20 rows)