我有以下表格是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;
這是一個可怕的數據庫設計。難怪你正面臨性能問題。數據庫是關於彼此相關的實體,例如國家和國家,每個國家都有自己的屬性。所以你應該有一個國家表格和一張州表格,每個州的記錄都會指向一個國家,有一個位置等等。然而,你有一件由三件事組成的「事物」,它們的組合構成了一個虛擬記錄的屬性,再加上它的適當位置。你甚至有兩次。改變你的數據模型,一切都會好的。 –