一个JOIN比两个相同的SELECT工作得慢

时间:2017-09-07 09:16:18

标签: postgresql postgis nominatim

我有这个问题:

SELECT *
FROM placex AS place
WHERE ST_DWithin(geometry, (SELECT geometry FROM placex WHERE place_id = 412369), 300)
AND name->'name' = (SELECT name->'name' FROM placex WHERE place_id = 412369)

交货期:530-650ms

这个查询使用两个SELECT,它很丑陋,我想要使用连接重写。但是这个查询:

SELECT same.*
FROM placex AS same
JOIN placex AS place ON place.place_id = 412369
WHERE ST_DWithin(same.geometry, place.geometry, 300)
AND same.name->'name' = place.name->'name';

在2.8秒内完成。默认情况下,所有索引均为默认值(Nominatim)。如何优化第二个查询?或者先使用而不用担心?

PostgreSQL 9.6.4,POSTGIS 2.3.3 r15473

EXPLAIN (ANALYZE, BUFFERS)

1 个答案:

答案 0 :(得分:0)

我无法猜出原因,但我认为你可以尝试像这样重写第二个查询

$(window).on("load", function() {
    alert("window is loaded including images");
});

或者您可以将第一个查询重写为

SELECT same.* 
FROM   placex AS same 
       JOIN (SELECT name, 
                    geometry 
             FROM   placex 
             WHERE  place_id = 412369) AS place 
         ON ST_DWithin(same.geometry, place.geometry, 300) 
            AND same.name->'name' = place.name->'name';