Materialized views - schedule, indexes, query rewrite, etc.
I'm trying to get the hang of materialized views and need some help . . .
running Oracle 10gR2
I created a materialized view as follows:
CREATE MATERIALIZED VIEW "MY_SCHEMA"."USERS"
AS
SELECT * FROM "USERS"@PRODUCTION -- to copy the USERS table from my production database to this database
Worked fine. So far so good. Then, I altered it to schedule the refresh:
ALTER MATERIALIZED VIEW "MY_SCHEMA"."USERS" REFRESH FORCE START WITH SYSDATE NEXT TRUNC(SYSDATE + 1) + 4/24
-- to schedule a refresh immediately (SYSDATE) and every morning thereafter at 4:00 am (I think)
then, I wanted to enable query rewrite, so I issued the following command:
ALTER MATERIALIZED VIEW "MY_SCHEMA"."USERS" ENABLE QUERY REWRITE;
and I got the
ALTER MATERIALIZED VIEW succeeded.
message for both of those commands (I'm doing this in SQL Developer).
Then, I edit the materialized view in SQL Developer, click on the 'SQL' tab, and get the following:
CREATE MATERIALIZED VIEW "MY_SCHEMA"."USERS"
ORGANIZATION HEAP PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "TBLSPC_PHIGH"
BUILD IMMEDIATE
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "TBLSPC_PHIGH"
REFRESH FORCE ON DEMAND START WITH sysdate+0 NEXT TRUNC(SYSDATE + 1) + 4/24
WITH PRIMARY KEY USING DEFAULT LOCAL ROLLBACK SEGMENT
DISABLE QUERY REWRITE
AS SELECT "USERS"."User" "User","USERS"."Logon" "Logon","USERS"."Name" "Name","USERS"."Password"
"Password","USERS"."Level" "Level","USERS"."Producer" "Producer","USERS"."Deleted" "Deleted","USERS"."ClaimDoctor"
"ClaimDoctor","USERS"."UserType" "UserType","USERS"."Locked" "Locked","USERS"."Scheduler"
"Scheduler","USERS"."RestrictDays" "RestrictDays","USERS"."First" "First","USERS"."DisableAutoChart"
"DisableAutoChart","USERS"."MaxChartOut" "MaxChartOut","USERS"."MaxHoursOut" "MaxHoursOut","USERS"."EffDate"
"EffDate","USERS"."ExpDate" "ExpDate","USERS"."SwipeLogon" "SwipeLogon","USERS"."SwipePassword"
"SwipePassword","USERS"."PwdLastChanged" "PwdLastChanged","USERS"."Audit" "Audit","USERS"."IsInstructor"
"IsInstructor" FROM "USERS"@PRODUCTION.REGRESS.RDBMS.DEV.US.ORACLE.COM "USERS";
The problems I've encountered so far:
1) I created this two days ago and changed the value of one column in one row in the 'source' USERS table
(USERS@PRODUCTION) as a test case. So far, that change has not propagated over to my materialized view even though
it has had two chances to do so (yesterday morning at 4:00 am and this morning at 4:00 am).
2) I enabled QUERY REWRITE. Why does it still show DISABLE?
3) The primary key of the table came over (as far as I can tell) because it shows in SQL Developer. However, the
indexed columns are no longer indexed. Do I need to recreate those indexes manually and will they persist?
4) As you have guessed, I have just taken the plunge into materialized views, and have not been able to find
anything on the Web along the lines of 'Materialized Views for Dummies'. So, any and all advice/suggestions/help
will be welcome.
Thanks,
Carl
The Refresh would be executed by a job submitted in the background.
Query USER_JOBS (or DBA_JOBS) to see if a job has been submitted and if it has been running (you'd be able to see LAST_DATE, LAST_SEC and NEXT_DATE and NEXT_SEC). \
If the Refresh job has been failing the FAILURES count would be incremented. If there are 16 consecutive failures, the job is marked BROKEN. (if a job fails, Oracle retries it automatically and keeps retrying till it is BROKEN). If the job has been failing you would get messages in the database instance alert log file and trace files.
If the Refresh job has not been running check the parameter value for JOB_QUEUE_PROCESSES -- a value of 0 would not run in jobs in the USER_JOBS/DBA_JOBS view. Ask the DBA to increase set this parameter to at least 1.
Hemant K Chitale
http://hemantoracledba.blogspot.com
Similar Messages
-
Materialized View and Indexing
I have the following Materialized view based on the join of 2 tables merchant and address.
Name Null? Type
FIRST_NAME VARCHAR2(15)
MEMBER_DATE DATE
DBA_NAME VARCHAR2(15)
LEGAL_NAME VARCHAR2(15)
STREET VARCHAR2(50)
CITY VARCHAR2(25)
STATE VARCHAR2(2)
PHONE NUMBER
Then I create the preference as below
BEGIN
CTX_DDL.CREATE_PREFERENCE ('my_pref', 'MULTI_COLUMN_DATASTORE');
CTX_DDL.SET_ATTRIBUTE ('my_pref', 'COLUMNS', 'first_name, member_date, dba_name, legal_name, street, city, state, phone');
END;
When I create an index on the above view, I specify the First_name as the column on which the index is based.
This is fine if I have search queries based on First_name.
But the moment I use the contains query to search on any other column name e.g: Legal_name, Oracle text gives an error saying the column is not indexed.
When we have such a scenario where any column of the view can be searched, how do you create the index? Can we specify multiple columns when creating the index? If not, then how do we solve this problem?Hi,
You still keep the indexed column name in the WHERE clause, but can search for anything in any of the columns. Example using the HR sample schema:
BEGIN
CTX_DDL.CREATE_PREFERENCE ('my_pref', 'MULTI_COLUMN_DATASTORE');
CTX_DDL.SET_ATTRIBUTE ('my_pref', 'COLUMNS', 'FIRST_NAME,LAST_NAME,EMAIL,PHONE_NUMBER,SALARY');
END;
CREATE INDEX EMPLOYEE_IDX ON EMPLOYEES(FIRST_NAME)
INDEXTYPE IS CTXSYS.CONTEXT
PARAMETERS ('DATASTORE MY_PREF');
-- Searching on first name works
SELECT SCORE(1), FIRST_NAME, LAST_NAME, PHONE_NUMBER, SALARY
FROM EMPLOYEES
WHERE CONTAINS(FIRST_NAME, 'JENNIFER', 1) > 0;
SCORE(1) FIRST_NAME LAST_NAME PHONE_NUMBER
SALARY
8 Jennifer Dilly 650.505.2876
3600
8 Jennifer Whalen 515.123.4444
4400
-- Searching for last name, but keeping the indexed column name in the WHERE clause
SELECT SCORE(1), FIRST_NAME, LAST_NAME, PHONE_NUMBER, SALARY
FROM EMPLOYEES
WHERE CONTAINS(FIRST_NAME, 'whalen', 1) > 0;
SCORE(1) FIRST_NAME LAST_NAME PHONE_NUMBER
SALARY
9 Jennifer Whalen 515.123.4444
4400
-- A search for a salary
SELECT SCORE(1), FIRST_NAME, LAST_NAME, PHONE_NUMBER, SALARY
FROM EMPLOYEES
WHERE CONTAINS(FIRST_NAME, '3600', 1) > 0;
SCORE(1) FIRST_NAME LAST_NAME PHONE_NUMBER
SALARY
8 Jennifer Dilly 650.505.2876
3600
8 Renske Ladwig 650.121.1234
3600
Thanks,
Ron -
What rights do I need for materialized views scheduling
Hello,
Can anyone tell me what rights do I need to schedule a materialized views at 1:00 am every night?
To create material views I've use the folowing script:
CREATE MATERIALIZED VIEW MV_view
REFRESH COMPLETE
START WITH sysdate NEXT round(SYSDATE + 1) + 1/24
AS SELECT ... join tabeles... ;.
The problem is that on my test server the MV_view run every night, but on production server my materialized views run only once (when I've run the script above).
I have take a look into the user_refresh and in field NEXT_DATE it's the time of my MV_view creation.
It is possible tha my DBA put some restrictions on my schema? Or do I need some rights to use START WITH and NEXT in my script on production server?
Thanks,
XaMOriginally Posted by wyld
Hi
I find the correct method: I do not only assign reporting rights to htm , I need modify "Inventory report rights"of "Administrator task" in the left of ZCC Console.
If I assign and modify the correct rights, he will create new & run these reports.
wyldkao
Which version you are using? I can't find Inventory Report Rights anywhere? I could only get this working by giving superadmin Please can some explain Thanks -
Oracle materialized view + spatial index
Hello,
i faced with problem when i create spatial index on materialized view.
I create materialized view on 2 table which contains spatial geometrys.
create materialized view mv_geom as
select g.nid, g.geometry
from geom1
union all
select select g.nid, g.geometry
from geom2
i created spatial index - all works fine, but when i try to drop table, i get exception "ORA-04020"...
Materialized view become invalid but don't drop from database.
SQL> drop materialized view mv_geom;
Materialized view dropped
But object does not drop
select * from all_objects z where z.OBJECT_NAME = 'geom1'
and z.OBJECT_TYPE ='MATERIALIZED VIEW'
Any ideas?
Thank's for help!I don't quite follow you. Do you mean that after you dropped mv "mv_geom".
you expect that the base table "geom1" should be dropped? -
ORA-25175 when trying to create materialized view, organization index SOLV
Hi,
I'm trying to create a materialized view to compensate for non-normalized table.
I have table anormal, and mv is to be on distinct agency, company in that table.
Here goes:
SQL> select * from v$version;
BANNER
Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 - 64bi
PL/SQL Release 10.2.0.3.0 - Production
CORE 10.2.0.3.0 Production
TNS for IBM/AIX RISC System/6000: Version 10.2.0.3.0 - Productio
NLSRTL Version 10.2.0.3.0 - Production
SQL> create table anormal (company varchar2(8) not null
2 ,dept varchar2(8) not null
3 ,agency varchar2(8) not null
4 ,constraint anormal_pk primary key (company,dept));
Table created.
SQL> insert into anormal values('c1', 1, 'Ajax');
1 row created.
SQL> insert into anormal values('c1', 2, 'Ajax');
1 row created.
SQL> insert into anormal values('c1', 3, 'Acme');
1 row created.
SQL> insert into anormal values('c2', 1, 'Acme');
1 row created.
SQL> insert into anormal values('c2', 2, 'Acme');
1 row created.
SQL> commit;
Commit complete.
SQL> select * from anormal;
COMPANY DEPT AGENCY
c1 1 Ajax
c1 2 Ajax
c1 3 Acme
c2 1 Acme
c2 2 Acme
SQL> create materialized view agency_comp_mv
2 organization index --primary key (agency, company)
3 build deferred
4 refresh on demand
5 as
6 select distinct agency, company from anormal
7 /
select distinct agency, company from anormal
ERROR at line 6:
ORA-25175: no PRIMARY KEY constraint found
SQL>The error tells me that there is no primary key, but is there any way to add such?
- or is my problem rather that I cannot use DISTINCT with organization index?
Regards
Peter
bump
Message was edited by:
Peter Gjelstrup
<SOLVED>
Message was edited by:
Peter GjelstrupThanks William,
I tried with ON PREBUILT TABLE, just to see what happens.
Building on my OP:
SQL> create table iot(agency varchar2(8) not null, company varchar2(8) not null
2 ,constraint iot_pk primary key (agency,company))
3 organization index;
Table created.
SQL> insert into iot select distinct agency, company from anormal;
4 rows created.
SQL> commit;
Commit complete.
SQL> create materialized view iot on prebuilt table
2 as
3 select distinct agency, company from anormal;
Materialized view created.
SQL> select * from iot;
AGENCY COMPANY
Acme c1
Acme c2
Ajax c1
SQL> insert into anormal values('c3', 2, 'Foo');
insert into anormal values('c3', 2, 'Foo')
1 row created
SQL> select * from anormal;
COMPANY DEPT AGENCY
c1 1 Ajax
c1 2 Ajax
c1 3 Acme
c2 1 Acme
c2 2 Acme
c3 2 Foo
6 rows selected.
SQL> select * from iot;
AGENCY COMPANY
Acme c1
Acme c2
Ajax c1
3 rows selectedNow, try to refresh and MV
SQL> alter materialized view iot refresh complete;
Materialized view altered.
SQL> select object_type, to_char(last_ddl_time, 'hh:mi:ss'), status
2 from user_objects where object_name = 'IOT';
OBJECT_TYPE DDL_TIME STATUS
TABLE 10:40:42 VALID
MATERIALIZED VIEW 10:41:13 VALID
SQL> select * from iot;
AGENCY COMPANY
Acme c1
Acme c2
Ajax c1Agency "Foo" is not in MV,
Looks like that IOT select'ed from is indeed the MV:
SQL> drop table iot;
drop table iot
ERROR at line 1:
ORA-12083: must use DROP MATERIALIZED VIEW to drop "STIK"."IOT"
SQL>So, seems that Maxim is right, it just can't be done. And it seems my options are:
Create HEAP organized MV or
Create and maintain IOT
Regards
Peter -
Spatial vs. materialized views/query rewrite
Dear all,
we are trying to use Spatial (Locator) functionality together with performance optimization using materialized views and query rewrite, and it does not seem to work. Does anybody has experience with this?
The problem in more detail:
* There is a spatial attribut (vom Typ GEOMETRY) in our table;
* we define a materialized view on that table;
* we run a query that could be better answered using the materialized view with query rewrite;
*the optimizer does not choose the plan using the materialized view, query rewrite does not take place;
This happenes, even if neither the materialized view, nor the query contains the spatial attribut.
The explanation given by the procedure DBMS_MVIEW.Explain_Rewrite is:
"QSM-01064 query has a fixed table or view Cause: Query
rewrite is not allowed if query references any fixed tables or views"
We are using Oracle 9R2, Enterprise Edition, with locator. Nevertheless, it would also be interesting, if there is any improvement in 10g?
A more complicated task, using materialized views to optimize spatial operations (e.g., sdo_relate) would also be very interesting, as spatial joins are very expensive operations.
Thanks in advance for any comments, ideas!
Cheers,
Gergely LukacsHi Dan,
thanks for your rapid response!
A simple example is:
alter session set query_rewrite_integrity=trusted;
alter session set query_rewrite_enabled=true;
set serveroutput on;
/* Creating testtable */
CREATE TABLE TESTTABLE (
KEY1 NUMBER (4) NOT NULL,
KEY2 NUMBER (8) NOT NULL,
KEY3 NUMBER (14) NOT NULL,
NAME VARCHAR2 (255),
X NUMBER (9,2),
Y NUMBER (9,2),
ATTR1 VARCHAR2 (2),
ATTR2 VARCHAR2 (30),
ATTR3 VARCHAR2 (80),
ATTR4 NUMBER (7),
ATTR5 NUMBER (4),
ATTR6 NUMBER (5),
ATTR7 VARCHAR2 (40),
ATTR8 VARCHAR2 (40),
CONSTRAINT TESTTABLE_PK
PRIMARY KEY ( KEY1, KEY2, KEY3 ));
/* Creating materialized view */
CREATE MATERIALIZED VIEW TESTTABLE_MV
REFRESH COMPLETE
ENABLE QUERY REWRITE
AS SELECT DISTINCT ATTR7, ATTR8
FROM TESTTABLE;
/* Creating statistics, just to make sure */
execute dbms_stats.gather_table_stats(ownname=> 'TESTSCHEMA', tabname=> 'TESTTABLE', cascade=>TRUE);
execute dbms_stats.gather_table_stats(ownname=> 'TESTSCHEMA', tabname=> 'TESTTABLE_MV', cascade=>TRUE);
/* Explain rewrite procedure */
DECLARE
Rewrite_Array SYS.RewriteArrayType := SYS.RewriteArrayType();
querytxt VARCHAR2(1500) :=
'SELECT COUNT(*) FROM (
SELECT DISTINCT
ATTR8 FROM
TESTTABLE
i NUMBER;
BEGIN
DBMS_MVIEW.Explain_Rewrite(querytxt, 'TESTTABLE_MV', Rewrite_Array);
FOR i IN 1..Rewrite_Array.count
LOOP
DBMS_OUTPUT.PUT_LINE(Rewrite_Array(i).message);
END LOOP;
END;
The message you get is:
QSM-01009 materialized view, string, matched query text
Cause: The query was rewritten using a materialized view, because query text matched the materialized view text.
Action: No action required.
i.e. query rewrite works!
/* Adding geometry column to the testtable -- not to the materialized view, and not to the query! */
ALTER TABLE TESTTABLE
ADD GEOMETRYATTR mdsys.sdo_geometry;
/* Explain rewrite procedure */
DECLARE
Rewrite_Array SYS.RewriteArrayType := SYS.RewriteArrayType();
querytxt VARCHAR2(1500) :=
'SELECT COUNT(*) FROM (
SELECT DISTINCT
ATTR8 FROM
TESTTABLE
i NUMBER;
BEGIN
DBMS_MVIEW.Explain_Rewrite(querytxt, 'TESTTABLE_MV', Rewrite_Array);
FOR i IN 1..Rewrite_Array.count
LOOP
DBMS_OUTPUT.PUT_LINE(Rewrite_Array(i).message);
END LOOP;
END;
The messages you get are:
QSM-01064 query has a fixed table or view
Cause: Query rewrite is not allowed if query references any fixed tables or views.
Action: No action required.
QSM-01019 no suitable materialized view found to rewrite this query
Cause: There doesn't exist any materialized view that can be used to rewrite this query.
Action: Consider creating a new materialized view.
i.e. query rewrite does not work!
If this works, the next issue is to use materialized views for optimizing spatial operations, e.g., a spatial join. I can supply you with an example, if necessary (only makes sense, I think, after the first problem is solved).
Thanks in advance for any ideas, comments!
Cheers,
Gergely -
Help with query rewrite and materialized views
Hello everybody,
I'm currently learning how to use Oracle (10G Enterprise) and in particular, Materialized Views.
I seem to have a problem making the optimizer use a materialized view. I have already set the OPTIMIZER_MODE, QUERY_REWRITE_ENABLED and QUERY_REWRITE_INTEGRITY as needed.
I need to create a materialized view for the following query:
Q1:
SELECT PS_SUPPKEY, PS_PARTKEY, PS_SUPPCOST
FROM PARTSUPPLIER E, PART WHERE PS_PARTKEY=P_PARTKEY and (lower(P_COMMENT) LIKE ''_o_a\%'' or lower(P_COMMENT) LIKE ''_o_u\%'')
and PS_SUPPCOST =
(SELECT min( PS_SUPPCOST)
FROM PARTSUPPLIER I
WHERE E.PS_PARTKEY=I.PS_PARTKEY)'
I created it using the following code:
CREATE MATERIALIZED VIEW mv_q1
ENABLE QUERY REWRITE
AS SELECT PS_SUPPKEY, PS_PARTKEY, PS_SUPPCOST
FROM PARTSUPPLIER E JOIN PART ON (PS_PARTKEY=P_PARTKEY)
WHERE lower(P_COMMENT) LIKE '_o_a%' or lower(P_COMMENT) LIKE '_o_u%'
and PS_SUPPCOST=
(SELECT min( PS_SUPPCOST)
FROM PARTSUPPLIER I
WHERE E.PS_PARTKEY=I.PS_PARTKEY);
I have created the statistics using:
execute dbms_stats.gather_table_stats('frandres',' mv_q1');
execute dbms_stats.gather_table_stats('frandres','PARTSUPPLIER');
execute dbms_stats.gather_table_stats('frandres','PART');
Both partsupplier and part are tables and not views.
When executing Q1, the plan does not use the materialized view. Furthermore, when using explain rewrite:
DECLARE
qrytxt VARCHAR2(3000) := 'SELECT PS_SUPPKEY, PS_PARTKEY, PS_SUPPCOST
FROM PARTSUPPLIER E, PART WHERE PS_PARTKEY=P_PARTKEY and (lower(P_COMMENT) LIKE ''_o_a\%'' or lower(P_COMMENT) LIKE ''_o_u\%'')
and PS_SUPPCOST =
(SELECT min( PS_SUPPCOST)
FROM PARTSUPPLIER I
WHERE E.PS_PARTKEY=I.PS_PARTKEY)';
BEGIN
dbms_mview.EXPLAIN_REWRITE
(qrytxt,'MV_Q1','MV_Q1');
END;
I get the following message:
MESSAGE
QSM-01150: query did not rewrite
QSM-01263: query rewrite not possible when query references a dictionary table o
r view
QSM-01219: no suitable materialized view found to rewrite this query
What I can't understand is why it says I am referencing the dictionary or a view?
If I remove the (lower(P_COMMENT) LIKE ''_o_a\%'' or lower(P_COMMENT) LIKE ''_o_u\%'') condition to the query (using the same materialized view), I get the following message from EXPLAIN_REWRITE:
MESSAGE
QSM-01150: query did not rewrite
QSM-01219: no suitable materialized view found to rewrite this query
Which is reasonable.
I don't know if the like condition is messing up my materialized view. Can anyone please help?
Thanks a lot in advance.
Edited by: user12072111 on Oct 29, 2009 9:43 PMBingo!
The 10.2.0.3 patch set is supposed to fix this ( [List of bugs fixed (MVs)|http://www.dbatools.net/doc/bug10203.html#MVIEW] )
In particular:
5052568 Query rewrite does not work for SQL with LIKE clause.
Thank you very much for your message!
The downside is that I'm only using Oracle for educational purposes and consequently have no Metalink id, so I can't install the patch. Thanks a lot though! -
Query Rewrite with regular database Views
Hi all,
I'm trying to make my programmer's life easier by creating a database view for them to query the data, so they don't have to worry about joining tables. However, query rewrite doesn't work no matter how I define the MV and View. Here's an example:
I've Sales tables with columns: PDate, CustCode, Amount
and Customer table with columns: CustCode, CustDesc
I create a view SALES_V with columns: PDate, CustCode, CustDesc, Amount by joining Sales table with Customer table as follows:
create or replace view SALES_V as
select PDate, c.CustCode, c.CustDesc, Amount
from Sales s
join Customer c on (s.CustCode=c.CustCode);
For the sake of speed, I create a materialized view SALES_TOT_MV with columns: PDate, Amount with the following SQL:
create materialized view SALES_TOT_MV
enable query rewrite
as select PDate, sum(Amount) Amount from Sales
group by PDate;
When I run the following query, I expect it to be rewritten to make use of SALES_TOT_MV:
select PDate, sum(Amount) from SALES_V
group by PDate;
However, explain plan always tell me it's using SALES table, not the SALES_TOT_MV.
Can somebody tell me it's a limitation of Oracle optimizer or I'm just missing something for this?
Thanks in advance!!
- Andrew
Edited by: blackhole001 on Jan 28, 2010 12:34 PMblackhole001 wrote:
Hi all,
I'm trying to make my programmer's life easier by creating a database view for them to query the data, so they don't have to worry about joining tables. This sounds like a pretty horrible idea. I say this because you will eventually end up with programmers that know nothing about your data model and how to properly interact with it.
Additionally, what you will get is a developer that takes one of your views and see's that of the 20 columns in it, it has 4 that he needs. If all those 4 columns comes from a simple 2 table join, but the view has 8 tables, you're wasting a tonne of resources by using the view (and heaven forbid they have to join that view to another view to get 4 of the 20 columns from that other view as well).
Ideally you'd write stored routines that satisfy exactly what is required (if you are the database resource and these other programmers are java, .net, etc... based) and the front end developers would call those routines customized for an exact purpose.
Creating views is not bad, but it's by no means a proper solution to having developers not learn or understand SQL and/or the data model. -
UPDATING the query in materialized view
Hi,
i have a little doubt in Materialized view. i created a materialized view and log with the following query,
create table test_mv (a number ,b number )
alter table test_mv add constraint t_pk primary key ( a );
insert into test_mv values (1,2);
insert into test_mv values(2,2);
insert into test_mv values(3,2);
insert into test_mv values(4,2);
insert into test_mv values(5,2);
commit;
CREATE MATERIALIZED VIEW LOG ON test_mv
WITH SEQUENCE, ROWID
*(a,b)*
INCLUDING NEW VALUES;
CREATE MATERIALIZED VIEW product_sales_mv
ENABLE QUERY REWRITE
AS SELECT b from test_mv;
Now i want to update the query in the MV as 'Select a from test_mv' . for that i tried with
*'ALTER MATERIALIZED VIEW product_sales_mv AS SELECT a from test_mv;'*
But it throwing error,
Error starting at line 5 in command:
alter MATERIALIZED VIEW product_sales_mv AS SELECT b from test_mv
Error report:
SQL Error: ORA-00933: SQL command not properly ended
+00933. 00000 - "SQL command not properly ended"+
*Cause:+
*Action:+
i guess i am doing wrong. kindly help me here. i want to update it without drop and recreate.
thanks,
Jeevanand.KeHi Jeeva,
No. you cannot add or drop columns to the materialized view using the ALTER Statement.
To Change the Structure of the view , drop and re-create the materialized view.
To Alter a materialized view log, You can use the ALTER MATERIALIZED VIEW LOG. By this,
You can add new columns to a materialized view log.
ALTER MATERIALIZED VIEW LOG ON sggi_mrps.emp ADD(deptno);Thanks,
Shankar
Edited by: Shankar Viji on Aug 24, 2012 2:11 AM -
Materialized View Not Picked Up
Hopefully I'm doing something wrong, because so far
Materialized Views are useless. What's happening
is that the MV is only picked up by a select
statement when the select is identical to the MV
query - this means that we'd have to create a
separate MV for each possible query.
Here's an example:
create materialized view
FACT_DIM04_LVL02
enable query rewrite
as
select
d04.DKEY
, d04.LVL02_DESCR
, f.KEY01
, f.KEY03
, f.YEAR_MONTH
, f.SCENARIO
, sum(f.AMOUNT) "AMOUNT"
from FACT1 f
, DIM04 d04
where (f.KEY04 = d04.KEY04)
and (f.ATTR01 = 'O')
and (d04.LVL01_DESCR = '9999')
and (f.SCENARIO IN ('ACT', 'PLAN'))
group by
d04.DKEY
, d04.LVL02_DESCR
, f.KEY01
, f.KEY03
, f.YEAR_MONTH
, f.SCENARIO
analyze table FACT_DIM04_LVL02 compute statistics ;
So far so good. Now let's do an EXPLAIN using the exact
same DDL used to create the MV:
explain plan
set STATEMENT_ID = 'FACT04'
for
select
d04.DKEY
, d04.LVL02_DESCR
, f.KEY01
, f.KEY03
, f.YEAR_MONTH
, f.SCENARIO
, sum(f.AMOUNT) "AMOUNT"
from FACT1 f
, DIM04 d04
where (f.KEY04 = d04.KEY04)
and (f.ATTR01 = 'O')
and (d04.LVL01_DESCR = '9999')
and (f.SCENARIO IN ('ACT', 'PLAN'))
group by
d04.DKEY
, d04.LVL02_DESCR
, f.KEY01
, f.KEY03
, f.YEAR_MONTH
, f.SCENARIO
This gives:
Query Plan
SELECT STATEMENT Cost = 602
TABLE ACCESS FULL FACT_DIM04_LVL02
Great. However, now let's add one tiny little additional
where clause to the query:
explain plan
set STATEMENT_ID = 'FACT04'
for
select /*+ REWRITE */
d04.DKEY
, d04.LVL02_DESCR
, f.KEY01
, f.KEY03
, f.YEAR_MONTH
, f.SCENARIO
, sum(f.AMOUNT) "AMOUNT"
from FACT1 f
, DIM04 d04
where (f.KEY04 = d04.KEY04)
and (f.ATTR01 = 'O')
and (d04.LVL01_DESCR = '9999')
and (f.SCENARIO IN ('ACT', 'PLAN'))
and (1 = 1)
group by
d04.DKEY
, d04.LVL02_DESCR
, f.KEY01
, f.KEY03
, f.YEAR_MONTH
, f.SCENARIO
Notice that I added the simplest predicate possible
(1 = 1), AND I've added a hint telling the optimizer
to use the new MV. However, Oracle does not recognize
the MV; instead it goes back to the base tables:
Query Plan
SELECT STATEMENT Cost = 4130
SORT GROUP BY
HASH JOIN
TABLE ACCESS BY INDEX ROWID DIM04
INDEX RANGE SCAN DIM04_LVL02
TABLE ACCESS FULL FACT_DIM04_LVL02
In other words, this MV only seems to work if you're using
the EXACT query that was used to create the MV, which
makes it almost useless.
I notice that in DBA_MVIEWS, there's a field called
REWRITE_CAPABILITY, and this has a value of TEXTMATCH:
SQLlist
1 select mview_name, rewrite_enabled, rewrite_capability
2* from dba_mviews
SQL/
MVIEW_NAME R REWRITE_C
FACT_DIM04_LVL02 Y TEXTMATCH
TEST01 Y GENERAL
Here's what the manual says about this:
TEXTMATCH: The defining query of the materialized view
contained restrictions on the use of query rewrite.
Yes, I'm using CBO and have set QUERY REWRITE to true.
Also, should it matter, we're on 8.17, Sun Solaris 2.7
Any ideas out there? TIA.What is the value of the job_queue_processes initialization parameter?
Check dba|all|user_jobs to see if a refresh is actually scheduled and not marked broken.
If a job is present, what is the value of the next_date column? -
Oracle Materialized view tables not getting refreshed automatically.
At Destination:--
CREATE MATERIALIZED VIEW PROG_MEDIA
TABLESPACE ONA_TS1
BUILD IMMEDIATE
REFRESH FAST with rowid
START WITH SYSDATE
NEXT SYSDATE+1/96
AS (select /*+ PARALLEL(a,6) */ * from PROG_MEDIA@onair a);
exec dbms_mview.refresh('PROG_MEDIA','C');
At Source:--
CREATE MATERIALIZED VIEW LOG ON ONAIR.PROG_MEDIA
TABLESPACE MVLOG
WITH ROWID;
Fired manually:--
13:00:43 SQL> exec dbms_mview.refresh('PROG_MEDIA','C');
PL/SQL procedure successfully completed.
At source:--
13:18:35 SQL> select PROG_MEDIA_ID,PROG_MEDIA_NAME from PROG_MEDIA where PROG_MEDIA_ID='102226';
PROG_MEDIA_ID
PROG_MEDIA_NAME
102226
test
At Destination:--
14:03:28 SQL> select PROG_MEDIA_ID,PROG_MEDIA_NAME from PROG_MEDIA where PROG_MEDIA_ID='102226';
PROG_MEDIA_ID PROG_MEDIA_NAME
102226 UTSAV-5-162077
JOB LOG_USER THIS_DATE LAST_DATE_TIME NEXT_DATE_TIME INTERVAL FAILURES WHAT
242 ONAIR 17-05-11 14:17:24 SYSDATE+1/96 7 dbms_refresh.refresh('"ONAIR"."PROG_MEDI
A"');
242 ONAIR 17-05-11 14:32:25 SYSDATE+1/96 8 dbms_refresh.refresh('"ONAIR"."PROG_MEDI
A"');
1> I have scheduled this refresh job for every 15mins, but its not getting executed. Also my job_queue_process is '20'. Whenever i execute it manually it get's refresh.
2> I can schedule a cron for the same for every 15mins but i really dont want to do it.
Kindly help me out.Hi,
I have created MV_CAPABILITIES_TABLE & executed below commands:--
17:56:54 SQL> EXECUTE DBMS_MVIEW.EXPLAIN_MVIEW ('"ONAIR"."PROG_MEDIA"');
PL/SQL procedure successfully completed.
17:57:26 SQL> SELECT capability_name, possible, SUBSTR(related_text,1,8)
17:57:27 2 AS rel_text, SUBSTR(msgtxt,1,60) AS msgtxt
17:57:27 3 FROM MV_CAPABILITIES_TABLE
17:57:27 4 ORDER BY seq;
CAPABILITY_NAME P REL_TEXT MSGTXT
PCT N
PCT N
REFRESH_COMPLETE Y
REFRESH_COMPLETE Y
REFRESH_FAST Y
REFRESH_FAST Y
REWRITE N
REWRITE N
PCT_TABLE N PROG_MED relation is not a partitioned table
PCT_TABLE N PROG_MED relation is not a partitioned table
REFRESH_FAST_AFTER_INSERT Y
REFRESH_FAST_AFTER_INSERT Y
REFRESH_FAST_AFTER_ONETAB_DML Y
REFRESH_FAST_AFTER_ONETAB_DML Y
REFRESH_FAST_AFTER_ANY_DML Y
REFRESH_FAST_AFTER_ANY_DML Y
REFRESH_FAST_PCT N PCT is not possible on any of the detail tables in the mater
REFRESH_FAST_PCT N PCT is not possible on any of the detail tables in the mater
REWRITE_FULL_TEXT_MATCH N PROG_MED mv references a remote table or view in the FROM list
REWRITE_FULL_TEXT_MATCH N PROG_MED mv references a remote table or view in the FROM list
REWRITE_FULL_TEXT_MATCH N query rewrite is disabled on the materialized view
REWRITE_FULL_TEXT_MATCH N query rewrite is disabled on the materialized view
REWRITE_PARTIAL_TEXT_MATCH N materialized view cannot support any type of query rewrite
REWRITE_PARTIAL_TEXT_MATCH N materialized view cannot support any type of query rewrite
REWRITE_PARTIAL_TEXT_MATCH N query rewrite is disabled on the materialized view
REWRITE_PARTIAL_TEXT_MATCH N query rewrite is disabled on the materialized view
REWRITE_GENERAL N materialized view cannot support any type of query rewrite
REWRITE_GENERAL N materialized view cannot support any type of query rewrite
REWRITE_GENERAL N query rewrite is disabled on the materialized view
REWRITE_GENERAL N query rewrite is disabled on the materialized view
REWRITE_PCT N general rewrite is not possible or PCT is not possible on an
REWRITE_PCT N general rewrite is not possible or PCT is not possible on an
PCT_TABLE_REWRITE N PROG_MED relation is not a partitioned table
PCT_TABLE_REWRITE N PROG_MED relation is not a partitioned table
34 rows selected.
Elapsed: 00:00:00.09
What to be done next?
Also i fired the job manually by:--
17:38:02 SQL> set serveroutput on
17:39:45 SQL> EXEC DBMS_JOB.RUN(242);
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.48 -
Hi,
i need to create materialized fast refresh view from 3 base tables.I was tried but below error coming,since i informed to my manger not possible.but his not agrees.Could somone help to create materialized view with fast refresh
ORA-12015:cannot create a fast refresh materialized view from a complex query
create materialized view MV_DGN_TEST_SESSION
refresh start with trunc(sysdate)+4/24
next trunc(sysdate+1)+4/24
as
WITH bad_domain_groups as
SELECT DISTINCT tsd.domain_group_id
FROM test_session_detail tsd,
test_session ts ,
test_detail td
WHERE tsd.test_session_id = ts.test_session_id
AND tsd.test_detail_id =td.test_detail_id
AND ts.test_session_status_id = 4
AND td.test_type_id =8
AND tsd.test_session_status_id <> 3
AND tsd.domain_group_id IS NOT NULL
select a.test_session_id,b.test_session_detail_id,b.test_detail_id,a.completion_date,a.institution_id,
b.score,b.domain_group_id
FROM test_session a ,
test_session_detail b,
test_detail c
, bad_domain_groups bad
WHERE a.test_session_id =b.test_session_id
AND a.test_session_status_id IN (3,4)
AND b.test_detail_id =c.test_detail_id
AND c.test_type_id IN (8)
AND b.domain_group_id = bad.domain_group_id (+)
AND bad.domain_group_id IS NULL
AND b.domain_group_id is not null; Edited by: user575115 on Nov 18, 2009 11:17 PMDocs say:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_6002.htm#i2106785 :
"Restrictions on the Defining Query of a Materialized View The materialized view query is subject to the following restrictions:
* The defining query of a materialized view can select from tables, views, or materialized views owned by the user SYS, but you cannot enable QUERY REWRITE on such a materialized view.
* You cannot define a materialized view with a subquery in the select list of the defining query. You can, however, include subqueries elsewhere in the defining query, such as in the WHERE clause.
* Materialized join views and materialized aggregate views with a GROUP BY clause cannot select from an index-organized table.
* Materialized views cannot contain columns of datatype LONG.
* You cannot create a materialized view log on a temporary table. Therefore, if the defining query references a temporary table, then this materialized view will not be eligible for FAST refresh, nor can you specify the QUERY REWRITE clause in this statement.
* If the FROM clause of the defining query references another materialized view, then you must always refresh the materialized view referenced in the defining query before refreshing the materialized view you are creating in this statement.
If you are creating a materialized view enabled for query rewrite, then:
* The defining query cannot contain, either directly or through a view, references to ROWNUM, USER, SYSDATE, remote tables, sequences, or PL/SQL functions that write or read database or package state.
* Neither the materialized view nor the master tables of the materialized view can be remote.
If you want the materialized view to be eligible for fast refresh using a materialized view log, then some additional restrictions may apply."
Try to reconstruct the query without the WITH-clause. -
Dear All,
I have created a materialized view which refreshes on commit.materialized view is enabled query rewrite.I have created a materialized view log on the base table also While inserting into the base table it takes lot of time................Can u please tell me why?Dear Rahul,
Here is my materialized view..........
create materialized view mv_test on prebuilt table refresh force on commit
enable query rewrite as
SELECT P.PID,
SUM(HH_REGD) AS HH_REGD,
SUM(INPRO_WORKS) AS INPRO_WORKS,
SUM(COMP_WORKS) AS COMP_WORKS,
SUM(SKILL_WAGE) AS SKILL_WAGE,
SUM(UN_SKILL_WAGE) AS UN_SKILL_WAGE,
SUM(WAGE_ADVANCE) AS WAGE_ADVANCE,
SUM(MAT_AMT) AS MAT_AMT,
SUM(DAYS) AS DAYS,
P.INYYYYMM,P.FIN_YEAR
FROM PROG_MONTHLY P
WHERE SUBSTR(PID,5,2)<>'PP'
GROUP BY PID,P.INYYYYMM,P.FIN_YEAR;
Please help me if query enable rewrite does any performance degradation......
Thanks & Regards
Kris -
Query Rewrite ISSUE (ANSI JOINS do not work, traditional join works ) 11gR2
For some types of queries constructed with ANSI JOINS, materialized views are not being used.
This is currently increasing time on various reports since we cannot control the way the queries are generated(Tableau Application generates and runs queries against the STAR Schema).
Have tried to debug this behavior using DBMS_MVIEW.EXPLAIN_REWRITE and mv_capabilities function without any success.
The database is configured for query rewrite: REWRITE INTEGRITY, QUERY REWRITE ENABLED and other settings are in place.
Have successfully reproduced the issue using SH Sample schema:
Q1 and Q2 are logically identical the only difference between them being the type of join used:
Q1: ANSI JOIN
Q2: Traditional join
Below is an example that can be validated on SH sample schema.
Any help on this will be highly appreciated.
-- Q1: the query is generated by an app and needs to be rewritten with materialized view
SELECT cntr.country_subregion, cust.cust_year_of_birth, COUNT(DISTINCT cust.cust_first_name)
FROM customers cust
INNER JOIN countries cntr
ON cust.country_id = cntr.country_id
GROUP BY cntr.country_subregion, cust_year_of_birth;
-- Q2: the query with traditional join is rewritten with materialized view
SELECT cntr.country_subregion, cust.cust_year_of_birth, COUNT(DISTINCT cust.cust_first_name)
FROM customers cust
INNER JOIN countries cntr
ON cust.country_id = cntr.country_id
GROUP BY cntr.country_subregion, cust_year_of_birth;Tested both queries with the following materialized views:
CREATE MATERIALIZED VIEW MVIEW_TEST_1
ENABLE QUERY REWRITE
AS
SELECT cntr.country_subregion, cust.cust_year_of_birth, COUNT(DISTINCT cust.cust_first_name)
FROM customers cust
INNER JOIN countries cntr
ON cust.country_id = cntr.country_id
GROUP BY cntr.country_subregion, cust_year_of_birth;
CREATE MATERIALIZED VIEW MVIEW_TEST_2
ENABLE QUERY REWRITE
AS
SELECT cntr.country_subregion, cust.cust_year_of_birth, COUNT(DISTINCT cust.cust_first_name)
FROM customers cust, countries cntr
WHERE cust.country_id = cntr.country_id
GROUP BY cntr.country_subregion, cust_year_of_birth;Explain Plans showing that Q1 does not use materialized view and Q2 uses materialized view
SET AUTOTRACE TRACEONLY
--Q1 does not use MVIEW_TEST_1
SQL> SELECT cntr.country_subregion, cust.cust_year_of_birth, COUNT(DISTINCT cust.cust_first_name)
FROM customers cust
INNER JOIN countries cntr
ON cust.country_id = cntr.country_id
GROUP BY cntr.country_subregion, cust_year_of_birth; 2 3 4 5
511 rows selected.
Execution Plan
Plan hash value: 1218164197
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 425 | 12325 | | 916 (1)| 00:00:11 |
| 1 | HASH GROUP BY | | 425 | 12325 | | 916 (1)| 00:00:11 |
| 2 | VIEW | VM_NWVW_1 | 55500 | 1571K| | 916 (1)| 00:00:11 |
| 3 | HASH GROUP BY | | 55500 | 1842K| 2408K| 916 (1)| 00:00:11 |
|* 4 | HASH JOIN | | 55500 | 1842K| | 409 (1)| 00:00:05 |
| 5 | TABLE ACCESS FULL| COUNTRIES | 23 | 414 | | 3 (0)| 00:00:01 |
| 6 | TABLE ACCESS FULL| CUSTOMERS | 55500 | 867K| | 405 (1)| 00:00:05 |
--Q2 uses MVIEW_TEST_2
SQL> SELECT cntr.country_subregion, cust.cust_year_of_birth, COUNT(DISTINCT cust.cust_first_name)
FROM customers cust, countries cntr
WHERE cust.country_id = cntr.country_id
GROUP BY cntr.country_subregion, cust_year_of_birth; 2 3 4
511 rows selected.
Execution Plan
Plan hash value: 2126022771
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
| 0 | SELECT STATEMENT | | 511 | 21973 | 3 (0)| 00:00:01 |
| 1 | MAT_VIEW REWRITE ACCESS FULL| MVIEW_TEST_2 | 511 | 21973 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------------------------Database version 11gR1 (Tested also on 11gR2)
SQL> select * from v$version;
BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - ProductionThanks for the formatting tips.
Just found an Oracle Bug which explains the above behavior.
Unfortunately the bug will be fixed only in 12.1 Release so as a workaround will try to use traditional joins.
For those who have metalink access see [Bug 10145667 : ERRORS TRYING TO REWRITE QUERY WITH EXACT TEXT MATCH TO MVIEW] -
Query Rewrite (QSM-01263) and Views in Execution Plan
Hello!
I created a query rewrite enabled materialized view from a query, which contains only tables (no views). Query rewrite didn't work, so i checked the query with dbms_mview.explan_rewrite, which told my that my query contains references to views or dictionary tables. I checked my query again, but there are only tables, no views, no dictionary tables.
When I look in the execution plan of my query I see that the query optimizer generates views, I guess from my subquery (?). "A view definition was processed, either from a stored view...or as defined by steps...".
I suppose that's the reason why my query rewrite doesn't work. All my other mat views are working fine, so the usual parameters (query_rewrite_enabled, integrity, etc.) are set correctly.
Do you have any ideas how to get my query rewrite enabled work?
Thanks!Modifying the query (potentially with hints) so that Oracle doesn't do the view transformation would be one option.
Maybe you are looking for
-
Report in Payment Wizard - Non Included Transaction Report
Hi Guys, Within the Payment Wizard is a report called " Non Included Transaction Report", later in the wizard also referred to as "Error Log Report (System)". I would like to recreate this report so that I can add some additional fields on the Purcha
-
MacBook pro 8,2 late-2011 after series of kernel panics and video glitches stopped booting
Hello, friends... I am shattered, my MacBook Pro stopped booting today. It all started two days ago, when I came to India and got USB modem dongle from Tata Docomo. It was making Lion kernel panic whenever it was connected to Internet, but had no eff
-
How can I tell if my computer is using ethernet or wireless for downloads?
Hi, My computer is hooked up via ethernet to my router (base station) which is connected to my DSL modem, and I also have my Airport on because I have extended my network with an airport express so I can stream music to my stereo across the room usin
-
Automatic credit release of blocked Sales orders with pre payment
Hi , We have a requirement as given below.Please suggest a better solution for this. The customers have the 'prepayment ' payment terms with different percentages of the sales order value to be paid before the delivery creation. When the sales orders
-
Getting music from your iPod onto your hard drive?
Hey, I am trying to transfer some music files from my iPod on to my hard drive. I have tried dragging them on to the hard drive and I went through all the options and configurations and yet I can't seem to figure it out. Can someone please help me ou