BEFORE: SELECT ports.id, element.name as port, categories.name as category, ports.category_id, version as version, revision as revision, ports.element_id, maintainer, short_description, to_char(ports.date_added - SystemTimeAdjust(), 'DD Mon YYYY HH24:MI:SS') as date_added, last_commit_id as last_change_log_id, package_exists, extract_suffix, homepage, status, broken, forbidden , onwatchlist from element, categories, ports LEFT OUTER JOIN (SELECT element_id as wle_element_id, COUNT(watch_list_id) as onwatchlist FROM watch_list JOIN watch_list_element ON watch_list.id = watch_list_element.watch_list_id AND watch_list.user_id = 2 GROUP BY watch_list_element.element_id) AS TEMP ON TEMP.wle_element_id = ports.element_id WHERE ports.element_id = element.id and ports.category_id = categories.id and status = 'A' and ports.date_added > (now() - interval '1 day' - SystemTimeAdjust()) order by category, port Sort (cost=15093.39..15093.39 rows=1940 width=210) (actual time=2060.75..2060.75 rows=0 loops=1) -> Hash Join (cost=10059.80..14987.42 rows=1940 width=210) (actual time=2060.53..2060.53 rows=0 loops=1) -> Hash Join (cost=10058.12..14951.79 rows=1940 width=196) (actual time=2059.63..2059.63 rows=0 loops=1) -> Seq Scan on element (cost=0.00..2616.39 rows=81601 width=21) (actual time=0.19..831.92 rows=79154 loops=1) -> Hash (cost=9979.94..9979.94 rows=2873 width=175) (actual time=649.86..649.86 rows=0 loops=1) -> Hash Join (cost=2.17..9979.94 rows=2873 width=175) (actual time=647.53..647.53 rows=0 loops=1) -> Seq Scan on ports (cost=0.00..9963.40 rows=2873 width=163) (actual time=646.51..646.51 rows=0 loops=1) -> Hash (cost=2.17..2.17 rows=1 width=12) (actual time=0.91..0.91 rows=0 loops=1) -> Subquery Scan temp (cost=2.15..2.17 rows=1 width=12) (actual time=0.75..0.86 rows=2 loops=1) -> Aggregate (cost=2.15..2.17 rows=1 width=12) (actual time=0.73..0.81 rows=2 loops=1) -> Group (cost=2.15..2.16 rows=3 width=12) (actual time=0.65..0.73 rows=3 loops=1) -> Sort (cost=2.15..2.15 rows=3 width=12) (actual time=0.63..0.65 rows=3 loops=1) -> Hash Join (cost=1.04..2.14 rows=3 width=12) (actual time=0.41..0.49 rows=3 loops=1) -> Seq Scan on watch_list_element (cost=0.00..1.04 rows=4 width=8) (actual time=0.07..0.10 rows=4 loops=1) -> Hash (cost=1.04..1.04 rows=2 width=4) (actual time=0.21..0.21 rows=0 loops=1) -> Seq Scan on watch_list (cost=0.00..1.04 rows=2 width=4) (actual time=0.12..0.15 rows=5 loops=1) -> Hash (cost=1.54..1.54 rows=54 width=14) (actual time=0.82..0.82 rows=0 loops=1) -> Seq Scan on categories (cost=0.00..1.54 rows=54 width=14) (actual time=0.11..0.47 rows=54 loops=1) Total runtime: 2061.28 msec AFTER: explain analyze select TEMP.id, element.name as port, categories.name as category, TEMP.category_id, TEMP.version as version, TEMP.revision as revision, TEMP.element_id, TEMP.maintainer, TEMP.short_description, TEMP.date_added, TEMP.last_change_log_id, TEMP.package_exists, TEMP.extract_suffix, TEMP.homepage, element.status, TEMP.broken, TEMP.forbidden, onwatchlist FROM ( SELECT ports.id, ports.category_id, version as version, revision as revision, ports.element_id, maintainer, short_description, to_char(ports.date_added - SystemTimeAdjust(), 'DD Mon YYYY HH24:MI:SS') as date_added, last_commit_id as last_change_log_id, package_exists, extract_suffix, homepage, broken, forbidden, onwatchlist from ports LEFT OUTER JOIN (SELECT element_id as wle_element_id, COUNT(watch_list_id) as onwatchlist FROM watch_list JOIN watch_list_element ON watch_list.id = watch_list_element.watch_list_id AND watch_list.user_id = 2 GROUP BY watch_list_element.element_id) AS TEMP2 ON TEMP2.wle_element_id = ports.element_id where ports.date_added = (select now() - interval '17 day' - SystemTimeAdjust())::timestamp) AS TEMP, element, categories WHERE TEMP.category_id = categories.id and element.status = 'A' and TEMP.element_id = element.id order by category, port; Sort (cost=9906.90..9906.90 rows=1 width=210) (actual time=801.66..801.66 rows=0 loops=1) InitPlan -> Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.03..0.03 rows=1 loops=1) -> Nested Loop (cost=2.17..9906.89 rows=1 width=210) (actual time=801.46..801.46 rows=0 loops=1) -> Nested Loop (cost=2.17..9904.67 rows=1 width=196) (actual time=801.45..801.45 rows=0 loops=1) -> Hash Join (cost=2.17..9900.93 rows=1 width=175) (actual time=801.44..801.44 rows=0 loops=1) -> Seq Scan on ports (cost=0.00..9898.75 rows=1 width=163) (actual time=800.72..800.72 rows=0 loops=1) -> Hash (cost=2.17..2.17 rows=1 width=12) (actual time=0.65..0.65 rows=0 loops=1) -> Subquery Scan temp2 (cost=2.15..2.17 rows=1 width=12) (actual time=0.54..0.62 rows=2 loops=1) -> Aggregate (cost=2.15..2.17 rows=1 width=12) (actual time=0.52..0.58 rows=2 loops=1) -> Group (cost=2.15..2.16 rows=3 width=12) (actual time=0.47..0.53 rows=3 loops=1) -> Sort (cost=2.15..2.15 rows=3 width=12) (actual time=0.46..0.47 rows=3 loops=1) -> Hash Join (cost=1.04..2.14 rows=3 width=12) (actual time=0.31..0.39 rows=3 loops=1) -> Seq Scan on watch_list_element (cost=0.00..1.04 rows=4 width=8) (actual time=0.07..0.10 rows=4 loops=1) -> Hash (cost=1.04..1.04 rows=2 width=4) (actual time=0.19..0.19 rows=0 loops=1) -> Seq Scan on watch_list (cost=0.00..1.04 rows=2 width=4) (actual time=0.11..0.15 rows=5 loops=1) -> Index Scan using element_pkey on element (cost=0.00..3.45 rows=1 width=21) -> Seq Scan on categories (cost=0.00..1.54 rows=54 width=14) Total runtime: 802.25 msec