【问题标题】:oracle sql clarification to use case condition in where clause, error sdo_nn cannot be evaluated without an indexoracle sql 澄清在 where 子句中使用 case 条件,错误 sdo_nn 无法在没有索引的情况下进行评估
【发布时间】:2019-09-07 10:00:40
【问题描述】:

我正在使用带有 oracle 空间的 oracle 11g,如果我在 where 条件中使用如下距离,则此查询有效

mdsys.sdo_nn ( b1.location, l_location , 'SDO_BATCH_SIZE=100 distance='|| 
c1.tier_radius||' unit=KM', 1) = 'TRUE'

但是我希望根据条件评估距离。下面的查询给出了错误 sdo_nn 无法在没有索引的情况下进行评估,我希望根据类似这样的条件评估距离

select
        /*+ first_rows LEADING(c) USE_NL(c1 b1) INDEX(b1 BRANCH_LOCATION_SPIX)*/
        b1.BRANCH_ID,
        mdsys.sdo_nn_distance(1) branchDistance
      from
        branch b1, city c1
      where
        mdsys.sdo_nn ( b1.location, l_location , 'SDO_BATCH_SIZE=100 distance='|| (case  when b1.restaurant_id not in (3970,3971,3972) then c1.tier_radius
        else 15 end) ||' unit=KM', 1) = 'TRUE'   
        and b1.city_id = c1.city_id  

【问题讨论】:

  • 查询是否适用于此? mdsys.sdo_nn ( b1.location, l_location , 'SDO_BATCH_SIZE=100 distance=15 unit=KM', 1) = 'TRUE'?

标签: sql oracle


【解决方案1】:

我们知道那部分

mdsys.sdo_nn ( b1.location, l_location , 'SDO_BATCH_SIZE=100 distance='|| 
c1.tier_radius||' unit=KM', 1) = 'TRUE'

有效,那么我认为您可以使用包含union all 的查询,如下所示:

select  /*+ first_rows LEADING(c) USE_NL(c1 b1) INDEX(b1 BRANCH_LOCATION_SPIX)*/
        b1.BRANCH_ID,
        mdsys.sdo_nn_distance(1) branchDistance
  from branch b1 
  join city c1 on c1.city_id = b1.city_id
 where mdsys.sdo_nn ( b1.location, l_location , 'SDO_BATCH_SIZE=100 distance='||c1.tier_radius||' unit=KM', 1) = 'TRUE'
   and b1.restaurant_id not in (3970,3971,3972)
union all
select  /*+ first_rows LEADING(c) USE_NL(c1 b1) INDEX(b1 BRANCH_LOCATION_SPIX)*/
        b1.BRANCH_ID,
        mdsys.sdo_nn_distance(1) branchDistance
  from branch b1 
  join city c1 on c1.city_id = b1.city_id
 where mdsys.sdo_nn ( b1.location, l_location , 'SDO_BATCH_SIZE=100 distance=15 unit=KM', 1) = 'TRUE'
   and b1.restaurant_id in (3970,3971,3972)

【讨论】:

  • 是联合所有工作,但不会有性能问题,因为联合执行两个查询而不是一个?
  • @Ayub 我从未遇到过与union all 相关的任何性能问题(但union 可能会导致性能问题)。
  • 我觉得这与其中一个示例中提到的问题有关,其中提到错误 SDO_NN 无法在不使用索引 spatialdbadvisor.com/oracle_spatial_tips_tricks/73/… 的情况下进行评估
猜你喜欢
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 2023-04-09
  • 2021-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多