客户的需求很简单, 两个图层,一个面图层(3),一个点图层(200w记录) 返回的结果也很简单: 返回每个面图层下面所包含的点的ID 返回结果的时间:由于时间太久,不清楚具体需要多长时间
两张图层的定义如下:
SQL> desc testpoint
Name Null? Type
----------------------------------------- -------- ----------------------------
OBJECTID NOT NULL NUMBER(38)
X NUMBER(38,8)
Y NUMBER(38,8)
WKT VARCHAR2(50)
SQL> desc testpolygon
Name Null? Type
----------------------------------------- -------- ----------------------------
OBJECTID NOT NULL NUMBER(38)
SHAPE ST_GEOMETRY
给我的SQL语句如下:
select testpolygon.objectid,testpoint.objectid from testpolygon,testpoint where sde.st_within(sde.st_geomfromtext(testpoint.wkt,4326),testpolygon.shape)=1;
乍一看这个语句写的还挺有水平,但是等到看到sde.st_geomfromtext(testpoint.wkt,4326)这句的时候就知道问题出在哪里了。
使用OGC的WKT存储了点的坐标,然后通过st_geometryfromtext函数转换成st_geometry类型,然后使用st_within函数进行空间运算。
跟
http://blog.csdn.net/liufeng1980423/article/details/51190392 介绍的内容类似。
- 数据库后台的执行计划不会选择走索引,只会走全表扫描。
- 200w个点都要转换一次
- 因此性能肯定非常慢
解决方法:
添加一个类型为ST_GEOMETRY类型的字段,并使用WKT字段对其进行更新并创建空间索引
alter table testpoint add (shape st_geometry);
update testpoint set shape=sde.st_geomfromtext(wkt,4326);
commit;
CREATE INDEX testpoint_idx on testpoint(shape) INDEXTYPE is SDE.ST_SPATIAL_INDEX PARAMETERS ('st_grids=0.5 st_srid=4326');
SQL语句修改成。
select testpolygon.objectid,testpoint.objectid from testpolygon,testpoint where sde.st_within(testpoint.shape,testpolygon.shape)=1;
文章来源:http://blog.csdn.net/liufeng1980423/article/details/51423347