首页 文章

是否真的禁止在快速刷新物化视图中使用非确定性函数?

提问于
浏览
2

如果我创建一个包含 virtual column 的物化视图(快速刷新)并且该列包含一个函数,则会引发一个错误,指出该函数应该是 DETERMINISTIC .

我做了一招!我添加了函数 DETERMINISTIC 关键字(尽管's not) and then created the materialized view successfully. After then I'已从函数中删除了 DETERMINISTIC 关键字,并且物化视图有效 .

可以在虚拟列中使用非确定性函数并在物化视图中使用该虚拟列并快速刷新吗?物化视图是否仍然快速刷新?

2 回答

  • 0

    正如您已经想到的那样,不严格禁止在表格或物化视图中使用非确定性函数 . 错误 ORA-30553: The function is not deterministic 的目的是确保您永远不会创建一个可以在没有Oracle知识的情况下进行更改的数据结构 . 如果Oracle无法跟踪对表的更改,那么许多事情可能会中断,例如索引和完整性约束 .

    下面的示例显示了导致错误结果的更改功能 . 重新创建确定性函数以返回不同的结果违反了 deterministic 的精神 .

    我相信这也部分地回答了你之前的问题,“确定性函数何时使用先前的计算值?”当它不重新验证约束或更新索引时,它会隐式使用该值 .

    --#1: Simple deterministic function that returns 1. 
    create or replace function not_deterministic return varchar2 deterministic is
    begin
        return 'old value';
    end;
    /
    
    --#2: Virtual table that uses the function and has an index on the function.
    drop table deterministic_test;
    create table deterministic_test(a char(100), b as (not_deterministic()) not null);
    insert into deterministic_test(a) select 1 from dual connect by level <= 100000;
    create index deterministic_test_index on deterministic_test(b);
    begin
        dbms_stats.gather_table_stats(user, 'deterministic_test');
    end;
    /
    
    --#3: All the values are 'old value'.
    select distinct b from deterministic_test where a is not null;
    
    b
    -
    old value
    
    --#4: Change the function to return 'new value'.
    create or replace function not_deterministic return varchar2 deterministic is
    begin
        return 'new value';
    end;
    /
    
    --#5: Indexes are not maintained leading to wrong, inconsistent results.
    --Both should return 'new value'.
    select distinct b from deterministic_test;
    
    B
    -
    old value
    
    select /*+full(deterministic_test)*/ distinct b from deterministic_test;
    
    B
    -
    new value
    
  • 2

    我不认为在物化视图中创建虚拟列是有意义的,这与物化视图的目的相矛盾 .

    物化视图用于计算或选择它们需要花费太多时间的 store 值 . 因此,也应存储来自虚拟列的值 .

相关问题