地理空间数据库:空间索引的创建与查询
利用PostgreSQL/PostGIS扩展模块对空间数据索引管理,提升空间查询效率。
要求:
(1)理解空间索引的概念;
(2)掌握PostGIS:B-Tree、GiST、SP-GiST三类典型索引算法;
(3)能够对各类空间数据创建索引、优化索引。
1 概念回顾
1.1 PostGIS支持的索引
PostGIS默认支持三类空间索引:B-Tree、GiST、SP-GiST;
1、B-Tree索引
用于可以在一个方向上排序的数据,如数字(numbers),字母(letters),日期(dates)。地理数据不能再一个方向上排序,所以B-Tree不能用于地理数据。
R-Trees是将数据分解成矩形,子矩形,子子矩形等。R-Trees被一些数据库用于地理数据的索引。但是PostgreSQL的R-Tree实现没有GiST实现那么健壮。
2、GiST(Generalized Search Trees)索引
是“通用搜索树”,将数据分解成“东西在哪一边”,“东西覆盖什么”,“东西在什么里”,它可以用于广泛的数据结构,包括地理数据。重点处理:范围是否相交,是否包含,地理位置中的点面相交,或者按点搜索附近的点等。
GiST通过R-Tree去索引地理数据。
GiST用于加快各种不适用于B-Tree索引的不规则数据结构的查询速度。
注(1):一旦地理数据表超过几千行,你就需要建立一个索引来加快数据的空间搜索(除非你的所有搜索都基于非地理属性)。
注(2):创建索引后,需要通知PostgreSQL收集表的统计数据,以便优化查询规则,否则默认不使用索引。
3、BRIN索引
是一种块范围索引,只存储包含在一组表块(称为范围)中所有行的所有几何图形的边界。因此,要求数据和物理位置关联,而且不能太频繁更新,在物理顺序和逻辑顺序越一致的列,越适合建立BRIN索引。PostgreSQL
9.5版本开始引入。大多数情况下,效率是低于GiST索引的。
【什么情况用BRIN,不用GiST?】
只要GiST索引的大小不超过数据库可用的RAM大小,并能负担存储和工作负责空间损失,就可以用GiST索引,否则考虑使用BRIN索引。
4、SP-GiST索引
SP-GiST是空间划分GiST(Space-partitioned
GiST)的简称。SP-GiST支持划分搜索树,它们可用于开发许多各种不同的非平衡数据结构,例如四叉树、k-d树和单词查找树。这些结构的共同特征是它们反复地将搜索空间划分成大小不需要相等的分区。匹配这些划分规则的搜索将会很快。
存储的也是几何对象的边界,可作为GiST的一种候选索引,其性能在几何对象存在众多相互重叠时,会优于GiST索引。
2 实验任务
以实验2北美+南美数据为基础,将作业二中题目一空间查询利用空间索引实现,并展示相关结果。具体要求:
1、对北美数据相关几何字段建立GiST索引【postgresSQL会默认在geom字段建立索引】;
默认索引如下:
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/9c64cd6a1670d0d78cd9cc845c36aa06.png)
可以删除后重建;
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/ff72b4ee1fe7869d515d03321212391e.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/1d858a574549d5de2f78b4bd2a28cf4b.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/cf6fd97482405c4602d56b44e4f03a09.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/efee3d8e902cca0984ffe3659eb2cabb.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/6d4fbad9bec41318b12789596e8f96f7.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/4a1f5c59dbfba2d292a35989668973c7.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/aa84772f9021ae0944204c2b7d1f1a52.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/f882f6b76692e679d3cf94157f2c12d5.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/868850e200d3210386c6fa38052f83ff.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/02f0c48eced2f4efed5e315d5e31cebf.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/7770891b80ba37655a5a370b50173a60.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/ce64dff68b17d2763480e362c143cedd.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/3d2603f5fb0428b3d4cec9a18ca8b540.png)
2、以其中一个空间查询(任选一个使用空间索引查询的函数即可)为例,展示建立GiST索引和不建GiST索引空间查询耗时差异;
①建立GiST索引时:
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/a04f848da5e866cb8a1a40b23359f48c.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/bc529012e0acbca7e34613cac755b791.png)
②不建立GiST索引时:
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/4f8b873080c8b418c70ca8636263d9ee.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/a5cc1ea433ee99f18747c36983923a76.png)
3、将不适用空间索引的函数转换为能够使用空间索引的函数进行查询,并用explain命令查看索引使用前后效率;
列出距离华盛顿特区5000km以内的城市:
转换前:
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/ccf068e3ec275f3572674bdeb5ceb052.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/23fc133bf936cb669f9a388423cb8218.png)
转换后:
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/dd7fb3ffffbcf6cf9cb73292c9a1326f.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/1f93c999cd6c63cabcd7e87d20819d4f.png)
4、对比交叠、包含等空间查询函数在GiST索引和SP-GiST索引上的查询效率。
(1)交叠(Overlaps)
①GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/5dd8d0f4d373463df7546567055d5799.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/9f552943821a698425b024e0366afd0a.png)
②SP-GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/ed26c3e8a75e934ea5dccdbc5f454b31.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/be98bab35c45e7358ed46a99bdb82740.png)
(2)包含于(Within)
①GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/62d32cf5715e0855a082a66c8e575f36.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/500539c41ca9f8c7101639a4e9e98887.png)
②SP-GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/d24420a37e0c9247df3d4c6cc922b317.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/dca8e8948919aee3ec0083322345251f.png)
(3)包含(Contains)
①GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/34a66dffa1acf8024b94f3005fc9b823.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/f158c06f6bf651a9796b9092e5af5750.png)
②SP-GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/ffeefde5ccd8b9505bc3aaac4320b62f.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/e674d104c57716333c054949d0aebc33.png)
(4)相接(Touches)
①GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/167c0e9472016d65ee1f256975633b03.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/91ae57a0c3a4d6f5b5e218bfb4f3d55e.png)
②SP-GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/cf13ba91409b8bd27a433a99fda88408.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/ab4630bf3f9e3e2894b16bc3543ebfb6.png)
(5)穿越(Crosses)
①GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/814983888e9455515f9b0e3636815baf.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/bf1f2ea7f4c67ee9586e364b0a870368.png)
②SP-GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/0a58f9142785fe5186cafcde2a5fbf11.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/9ffd91843d0d24d6dbc5f5deab505a70.png)
(6)相离(Disjoint)
①GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/37873f781246a6309361d9972c574ea3.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/2b5863221dae677ab5147538a8fb2f1a.png)
②SP-GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/4e6d10b161671214d90b7152d7591888.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/5fca951043d34396021406d1f977d0d1.png)
(7)相交(Intersects)
①GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/07d5f49a7387a000d172bae832014dd3.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/83c90f6d3e027bf2ac11b482ab2cc2a3.png)
②SP-GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/1e16347c657b85317bb75ee0024aedb8.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/420e05cb9154112e075724fa7afbc4fa.png)
(8)相等(Equals)
①GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/09f18506226193c95f803e834f978138.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/99284778df2b898348efd6fab981ab04.png)
②SP-GiST索引
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/7aad16d0cec78d0380026f242e650925.png)
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea@master/post-images/e74dc72b8ff8b149f8be2a778d5c46b1.png)
SQL代码
CREATE INDEX northamerica_geom_idx
ON northamerica USING GIST(geom);
VACUUM ANALYZE northamerica(geom);
CREATE INDEX northamerica_cities_geom_idx
ON northamerica_cities USING GIST(geom);
VACUUM ANALYZE northamerica_cities(geom);
CREATE INDEX northamerica_lakes_geom_idx
ON northamerica_lakes USING GIST(geom);
VACUUM ANALYZE northamerica_lakes(geom);
CREATE INDEX northamerica_rivers_geom_idx
ON northamerica_rivers USING GIST(geom);
VACUUM ANALYZE northamerica_rivers(geom);
SELECT C1.cntry_name AS "Neighbors of USA"
FROM northamerica C1,northamerica C2
WHERE ST_Touches(C1.geom, C2.geom) = true
AND C2.cntry_name = 'United States';
SELECT C1.city_name,R1.name
FROM northamerica_cities C1, northamerica_rivers R1
WHERE ST_distance(C1.geom,R1.geom) <
ALL (SELECT ST_distance (C2.geom, R1.geom)
FROM northamerica_cities C2
WHERE C1.city_name<>C2.city_name);
SELECT Ci.city_name
FROM northamerica_cities Ci,northamerica_rivers R
WHERE ST_DWithin(Ci.geom,R.geom,300000/(6371004*PI()/180))
AND R.name='St. Lawrence';
SELECT Ci.city_name
FROM northamerica_cities Ci,northamerica_rivers R
WHERE ST_Contains(ST_Buffer(R.geom,300000/(6371004*PI()/180)),Ci.geom)
AND R.name='St. Lawrence';
SELECT C2.city_name
FROM northamerica_cities C1,northamerica_cities C2
WHERE ST_Distance(C1.geom,C2.geom)<(5000000/(6371004*PI()/180))
AND C1.city_name='Washington D.C.';
SELECT C2.city_name
FROM northamerica_cities C1,northamerica_cities C2
WHERE ST_DWithin(C1.geom,C2.geom,(5000000/(6371004*PI()/180)))
AND C1.city_name='Washington D.C.';
--交叠
SELECT ST_Overlaps(ST_Buffer(R.geom,10),L.geom),R.name,L.name,ST_Buffer(R.geom,2),L.geom
FROM northamerica_rivers R,northamerica_lakes L
WHERE R.gid=15 AND L.gid=12
--含于
SELECT Ci.city_name
FROM northamerica_cities Ci,northamerica_rivers R
WHERE ST_Within(Ci.geom,ST_Buffer(R.geom,300000/(6371004*PI()/180)))
AND R.name='St. Lawrence';
--包含
SELECT Ci.city_name
FROM northamerica_cities Ci,northamerica_rivers R
WHERE ST_Contains(ST_Buffer(R.geom,300000/(6371004*PI()/180)),Ci.geom)
AND R.name='St. Lawrence';
--相接
SELECT ST_Touches(C1.geom,C2.geom)
FROM northamerica C1,northamerica C2
WHERE C1.cntry_name='Argentina' AND C2.cntry_name='Brazil';
--穿越
SELECT R.name,C.cntry_name
FROM northamerica_rivers R,northamerica C
WHERE ST_Crosses(R.geom,C.geom);
--相离
SELECT ST_Disjoint(C1.geom,C2.geom)
FROM northamerica C1,northamerica C2
WHERE C1.cntry_name='United States' AND C2.cntry_name='Greenland';
--相交
SELECT ST_Intersects(R1.geom,R2.geom)
FROM northamerica_rivers R1,northamerica_rivers R2
WHERE R1.name='Mississippi' AND R2.name='Missouri';
--相等
SELECT ST_Equals(R1.geom,R2.geom)
FROM northamerica_rivers R1,northamerica_rivers R2
WHERE R1.name='Mississippi'AND R2.gid=15;
CREATE INDEX northamerica_geom_idx
ON northamerica USING SPGIST(geom);
CREATE INDEX northamerica_cities_geom_idx
ON northamerica_cities USING SPGIST(geom);
CREATE INDEX northamerica_lakes_geom_idx
ON northamerica_lakes USING SPGIST(geom);
CREATE INDEX northamerica_rivers_geom_idx
ON northamerica_rivers USING SPGIST(geom);
VACUUM ANALYZE northamerica(geom);
VACUUM ANALYZE northamerica_cities(geom);
VACUUM ANALYZE northamerica_lakes(geom);
VACUUM ANALYZE northamerica_rivers(geom);
![](https://gcore.jsdelivr.net/gh/manchan4869/Gridea/images/avatar.png)
GISer, a novice who is learning hard
博客内容遵循 署名-非商业性使用-相同方式共享 4.0
国际 ( CC 4.0 BY-SA )
协议
本文永久链接是:
https://blog.manchan.top/post/di-li-kong-jian-shu-ju-ku-san/