注:本文在oracle 19c版本下测试,其他数据库的写法差不多,也可以借鉴这个思路。
测试用表:
--生成测试用表,1000万记录:
create table t10m
as
with t1 as (select /*+ materialize */ * from dba_objects)
select /*+ leading(b) */
rownum as id,a.*
from t1 a,xmltable('1 to 1000') b
where rownumtrunc(sysdate)-365 and status'INVALID';
2.在mv上创建一个联合索引:create index idx_mv_t10m_owner_seq on mv_t10m (owner,owner_seq) ;
物化视图可以每天凌晨刷新一次,把前一天的数据做个排序。 使用这个mv的前提是谓词条件相关字段不会被update。
3.对应的高效写法:3.1select nvl(max(id),0) from mv_t10m where owner='SYS';--返回xxxxx
3.2select a.id,a.owner,a.object_idfrom mv_t10m awhere owner_seq> xxxxx-12345*10-10 and owner_seq