title: How i can speed up query?
body: <p>Query: </p>
  
  <pre><code>with bus_points AS (
   select osm_id, way from opengeo.ru_psk_point where public_transport='stop_position' and tags -&gt; 'bus' = 'yes'
   )
   select 
   id_start,start_way,dist_start,id_end,end_way,dist_end
   ,(dist_start+dist_end) as sum_dist 
   from 
   (select osm_id as id_start, way as start_way, ST_Distance_Spheroid(ST_Transform(way,4326), ST_GeomFromText('POINT(28.2789393 57.8155523)',4326), 'SPHEROID["WGS 84",6378137,298.257223563]') as dist_start from bus_points order by dist_start) t1,
   (select osm_id as id_end, way as end_way, ST_Distance_Spheroid(ST_Transform(way,4326), ST_GeomFromText('POINT(28.2951125 57.8141805)',4326), 'SPHEROID["WGS 84",6378137,298.257223563]') as dist_end from bus_points order by dist_end) t2,
   opengeo.ru_psk_rels rels 
   where array[id_start,id_end]&lt;@rels.parts 
   and array['route','ref']&lt;@rels.tags 
   and bus_idx_rels_parts(rels.id,id_start)&lt;bus_idx_rels_parts(rels.id,id_end) 
   order by sum_dist limit 1;
  </code></pre>
  
  <p>Analyse:</p>
  
  <pre><code>"Limit  (cost=1606.91..1606.91 rows=1 width=96) (actual time=4396.739..4396.745 rows=1 loops=1)"
  "  CTE bus_points"
  "    -&gt;  Seq Scan on ru_psk_point  (cost=0.00..1568.94 rows=1 width=136) (actual time=0.066..17.067 rows=181 loops=1)"
  "          Filter: ((public_transport = 'stop_position'::text) AND ((tags -&gt; 'bus'::text) = 'yes'::text))"
  "          Rows Removed by Filter: 24330"
  "  -&gt;  Sort  (cost=37.97..37.97 rows=1 width=96) (actual time=4396.710..4396.710 rows=1 loops=1)"
  "        Sort Key: (((st_distance_spheroid(st_transform(bus_points.way, 4326), '0101000020E6100000B311E39068473C40BB568C0464E84C40'::geometry, 'SPHEROID("WGS 84",6378137,298.257223562997)'::spheroid)) + (st_distance_spheroid(st_transform(bus_points.way, 4326), '0101000020E61000000B24287E8C4B3C403D450E1137E84C40'::geometry, 'SPHEROID("WGS 84",6378137,298.257223562997)'::spheroid))))"
  "        Sort Method: top-N heapsort  Memory: 17kB"
  "        -&gt;  Nested Loop  (cost=33.39..37.96 rows=1 width=96) (actual time=40.099..4356.620 rows=4731 loops=1)"
  "              -&gt;  Nested Loop  (cost=0.57..0.61 rows=1 width=96) (actual time=32.312..651.802 rows=32761 loops=1)"
  "                    -&gt;  Sort  (cost=0.28..0.29 rows=1 width=40) (actual time=25.935..27.065 rows=181 loops=1)"
  "                          Sort Key: (st_distance_spheroid(st_transform(bus_points.way, 4326), '0101000020E6100000B311E39068473C40BB568C0464E84C40'::geometry, 'SPHEROID("WGS 84",6378137,298.257223562997)'::spheroid))"
  "                          Sort Method: quicksort  Memory: 29kB"
  "                          -&gt;  CTE Scan on bus_points  (cost=0.00..0.27 rows=1 width=40) (actual time=2.071..24.295 rows=181 loops=1)"
  "                    -&gt;  Sort  (cost=0.28..0.29 rows=1 width=40) (actual time=0.041..1.184 rows=181 loops=181)"
  "                          Sort Key: (st_distance_spheroid(st_transform(bus_points.way, 4326), '0101000020E61000000B24287E8C4B3C403D450E1137E84C40'::geometry, 'SPHEROID("WGS 84",6378137,298.257223562997)'::spheroid))"
  "                          Sort Method: quicksort  Memory: 29kB"
  "                          -&gt;  CTE Scan on bus_points  (cost=0.00..0.27 rows=1 width=40) (actual time=1.709..4.828 rows=181 loops=1)"
  "              -&gt;  Bitmap Heap Scan on ru_psk_rels rels  (cost=32.82..37.34 rows=1 width=86) (actual time=0.092..0.096 rows=0 loops=32761)"
  "                    Recheck Cond: ((ARRAY[bus_points.osm_id, bus_points.osm_id] &lt;@ parts) AND ('{route,ref}'::text[] &lt;@ tags))"
  "                    Filter: (bus_idx_rels_parts(id, bus_points.osm_id) &lt; bus_idx_rels_parts(id, bus_points.osm_id))"
  "                    Rows Removed by Filter: 0"
  "                    -&gt;  BitmapAnd  (cost=32.82..32.82 rows=1 width=0) (actual time=0.067..0.067 rows=0 loops=32761)"
  "                          -&gt;  Bitmap Index Scan on idx_ru_psk_rels_gin_parts  (cost=0.00..12.23 rows=31 width=0) (actual time=0.017..0.017 rows=0 loops=32761)"
  "                                Index Cond: (ARRAY[bus_points.osm_id, bus_points.osm_id] &lt;@ parts)"
  "                          -&gt;  Bitmap Index Scan on idx_ru_psk_rels_gin_tags  (cost=0.00..20.33 rows=44 width=0) (actual time=0.197..0.197 rows=499 loops=5735)"
  "                                Index Cond: ('{route,ref}'::text[] &lt;@ tags)"
  "Total runtime: 4397.374 ms"
  </code></pre>
  
  <p>I need only one row with minimum SUM_DIST.</p>
  
  <p><strong>MORE</strong></p>
  
  <pre><code>CREATE OR REPLACE FUNCTION bus_idx_rels_parts(IN bigint, IN bigint, OUT idx integer)
  RETURNS integer AS
  ' SELECT idx from opengeo.ru_psk_rels_parts_idx where id=$1 and unnest=$2 '
  LANGUAGE sql VOLATILE
  COST 100;
  </code></pre>
  
  <p>and ORDER BY dist_* speed up time.</p>
  
  <p>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</p>