I have a couple of MVs in an Oracle 11g database which full-refresh swiftly when not atomic. A new requirement means that they must be.
I understand that when not-atomic DBMS_MVIEW.REFRESH performs a truncate before a series of inserts but when atomic => true all the records are deleted one by one first. Just deleting the records takes over an hour while the non-atomic version only a few minutes.
I need a way to allow queries to this MV to continue while I refresh it. The FAST refresh method is out of the question because the MV query uses counts and mosts records will have changes anyway.
So, the construct I've come up with is this:
LOCK TABLE FOOBAR_MVIEW_LOCK IN EXCLUSIVE MODE;
SELECT * FROM FOOBAR_MVIEW_DATA1,FOOBAR_MVIEW_DATA2;
COMMIT;
and
LOCK TABLE FOOBAR_MVIEW_LOCK IN EXCLUSIVE MODE;
EXECUTE DBMS_MVIEW.REFRESH( 'FOOBAR_MVIEW_DATA1,FOOBAR_MVIEW_DATA2','cc');
COMMIT;
Is there a better way? NB. My team leader gets nervous when I start talking about partition switching. :-)