在用户定义的聚合函数中进行有序迭代?

时间:2012-02-14 15:39:25

标签: oracle plsql

我刚刚实现了ODCIAggregate接口来创建自定义聚合函数。它工作得非常好而且速度很快,但我希望它能做更多的事情。我的声明是这样的:

SELECT SomeId, myAggregationFunction(Item) FROM
(
  SELECT 
    Foo.SomeId, 
    SomeType(Foo.SomeValue, Foo.SomeOtherValue) AS Item
  FROM
    Foo
  ORDER BY Foo.SomeOrderingValue
)
GROUP BY SomeId;

我的问题是项目没有按照我的内部(有序)SELECT返回它们的顺序传递给我的实现的ODCIAggregateIterate函数。

我已经用Google搜索过,并没有找到任何Oracle提供的方法。你有没有人根据这个要求试验过类似的问题?

谢谢!

2 个答案:

答案 0 :(得分:4)

您是否考虑过使用COLLECT代替数据盒?

至少对于字符串聚合,COLLECT方法更简单,更快。它确实让你的SQL更加怪异。

以下是仅使用简单字符串连接的示例。

--Create a type
create or replace type sometype as object
(
    someValue varchar2(100),
    someOtherValue varchar2(100)
);

--Create a nested table of the type.
--This is where the performance improvement comes from - Oracle can aggregate
--the types in SQL using COLLECT, and then can process all the values at once.
--This significantly reduces the context switches between SQL and PL/SQL, which
--are usually more expensive than the actual work.
create or replace type sometypes as table of sometype;

--Process all the data (it's already been sorted before it gets here)
create or replace function myAggregationFunction(p_sometypes in sometypes)
return varchar2 is
    v_result varchar2(4000);
begin
    --Loop through the nested table, just concatenate everything for testing.
    --Assumes a dense nested table
    for i in 1 .. p_sometypes.count loop
        v_result := v_result || ',' ||
            p_sometypes(i).someValue || '+' || p_sometypes(i).someOtherValue;
    end loop;

    --Remove the first delimeter, return value
    return substr(v_result, 2);
end;
/

--SQL
select someId
    ,myAggregationFunction
    (
        cast
        (
            --Here's where the aggregation and ordering happen
            collect(sometype(SomeValue, SomeOtherValue)
                order by SomeOrderingValue)
            as someTypes
        )
    ) result
from
(
    --Test data: note the unordered SoemOrderingValue.
    select 1 someId, 3 SomeOrderingValue, '3' SomeValue, '3' SomeOtherValue
    from dual union all
    select 1 someId, 1 SomeOrderingValue, '1' SomeValue, '1' SomeOtherValue
    from dual union all
    select 1 someId, 2 SomeOrderingValue, '2' SomeValue, '2' SomeOtherValue
    from dual
) foo
group by someId;

--Here are the results, aggregated and ordered.
SOMEID  RESULT
------  ------
1       1+1,2+2,3+3

答案 1 :(得分:-1)

Oracle很可能会重写您的查询并删除子查询。我从来没有做过你正在做的事情,但是你可以在内部查询中添加NO_UNNEST提示吗?

SELECT SomeId, myAggregationFunction(Item) FROM
(
  SELECT /*+ NO_UNNEST */
    Foo.SomeId, ...

即便如此,我真的不确定它会在子查询中使用ORDER BY做什么。