PostgreSQL - 加入 OSM 数据非常慢 table
PostgreSQL - very slow table join on OSM data
我在创建 table 的连接时遇到了问题。查询永远运行。我在一张 table 中打开了街道地图自行车路线,其中包含所有属性。
Table planet_osm_line
osm_id bigint,
route text,
name text,
network text,
osmc_color text,
reversed text,
state text,
"instance:cycle" text,
"relation:id" text,
ref text,
description text,
distance text,
tags hstore,
way geometry(LineString,900913)
有些线路是重复的(一种方式有 2 条或更多路线),因此我将独特的线路过滤到另一条 table 中,并尝试将它们与来自 planet_osm_line:[=14 的数据合并=]
DROP TABLE IF EXISTS public.bicycle_merge;
CREATE TABLE public.bicycle_merge AS
WITH singleRow as (
select count(way), way
from planet_osm_line
WHERE route IN ('bicycle')
group by way
having count(way) = 1
)
SELECT P.*
FROM planet_osm_line P
JOIN singleRow S
ON P.way = S.way
;
此查询永远运行....请原谅我的新手问题,但我做错了什么?
"Nested Loop (cost=28767.43..172920474.87 rows=5892712 width=335)"
" Join Filter: (p.way = s.way)"
" CTE singlerow"
" -> GroupAggregate (cost=27040.24..28767.43 rows=76764 width=218)"
" Filter: (count(planet_osm_line1.way) = 1)"
" -> Sort (cost=27040.24..27232.15 rows=76764 width=218)"
" Sort Key: planet_osm_line1.way"
" -> Seq Scan on planet_osm_line1 (cost=0.00..4543.55 rows=76764 width=218)"
Planet_osm_line table 有大约 70.000 行。独特的几何形状约为 50.000。
此查询适用于一小组数据,但现在我正在处理整个国家(波兰)的自行车路线。非常感谢您!
您正在加入两个普通几何图形。这意味着您正在对所有可能匹配项之间的几何结构进行二进制比较,逐字节比较。这确实需要很长时间。在您的 EXPLAIN ANALYZE
中,CTE 的成本为 28,767;联接是 6,000 倍。
相反,您应该测试两个几何图形是否相互接触(因为 OSM 已正确地理编码,您可以假设没有线相交):
WITH singleRow AS (
SELECT count(way), way
FROM planet_osm_line
WHERE route IN ('bicycle')
GROUP BY way
HAVING count(way) = 1
)
SELECT P.*
FROM planet_osm_line P
JOIN singleRow S ON <b>ST_Contains(P.way, S.way)</b>;
在您像这样检索的一组行上,您可以应用函数 ST_MakeLine()
将较小的行实际合并为一个。
我在创建 table 的连接时遇到了问题。查询永远运行。我在一张 table 中打开了街道地图自行车路线,其中包含所有属性。
Table planet_osm_line
osm_id bigint,
route text,
name text,
network text,
osmc_color text,
reversed text,
state text,
"instance:cycle" text,
"relation:id" text,
ref text,
description text,
distance text,
tags hstore,
way geometry(LineString,900913)
有些线路是重复的(一种方式有 2 条或更多路线),因此我将独特的线路过滤到另一条 table 中,并尝试将它们与来自 planet_osm_line:[=14 的数据合并=]
DROP TABLE IF EXISTS public.bicycle_merge;
CREATE TABLE public.bicycle_merge AS
WITH singleRow as (
select count(way), way
from planet_osm_line
WHERE route IN ('bicycle')
group by way
having count(way) = 1
)
SELECT P.*
FROM planet_osm_line P
JOIN singleRow S
ON P.way = S.way
;
此查询永远运行....请原谅我的新手问题,但我做错了什么?
"Nested Loop (cost=28767.43..172920474.87 rows=5892712 width=335)"
" Join Filter: (p.way = s.way)"
" CTE singlerow"
" -> GroupAggregate (cost=27040.24..28767.43 rows=76764 width=218)"
" Filter: (count(planet_osm_line1.way) = 1)"
" -> Sort (cost=27040.24..27232.15 rows=76764 width=218)"
" Sort Key: planet_osm_line1.way"
" -> Seq Scan on planet_osm_line1 (cost=0.00..4543.55 rows=76764 width=218)"
Planet_osm_line table 有大约 70.000 行。独特的几何形状约为 50.000。 此查询适用于一小组数据,但现在我正在处理整个国家(波兰)的自行车路线。非常感谢您!
您正在加入两个普通几何图形。这意味着您正在对所有可能匹配项之间的几何结构进行二进制比较,逐字节比较。这确实需要很长时间。在您的 EXPLAIN ANALYZE
中,CTE 的成本为 28,767;联接是 6,000 倍。
相反,您应该测试两个几何图形是否相互接触(因为 OSM 已正确地理编码,您可以假设没有线相交):
WITH singleRow AS (
SELECT count(way), way
FROM planet_osm_line
WHERE route IN ('bicycle')
GROUP BY way
HAVING count(way) = 1
)
SELECT P.*
FROM planet_osm_line P
JOIN singleRow S ON <b>ST_Contains(P.way, S.way)</b>;
在您像这样检索的一组行上,您可以应用函数 ST_MakeLine()
将较小的行实际合并为一个。