我有以下表格是postgres(postgis)。这两个表有1,不同类型的字符第2,第3列(9000)和类型“几何”的第4列(包含点几何(纬度,经度)):加入两张表
Table1
Column1 Column2 Column3 the_geom
Oklahoma numericalValue1 719 NULL
Oklahoma geometry NULL (34.6,95.3)
Oklahoma liesIn America NULL
Table2
Column1 Column2 Column3 the_geom
Mississippi liesIn America NULL
Mississippi geometry NULL (32.7,-89.53)
Mississippi numericalValue2 15.3 NULL
我想运行下面的查询在表1和表2上执行空间连接。问题中显示的表1和表2是玩具表,在我的实际数据集中,它们每个包含100万行。当我运行下面给出的查询时,我发现我的查询需要很长时间(超过10小时)才能运行。有人可以建议,如果有什么方法可以通过重新构建查询来优化查询。
select * from Table1 s1, Table1 s2, Table1 s3, Table2 s4, Table2 s5, Table2 s6 where
s1.column2='numericalValue1' and
s2.column2='geometry' and
s3.column2='liesIn' and
s1.column1=s2.column1 and
s2.column1=s3.column1 and
s4.column2='liesIn' and
s5.column2='geometry' and
s6.column2='numericalValue2' and
s4.column1=s5.column1 and
s5.column1=s6.column1 and
ST_DWithin(s2.the_geom, s5.the_geom, 5)
order by (cast(s1.column3 as double precision)+cast(s6.column3 as double precision))
limit 1;
这是一个可怕的数据库设计。难怪你正面临性能问题。数据库是关于彼此相关的实体,例如国家和国家,每个国家都有自己的属性。所以你应该有一个国家表格和一张州表格,每个州的记录都会指向一个国家,有一个位置等等。然而,你有一件由三件事组成的“事物”,它们的组合构成了一个虚拟记录的属性,再加上它的适当位置。你甚至有两次。改变你的数据模型,一切都会好的。 –