Oracle的SDO_CONTAINS在工会表上没有使用空间索引?

时间:2017-09-15 10:53:50

标签: oracle union spatial


我试图使用Oracle的sdo_contains空间运算符,但看起来,当你在联合表上使用它时,它并没有真正起作用。 以下代码在2分钟内运行,但您必须为每个源表复制空间运算符:

SELECT -- works
   x.code,
   count(x.my_id) cnt
FROM (select
         c.code,
         t.my_id
      from my_poi_table_1 t,my_shape c
      WHERE SDO_contains(c.shape,
                   sdo_geometry(2001,null,SDO_POINT_type(t.latitude, t.longitude,null),null,null)
                   ) = 'TRUE'
      union all
      select
         c.code,
         t.my_id
      from my_poi_table_2 t,my_shape c
      where SDO_contains(c.shape,
                   sdo_geometry(2001,null,SDO_POINT_type(t.lat, t.lng,null),null,null)
                   ) = 'TRUE'
      ) x
group by x.code

我想让它变得简单,所以我尝试先创建点,然后只使用一次sdo_contains,但它运行超过25分钟,因为它没有使用空间指数:

SELECT -- does not work
   c.code,
   count(x.my_id) cnt
FROM my_shape c,
     (select
         my_id,
         sdo_geometry(2001,null,SDO_POINT_type(latitude, longitude,null),null,null) point
      from my_poi_table_1 t
      union all
      select
         my_id2,
         sdo_geometry(2001,null,SDO_POINT_type(lat, lng,null),null,null) point
      from my_poi_table_2 t
      ) x
WHERE SDO_contains(c.shape,
                   x.point
                   ) = 'TRUE'
group by c.code

有没有办法在多个表的结果上使用sdo_contains而不必在选择中多次包含它?
Oracle:12.1.0.2

1 个答案:

答案 0 :(得分:1)

看来,sdo_contains无法(有效地)从子选择中读取:如果我将其中一个poi表放入子选择中,那么Oracle将不会对该部分使用空间索引:

SELECT -- does not work
   x.code,
   count(x.my_id) cnt
FROM (select --+ ordered index(c,INDEX_NAME)
         c.code,
         t.my_id
      from my_shape c,(select t.*,rownum rn from my_poi_table_1 t) t
      WHERE SDO_contains(c.shape,
                   sdo_geometry(2001,null,SDO_POINT_type(t.latitude, t.longitude,null),null,null)
                   ) = 'TRUE'
      union all
      select
         c.code,
         t.my_id
      from my_poi_table_2 t,my_shape c
      where SDO_contains(c.shape,
                   sdo_geometry(2001,null,SDO_POINT_type(t.lat, t.lng,null),null,null)
                   ) = 'TRUE'
      ) x
group by x.code