title: PostgreSQL slow query
body: <p>Having OpenVAS (backed by PostgreSQL) instance that is slow when opening 'Tasks' tab.</p>
  
  <p>Following query runs 22 seconds in PostgreSQL. Any suggestions how this can be optimized?</p>
  
  <pre><code>SELECT id, host,
         iso_time (start_time), iso_time (end_time),
         current_port, max_port, report,
         (SELECT uuid FROM reports WHERE id = report),
         (SELECT uuid FROM hosts
          WHERE id = (SELECT host FROM host_identifiers
                      WHERE source_type = 'Report Host'
                        AND name = 'ip'
                        AND source_id = (SELECT uuid FROM reports
                                         WHERE id = report)
                        AND value = report_hosts.host
                      LIMIT 1)
         )
  FROM report_hosts
  WHERE report = 702;
  </code></pre>
  
  <p>The plan is</p>
  
  <pre><code> Index Scan using report_hosts_by_report on report_hosts  (cost=0.42..1975570.99 rows=447 width=38) (actual time=50.042..22979.257 rows=1206 loops=1)
     Index Cond: (report = 702)
     SubPlan 1
       -&gt;  Index Scan using reports_pkey on reports  (cost=0.28..2.49 rows=1 width=37) (actual time=0.004..0.004 rows=1 loops=1206)
             Index Cond: (id = report_hosts.report)
     SubPlan 4
       -&gt;  Index Scan using hosts_pkey on hosts  (cost=4414.37..4416.59 rows=1 width=37) (actual time=0.001..0.001 rows=0 loops=1206)
             Index Cond: (id = $4)
             InitPlan 3 (returns $4)
               -&gt;  Limit  (cost=2.49..4414.09 rows=1 width=4) (actual time=18.998..18.998 rows=0 loops=1206)
                     InitPlan 2 (returns $2)
                       -&gt;  Index Scan using reports_pkey on reports reports_1  (cost=0.28..2.49 rows=1 width=37) (actual time=0.001..0.001 rows=1 loops=1206)
                             Index Cond: (id = report_hosts.report)
                     -&gt;  Seq Scan on host_identifiers  (cost=0.00..4411.60 rows=1 width=4) (actual time=18.997..18.997 rows=0 loops=1206)
                           Filter: ((source_type = 'Report Host'::text) AND (name = 'ip'::text) AND (source_id = $2) AND (value = report_hosts.host))
                           Rows Removed by Filter: 99459
   Planning time: 0.531 ms
   Execution time: 22979.575 ms
  </code></pre>