如何减少在postgres中运行查询的时间

问题描述 投票:0回答:1

我有以下查询,

SELECT seq, node, edge, cost as cost, agg_cost, geom, sT_AsGeoJson(geom), replace(
    (REPLACE(sT_AsGeoJson(geom),
     ( select ST_AsGeoJson(geom1) from sample(20.983455,52.231909,20.973400,52.169647) a where seq1 in ( select  min(seq1) from sample(20.983455,52.231909,20.973400,52.169647 ))),
   (SELECT (St_asgeoJson(ST_LineSubstring((select (geom1) from sample(20.983455,52.231909,20.973400,52.169647) a where seq1 in (select  min(seq1) from sample(20.983455,52.231909,20.973400,52.169647 ))),(select ST_LineLocatePoint((select st_astext(geom1) from sample(20.983455,52.231909,20.973400,52.169647) a where seq1 in (select  min(seq1) from sample(20.983455,52.231909,20.973400,52.169647 ))), (ST_MakePoint(20.9850904,52.2318352)))),1)))
    ))),
     ( select ST_AsGeoJson(geom1) from sample(20.983455,52.231909,20.973400,52.169647) a where seq1 in ( select  max(seq1) from sample(20.983455,52.231909,20.973400,52.169647 ))),
    (SELECT st_asgeojson((ST_LineSubstring((select (geom1) from sample(20.983455,52.231909,20.973400,52.169647) a where seq1 in (select  max(seq1) from sample(20.983455,52.231909,20.973400,52.169647 ))),0,(select ST_LineLocatePoint
                ((select st_astext(geom1) from sample(20.983455,52.231909,20.973400,52.169647) a where seq1 in (select  max(seq1) from sample(20.983455,52.231909,20.973400,52.169647 ))), (ST_MakePoint(20.9741841,52.1691584)))))))
     ))
      FROM pgr_dijkstra 
    ('SELECT id, source, target, st_length(geom, true) as cost FROM roads',
      (SELECT source FROM roads ORDER BY ST_Distance( ST_StartPoint(geom), ST_SetSRID(ST_MakePoint(20.983455, 52.231909), 4326), true ) ASC LIMIT 1),
      (SELECT target FROM roads ORDER BY ST_Distance( ST_StartPoint(geom), ST_SetSRID(ST_MakePoint(20.973400, 52.169647), 4326), true ) ASC LIMIT 1)) as pt
     JOIN roads rd ON pt.edge = rd.id;

while running query its taking 30 seconds.

Is there any way to reduce time for Executing.
postgresql postgis query-performance postgresql-9.5
1个回答
0
投票

减少时间,您需要注意:

  1. 查询的复杂度,例如,如果子查询要转'N'次,那么整个查询将取O(N POW(M)),其中M为查询的记录总数,] >
  2. 简化复杂性之后,使用索引来加快查询的性能。
  3. 使用主键轻松识别表中的记录
  4. [避免使用'Limit'关键字n子查询
  5. 尝试避免在子查询中使用组功能
  6. 例如:在您的“ WHERE”子句中,您有“ pt.edge = rd.id”您可以在引用的表>>中创建两列edge和id的组合的索引

© www.soinside.com 2019 - 2024. All rights reserved.