使用以下数据:
create table my_table (shape sdo_geometry);
begin
insert into my_table (shape) values (sdo_geometry('linestring(100 200, 300 400, 500 600)'));
insert into my_table (shape) values (sdo_geometry('linestring(700 800, 900 1000)'));
end;
/
我可以使用下面的表达式来选择几何体的起始点X坐标:
select
(sdo_util.get_coordinate(shape,1)).sdo_point.x as startpoint_x
from
my_table
STARTPOINT_X
------------
100
700
我想使用相同的表达式创建一个基于函数的索引-不创建自定义函数:
create index idx1 on my_table ((mdsys.sdo_util.get_coordinate(shape,1)).sdo_point.x);
但是我得到一个错误:
Error starting at line : 6 in command -
create index idx1 on my_table ((mdsys.sdo_util.get_coordinate(shape,1)).sdo_point.x)
Error report -
ORA-02327: cannot create index on expression with datatype ADT
02327. 00000 - "cannot create index on expression with datatype %s"
*Cause: An attempt was made to create an index on a non-indexable
expression.
*Action: Change the column datatype or do not create the index on an
expression whose datatype is one of VARRAY, nested table, object,
LOB, or REF.
错误状态,我们不能创建索引的表达式是一个抽象数据类型(ADT)如SDO_GEOEMTRY。
但是表达式的输出数据类型不是抽象的。它是一个数字。我不明白为什么会出现这个错误
Iam可以通过创建自定义函数来解决这个问题:
create or replace function startpoint_x(shape sdo_geometry) return number
deterministic is
begin
return sdo_util.get_coordinate(shape,1).sdo_point.x;
end;
/
并使用函数创建索引:
create index idx1 on my_table
(sys_context('USERENV', 'SESSION_USER')||'.'||startpoint_x(shape));
按预期工作。但我更愿意避免创建自定义函数,因为它只是多了一件需要管理的事情,而且似乎没有必要。此外,我想了解为什么自定义函数工作的根本原因,但在索引中使用表达式不起作用。
,db<的在小提琴
是否有办法在MDSYS.SDO_UTIL.GET_COORDINATE(SHAPE,1)).SDO_POINT.X
上创建基于函数的索引而不创建自定义函数?
您需要使用treat
function将函数返回的对象类型限制为特定的(子)类型:
create index idx1 on my_table (treat( mdsys.sdo_util.get_coordinate(shape,1) as sdo_geometry ).sdo_point.x );
✓
create index idx12 on my_table(shape.sdo_point.x);
✓
db<此处小提琴>此处小提琴>