Query rewrite problem related to FGAC/RLS/VPD

My problem in related to Fine Grained Access Control / DBMS_RLS.
Let's start with a simple example and elaborate further.
Basic problem:
Let's say we have two tables:
create table LEVEL1( L1NR NUMBER not null, TAG NUMBER);
alter table LEVEL1 add constraint LEVEL1_PK primary key (L1NR);
create table LEVEL2( L1NR NUMBER, L2NR NUMBER not null);
alter table LEVEL2 add constraint LEVEL2_PK primary key (L2NR);
alter table LEVEL2 add constraint LEVEL2_FK foreign key (L1NR) references LEVEL1 (L1NR);
I want to convince the database to rewrite a query that looks like this:
A)
SELECT L1.TAG, L2.L2NR
FROM
LEVEL2 L2
INNER JOIN LEVEL1 L1
ON L2.L1NR = L1.L1NR
WHERE
EXISTS( SELECT 1 FROM LEVEL1 L1B WHERE L1B.L1NR = L2.L1NR)
As L2 will be joined to L1 for my human eye and mind
it is obvious that the where clause is redundant,
and the query is equivalent to:
B)
SELECT L1.TAG, L2.L2NR
FROM
LEVEL2 L2
INNER JOIN LEVEL1 L1
ON L2.L1NR = L1.L1NR
Is there any way to convince the parser/optimizer to remove the redundant join?
Why do I have a query like A) and why I cannot rewrite it myself I will explain below.
For those to ask themselves why do I need such a thing let me elaborate.
We develop an application and we want to enforce security on the data at the row level.
The problem is more complicate than my following example, but the example is good enough.
Let's say that we have an application that makes invoices.
We want to give a user the privilege to make reports on the invoices issued for a category of customers.
I see three main ways to enforce row level security on data:
1) At the application level
2) Using views
3) Using Fine Grained Access Control / DBMS_RLS
Choice 1) is more flexible, but it has a major drawback:
You cannot make the database available for reporting with BI/ad-hoc reporting tools.
And also you have to be careful with every query you write. Or create something that takes care of the security, which can be really tricky.
And it also makes report creation for the application difficult since you need to embed the security system into the reporting module.
If you use Crystal Reports or some other reporting tool to build your application report you've got a problem.
Choices 2) and 3) are somewhat similar, but 3) is more flexible
For those who don't know how FGAC/DBMS_RLS works and don't want to dig deeper here is a short explanation:
DBMS_RLS allows the database developer to attach dynamic where clauses to queries.
It does this by rewriting queries like:
"select blabla from employees"
to "select blabla from (select * from employees where <some expression that filters the data according to the user's policy>)"
The filters are attached on a table by table basis, so queries like:
SELECT dept.NAME, empl.NAME
FROM
DEPARTMENT dept
INNER JOIN employees empl
are rewritten to
SELECT dept.NAME, empl.NAME
FROM
(SELECT * FROM DEPARTMENT WHERE <FILTER_DEPT>) dept
INNER JOIN (SELECT * FROM employees WHERE <FILTER_EMP>) empl
So far so good, nothing looks bad, but the devil is in the detail
Back to our reporting problem.
Let's say we have a scenario simpler than life, and I associate the user directly to a customer category by adding a CUST_CAT_ID column to the APP_USER table.
I want restrict the user to see only the data associated to its category (category, customers, invoices, invoice item)
So when he writes "select NAME, ADDRESS from customer" the query is rewritten to
"select NAME, ADDRESS from (SELECT * FROM CUSTOMER WHERE (EXISTS SELECT 1 FROM category join app_user on ... where customer.CUST_CAT_ID = app_user.CUST_CAT_ID and app_user.login = get_current_user()))"
where get_current_user is some framework function that gives me the current user.
A little complicated, but nothing too scary.
But when I write something like:
SELECT <relevant columns> FROM category cat JOIN customer cust JOIN invoice inv JOIN invoice_item item WHERE <some filters>
...(query totally legitimate for a sales report), this gets expanded to a scary query that looks like this:
SELECT <relevant columns>
FROM
(SELECT * FROM category WHERE (EXISTS SELECT 1 FROM app_user u WHERE u.CUST_CAT_ID = CAT_ID AND u.login = get_current_user())) cat
JOIN (SELECT * FROM customer WHERE(EXITS SELECT 1 FROM category JOIN app_user WHERE ...)) cust
JOIN (SELECT * FROM invoice WHERE(EXITS SELECT 1 FROM customer JOIN category JOIN app_user WHERE ...)) inv
JOIN (SELECT * FROM invoice_item WHERE(EXITS SELECT 1 FROM invoice JOIN customer JOIN category JOIN app_user WHERE ...)) item
WHERE
<some filters>
Oops! A query with 4 tables is expanded to a 14 tables query, when all I really need is:
SELECT <relevant columns>
FROM category cat JOIN customer cust JOIN invoice inv JOIN invoice_item item
WHERE <some filters> AND (EXISTS SELECT 1 FROM app_user u WHERE u.CUST_CAT_ID = CAT_ID AND u.login = get_current_user())
Let me tell you that we don't use here roles and privileges tables that we must use in a real life scenario.
In a real life scenario we will easily transform the original query in a 20-30 table join (grrrrrrr).
Well, I cannot change the way DBMS_RLS/FGAC works, and also if I choose to use views I cannot write a filtered view for every possible join that a user might create.
All I want is to find out if there is any way to instruct the parser/optimizer, using primary and foreign keys, optimizer parameters hints and other methods, to rewrite the query and eliminate redundant joins.
Of course I can create some materialized views or bitmap join indexes to help me in the process and speed up the query, but using this method in a database that has hundreds of tables can be a little problem in terms of management and performance.

Hello again,
It appeared that, the problem is present only when I try the query in pl/sql developer.
For some reason, even though I set QUERY_REWRITE_INTEGRITY to STALE_TOLERATED, it behaved as this parameter was set to ENFORCED.
So the case was that:
for session - STALE_TOLERATED
for system - ENFORCED
In v$parameter2 against "QUERY_REWRITE_INTEGRITY" was shown "stale_tolerated", but the query was not rewritten.
When I do the same (altering the session and perform the select query) in SQLPlus, everything works as expected - the query is rewritten.
And I conclude the problem is in PL/SQL Developer (my version is 8.0.1.1498) or something related to this.
Edited by: Verdi on 2010-2-12 14:00

Similar Messages

  • Peoplesoft Enterprise with OLS or VPD/FGAC/RLS

    I know that the EBusiness Suite 11i and 12i have been certified (with Patches) for VPD.
    I can't find information on Certification / Patches / Implementation of Peoplesoft Enterprise (particularly say Peoplesoft Financials Modules) with Oracle Label Security or with VPD/FGAC/RLS.
    Is there such information on metalink3 ? Has anyone implemented/enhanced Peoplesoft in this manner ?

    Well, generally the level security is application embedded and configured, very rarely database level implemented (because mainly only one database user is used). The report should be run through the application query which comes with row level security.
    But it is supported, even Database Vault.
    Nicolas.

  • Oracle Query Rewrite Alias Problem

    Hi,
    The problem desribed below is regarding the use of oracle aggregate awareness through the use of query rewrite.
    2 materialized views exist (one agg per day, one agg per month) and 1 dimension exists for time (date - month - quarter - year)
    Query rewrite seems to be working fine for a query as such (alias sql = alias mv):
    select DDFPC.MONTH_NO
    from BIDATA_MAIN.DF_REPOSITORY_SUBSET DF2, BIDATA_MAIN.DD_TIME DDFPC
    where DF2.FINANCIAL_PROCCESSING_DATE_ID = DDFPC.DATE_ID and DF2.ROUTING_CODE_ISS IN ('FPC','NCC')
    group by DDFPC.MONTH_NO
    However, considering our reporting tool generates aliases on the fly, we could get the following query (alias sql <> alias mv):
    select T2.MONTH_NO
    from BIDATA_MAIN.DF_REPOSITORY_SUBSET DF2, BIDATA_MAIN.DD_TIME T2
    where DF2.FINANCIAL_PROCCESSING_DATE_ID = T2.DATE_ID and DF2.ROUTING_CODE_ISS IN ('FPC','NCC')
    group by T2.MONTH_NO
    --> for the second query, query rewrite refuses to correctly use the dimension – it’ll rewrite to the DD (daily) aggregate, but not to the MM (monthly) aggregate
    Investigation gives: QSM-01072: materialized view, DA_REPOSITORY_MERCHANT_MM, and query have different joins between tables, DF_REPOSITORY_SUBSET and DD_TIME
    So*: How can query rewrite be forced to take place with different aliases between query & materialized view – taken into account a dimension is used for aggregation-awareness?
    This seems to indicate it should work...

    You are missing a join on the file_id column
    i.e.
    select sum(f.BLOCKS), sum(d.BLOCKS) from dba_free_space f ,dba_data_files d where f.TABLESPACE_NAME=d.TABLESPACE_NAME and f.TABLESPACE_NAME like SUBSTR('KU_TSD_1',0,10)
    and f.file_id = d.file_id
    This might have been easier to detect if you had removed the sum aggregation and looked at the data coming back from the raw query.

  • Materialized views - query rewrite -- optional joins & dimensions

    Hi,
    I've implemented a number of materialized views that are accessible via query rewrite.
    Most of these views make use of duplicate tables (DD_TIME table is joined via purchase, load, process, ... date columns)
    I've also created a dimension to make Oracle aware of the time relation (month is child of year...)
    The issue:
    --> If I create the mat. view with inner joins, query rewrite works with the dimensions, but I need to provide ALL joined tables in the query
    --> If I create the mat. view with outer joins, query rewrite works even with 1 joined table, but the dimension seems to be ignored
    How can I create 'optional' joins in the Mat. View while making use of dimensions for rollup?
    (the limitations on materialized view delta joins mentioned on [oracle docs|http://docs.oracle.com/cd/B28359_01/server.111/b28313/qradv.htm#autoId16] kinda has me worried)
    Example Mat. View:
    CREATE MATERIALIZED VIEW DA_REPOSITORY_MERCHANT_MM
    ... ENABLE QUERY REWRITE
    AS SELECT
    DDFPC.YEAR_NO,
    DDFPC.YEAR_MONTH,
    DDPUR.YEAR_NO,
    DDPUR.YEAR_MONTH,
    SUM( DF.TRANSACTION_AMOUNT )
    FROM DF_REP DF,
    DD_TIME DDFPC,
    DD_TIME DDPUR
    WHERE DF.FPC_DTE = DDFPC.DAY_DATE(+)
    AND DF.PUR_DTE = DDPUR.DAY_DATE(+)
    GROUP BY
    DDFPC.YEAR_NO,
    DDFPC.YEAR_MONTH,
    DDPUR.YEAR_NO,
    DDPUR.YEAR_MONTH
    Example query launched:
    SELECT
    DDFPC.YEAR_NO,
    SUM( DF.TRANSACTION_AMOUNT )
    FROM DF_REP DF,
    DD_TIME DDFPC
    WHERE DF.FPC_DTE = DDFPC.DAY_DATE
    GROUP BY
    DDFPC.YEAR_NO

    The problem was resolved by setting foreign keys in the fact table and making them not nullable + using inner joins.
    --> this informs oracle there can be no difference between a query without the dimensions and a query with the dimensions.
    As a result, the materialized view can contain 1 fact linked to 2 dimension tables, but the query rewrite will work for queries on the 1 fact joined with only 1 dimension

  • Query rewrite does not happen

    Hello all,
    let me start by wishing you all the best for the forthcoming year !
    And now, the problem
    I am trying to create a materialized view so that a few slow reports that run on OBIEE (10g) make use of query rewrite functionality. Unfortunately any attempt that had made was unsuccessful. I though of posting the query that is created by OBIEE and the different materialized views that i have created, so that anyone that had a similar problem before might help me through it.  So here it goes ( there is a separate discussion for the other query here ) :
    First comes the query that was created by OBIEE when no materialized view existed.
      SELECT DISTINCT D1.c5 AS c1,                  D1.c10 AS c2,                       D1.c9 AS c3,                  D1.c6 AS c4,                  D1.c8 AS c5,                  D1.c8 / NULLIF (D1.c6, 0) AS c6,
                      D1.c7 AS c7,                  D1.c7 / NULLIF (D1.c6, 0) AS c8,        D1.c4 AS c9,                  D1.c3 / NULLIF (D1.c1, 0) AS c10,                     D1.c2 / NULLIF (D1.c1, 0) AS c11
        FROM (SELECT SUM (D1.c6) OVER (PARTITION BY D1.c5) AS c1,
                     SUM (D1.c7) OVER (PARTITION BY D1.c5) AS c2,
                     SUM (D1.c8) OVER (PARTITION BY D1.c5) AS c3,
                     SUM (D1.c4) OVER (PARTITION BY D1.c5) AS c4,
                     D1.c5 AS c5,                 D1.c6 AS c6,                 D1.c7 AS c7,                 D1.c8 AS c8,                 D1.c9 AS c9,                 D1.c10 AS c10
                FROM (  SELECT COUNT (CASE D1.c12 WHEN 1 THEN D1.c11 ELSE NULL END)    AS c4,                    D1.c5 AS c5,                           SUM (D1.c13) AS c6,            
                             SUM (D1.c14) AS c7,                           SUM (D1.c15) AS c8,               COUNT (DISTINCT D1.c11) AS c9,                           D1.c10 AS c10
                          FROM (SELECT                   /*+ PARALLEL(T16913,8) */
                                      T19403.YEAR_MONTH   AS c5,
                                       T7252.PERCENTAGE AS c10,
                                       T366.BARCODE AS c11,
                                       ROW_NUMBER ()   OVER (  PARTITION BY T19403.YEAR_MONTH,   T366.BARCODE  ORDER BY  T19403.YEAR_MONTH DESC,   T366.BARCODE DESC)  AS c12,
                                       T16913.PHA_QUANTITY AS c13,
                                       T16913.EXEC_COST AS c14,
                                       T16913.EXEC_VALUE AS c15
                                  FROM EXECALENDAR_DIM T19403,
                                           DRUG_DIM T366,
                                           PERCENTAGE_DIM T7252,
                                           PRESCDRUG_FACT T16913
                                 WHERE (T366.DWHKEY = T16913.DRU_DWHKEY
                                        AND T7252.DWHKEY = T16913.PER_DWHKEY
                                        AND T16913.EXECUTION_DATE =
                                               T19403.CALENDAR_DATE
                                        AND T19403.YEAR_MONTH = '201212')) D1
                      GROUP BY D1.c5, D1.c10) D1) D1
    ORDER BY c1, c2
    so my first try was that
    CREATE MATERIALIZED VIEW PRESCDRUG_FACT_MV  BUILD IMMEDIATE  REFRESH ON DEMAND  ENABLE QUERY REWRITE  AS
    SELECT  /*+ PARALLEL(PRESCDRUG_FACT,8) */
                     EXECALENDAR_DIM.YEAR_MONTH       as  YEAR_MONTH
                    ,PRESCDRUG_FACT.PER_DWHKEY
                     ,count(*)                            as COUNT_ALL
                     ,COUNT (EXECALENDAR_DIM.YEAR_MONTH)  AS c_YEAR_MONTH
                     ,COUNT (DISTINCT PRESCDRUG_FACT.DRUG_CD) DRUG_CODE
                     ,COUNT (DISTINCT PRESCDRUG_FACT.DRUG_BARCODE) BARCODE
                      -- quantities
                     ,SUM (PRESCDRUG_FACT.PHA_QUANTITY) AS PHA_QUANTITY
                     ,SUM (PRESCDRUG_FACT.DOC_QUANTITY) AS DOC_QUANTITY
                     ,SUM (PRESCDRUG_FACT.APR_QUANTITY) AS APR_QUANTITY
                     -- values
                     ,SUM (PRESCDRUG_FACT.EXEC_VALUE)       AS EXEC_VALUE
                     ,SUM (PRESCDRUG_FACT.PUB_VALUE)        AS PUB_VALUE                
                     ,SUM (PRESCDRUG_FACT.APR_VALUE) AS APR_VALUE                
                     -- costs
                     ,SUM (PRESCDRUG_FACT.EXEC_COST) AS EXEC_COST
                     ,SUM (PRESCDRUG_FACT.PUB_COST) AS PUB_COST
                     ,SUM (PRESCDRUG_FACT.APR_COST) AS APR_COST
       FROM  EXECALENDAR_DIM,    PRESCDRUG_FACT
      WHERE EXECALENDAR_DIM.CALENDAR_DATE   = PRESCDRUG_FACT.EXECUTION_DATE
           AND EXECALENDAR_DIM.YEAR_MONTH in ( '201212')
       GROUP BY   EXECALENDAR_DIM.YEAR_MONTH ,PRESCDRUG_FACT.PER_DWHKEY
    and i got the following messages from DBMS_MVIEW.EXPLAIN_REWRITE
    QSM-01150: query did not rewrite
    QSM-01102: materialized view, PRESCDRUG_FACT_MV, requires join back to table, EXECALENDAR_DIM, on column, CALENDAR_DATE
    QSM-01102: materialized view, PRESCDRUG_FACT_MV, requires join back to table, PRESCDRUG_FACT, on column, EXECUTION_DATE
    QSM-01082: Joining materialized view, PRESCDRUG_FACT_MV, with table, PRESCDRUG_FACT, not possible
    QSM-01102: materialized view, PRESCDRUG_FACT_MV, requires join back to table, PRESCDRUG_FACT, on column, DRU_DWHKEY
    QSM-01102: materialized view, PRESCDRUG_FACT_MV, requires join back to table, PRESCDRUG_FACT, on column, EXEC_VALUE
    QSM-01102: materialized view, PRESCDRUG_FACT_MV, requires join back to table, PRESCDRUG_FACT, on column, EXEC_COST
    QSM-01102: materialized view, PRESCDRUG_FACT_MV, requires join back to table, PRESCDRUG_FACT, on column, PHA_QUANTITY
    QSM-01155: multi-mv query rewrite not possible when there is a Window Function
    QSM-01219: no suitable materialized view found to rewrite this query
    My second attempt is the following ( in comparison to the first i have added the DRUG_BARCODE column in the grouping clause
    CREATE MATERIALIZED VIEW PRESCDRUG_FACT_MV BUILD IMMEDIATE  REFRESH ON DEMAND ENABLE QUERY REWRITE  AS
    SELECT  /*+ PARALLEL(PRESCDRUG_FACT,8) */
                     EXECALENDAR_DIM.YEAR_MONTH       as  YEAR_MONTH
                     ,PRESCDRUG_FACT.PER_DWHKEY
                  ,PRESCDRUG_FACT.DRUG_BARCODE
                     ,count(*)                                            as COUNT_ALL
                     ,COUNT (EXECALENDAR_DIM.YEAR_MONTH)  AS c_YEAR_MONTH
                     ,COUNT (DISTINCT PRESCDRUG_FACT.DRUG_CD) DRUG_CODE
                     ,COUNT (DISTINCT PRESCDRUG_FACT.DRUG_BARCODE) BARCODE
                     ,COUNT (DISTINCT PRESCDRUG_FACT.PRESC_NUMBER) PRESC_NUMBER
                     ,COUNT (DISTINCT PRESCDRUG_FACT.PRESC_ID)  PRESC_ID
                      -- quantities
                     ,SUM (PRESCDRUG_FACT.PHA_QUANTITY) AS PHA_QUANTITY
                     ,SUM (PRESCDRUG_FACT.DOC_QUANTITY)        AS DOC_QUANTITY
                     ,SUM (PRESCDRUG_FACT.APR_QUANTITY) AS APR_QUANTITY
                     -- values
                     ,SUM (PRESCDRUG_FACT.EXEC_VALUE)       AS EXEC_VALUE
                     ,SUM (PRESCDRUG_FACT.PUB_VALUE)        AS PUB_VALUE                
                     ,SUM (PRESCDRUG_FACT.APR_VALUE) AS APR_VALUE                
                     -- costs
                     ,SUM (PRESCDRUG_FACT.EXEC_COST) AS EXEC_COST
                     ,SUM (PRESCDRUG_FACT.PUB_COST) AS PUB_COST
                     ,SUM (PRESCDRUG_FACT.APR_COST) AS APR_COST
        FROM  EXECALENDAR_DIM,    PRESCDRUG_FACT
      WHERE EXECALENDAR_DIM.CALENDAR_DATE   = PRESCDRUG_FACT.EXECUTION_DATE
           AND EXECALENDAR_DIM.YEAR_MONTH in ( '201212')
       GROUP BY   EXECALENDAR_DIM.YEAR_MONTH ,PRESCDRUG_FACT.PER_DWHKEY,DRUG_BARCODE
    and the results of DBMS_MVIEW.EXPLAIN_REWRITE
    QSM-01150: query did not rewrite
    QSM-01102: materialized view, PRESCDRUG_FACT_MV, requires join back to table, EXECALENDAR_DIM, on column, CALENDAR_DATE
    QSM-01102: materialized view, PRESCDRUG_FACT_MV, requires join back to table, PRESCDRUG_FACT, on column, EXECUTION_DATE
    QSM-01082: Joining materialized view, PRESCDRUG_FACT_MV, with table, PRESCDRUG_FACT, not possible
    QSM-01102: materialized view, PRESCDRUG_FACT_MV, requires join back to table, PRESCDRUG_FACT, on column, DRU_DWHKEY
    QSM-01102: materialized view, PRESCDRUG_FACT_MV, requires join back to table, PRESCDRUG_FACT, on column, EXEC_VALUE
    QSM-01102: materialized view, PRESCDRUG_FACT_MV, requires join back to table, PRESCDRUG_FACT, on column, EXEC_COST
    QSM-01102: materialized view, PRESCDRUG_FACT_MV, requires join back to table, PRESCDRUG_FACT, on column, PHA_QUANTITY
    QSM-01155: multi-mv query rewrite not possible when there is a Window Function
    QSM-01219: no suitable materialized view found to rewrite this query
    third try (rolling up the group results)
    CREATE MATERIALIZED VIEW PRESCDRUG_FACT_MV BUILD IMMEDIATE  REFRESH ON DEMAND ENABLE QUERY REWRITE  AS
    SELECT  /*+ PARALLEL(PRESCDRUG_FACT,8) */
                     EXECALENDAR_DIM.YEAR_MONTH       as  YEAR_MONTH
                     ,PRESCDRUG_FACT.PER_DWHKEY
                  ,PRESCDRUG_FACT.DRUG_BARCODE
                     ,count(*)                                            as COUNT_ALL
                     ,COUNT (EXECALENDAR_DIM.YEAR_MONTH)  AS c_YEAR_MONTH
                     ,COUNT (DISTINCT PRESCDRUG_FACT.DRUG_CD) DRUG_CODE
                     ,COUNT (DISTINCT PRESCDRUG_FACT.DRUG_BARCODE) BARCODE
                     ,COUNT (DISTINCT PRESCDRUG_FACT.PRESC_NUMBER) PRESC_NUMBER
                     ,COUNT (DISTINCT PRESCDRUG_FACT.PRESC_ID)  PRESC_ID
                      -- quantities
                     ,SUM (PRESCDRUG_FACT.PHA_QUANTITY) AS PHA_QUANTITY
                     ,SUM (PRESCDRUG_FACT.DOC_QUANTITY)        AS DOC_QUANTITY
                     ,SUM (PRESCDRUG_FACT.APR_QUANTITY) AS APR_QUANTITY
                     -- values
                     ,SUM (PRESCDRUG_FACT.EXEC_VALUE)       AS EXEC_VALUE
                     ,SUM (PRESCDRUG_FACT.PUB_VALUE)        AS PUB_VALUE                
                     ,SUM (PRESCDRUG_FACT.APR_VALUE) AS APR_VALUE                
                     -- costs
                     ,SUM (PRESCDRUG_FACT.EXEC_COST) AS EXEC_COST
                     ,SUM (PRESCDRUG_FACT.PUB_COST) AS PUB_COST
                     ,SUM (PRESCDRUG_FACT.APR_COST) AS APR_COST
        FROM  EXECALENDAR_DIM,    PRESCDRUG_FACT
      WHERE EXECALENDAR_DIM.CALENDAR_DATE   = PRESCDRUG_FACT.EXECUTION_DATE
           AND EXECALENDAR_DIM.YEAR_MONTH in ( '201212')
       GROUP BY   EXECALENDAR_DIM.YEAR_MONTH     , rollup (PRESCDRUG_FACT.PER_DWHKEY,    DRUG_BARCODE) 
    results from DBMS_MVIEW.EXPLAIN_REWRITE
    QSM-01150: query did not rewrite
    QSM-01295: no suitable grouping_id found in materialized view with grouping sets
    QSM-01155: multi-mv query rewrite not possible when there is a Window Function
    QSM-01219: no suitable materialized view found to rewrite this query  
    So, no luck so far. My guess is that it has to do something with the partition clause that appears in the original query.
    anyway, if anything cross your minds..
    thanks in advance
    Theodore

    Hello again,
    It appeared that, the problem is present only when I try the query in pl/sql developer.
    For some reason, even though I set QUERY_REWRITE_INTEGRITY to STALE_TOLERATED, it behaved as this parameter was set to ENFORCED.
    So the case was that:
    for session - STALE_TOLERATED
    for system - ENFORCED
    In v$parameter2 against "QUERY_REWRITE_INTEGRITY" was shown "stale_tolerated", but the query was not rewritten.
    When I do the same (altering the session and perform the select query) in SQLPlus, everything works as expected - the query is rewritten.
    And I conclude the problem is in PL/SQL Developer (my version is 8.0.1.1498) or something related to this.
    Edited by: Verdi on 2010-2-12 14:00

  • 11g OLAP cube MV's   with Query Rewrite option

    Hi All,
    I am trying to test the 11g OLAP cube MV's with the Query Rewrite option.
    I had created a cube on the schema OLAPTRAIN problem by oracle. I an selected necessary options in 'Materialized Views' tab of the Cube definition in AWM. here is the screenshot
    !http://i40.tinypic.com/9jzpte.png!
    and then I try to run the SQL query
    select SUM(S.QUANTITY) AS QUAN,
    SUM(S.SALES) AS SALES,
    T.CALENDAR_YEAR_NAME,
    P.DEPARTMENT_NAME,
    C.COUNTRY_NAME
    FROM
    TIMES T,CUSTOMERS C,PRODUCTS P, SALES_FACT S
    WHERE
    C.CUSTOMER_KEY = S.CUSTOMER AND
    T.DAY_KEY = S.DAY_KEY AND
    P.ITEM_KEY = S.PRODUCT
    group by T.CALENDAR_YEAR_NAME, P.DEPARTMENT_NAME, C.COUNTRY_NAME;
    and observed the Explain plan, it is not using OLAP cube built, instead it is using the relational tables given in the above sql query.
    Also, i have observed that , though enabling or disabling of the Query Rewrite option doesn't make any change in the Explain query for the above query.
    alter materialized view olaptrain.cb$sales_cube enable query rewrite;
    alter materialized view OLAPTRAIN.cb$sales_cube disable query rewrite;
    No idea why is this Query Rewrite feature is not working on my Database instance of 11g R2 . Do am I missing any steps that has be taken care of , to make this working. Any inputs would be appreciated.
    Thanks
    S

    Hi there,
    You should check out Note 577293.1 on Metalink - 'Oracle OLAP 11g: How to ensure use of Cube Materialized Views/Query Rewrite'
    Thanks,
    Stuart Bunby
    OLAP Blog: http://oracleOLAP.blogspot.com
    OLAP Wiki: http://wiki.oracle.com/page/Oracle+OLAP+Option
    OLAP on OTN: http://www.oracle.com/technology/products/bi/olap/index.html
    DW on OTN : http://www.oracle.com/technology/products/bi/db/11g/index.html

  • No query rewrite

    i have a problem with query rewrite in 10gR1.
    create table time_hierarchy (
    day date primary key,
    mmyyyy char(6) not null,
    qyyyy char(5) not null,
    yyyy char(4) not null
    ) organization index;
    create table tx (
    shopid number not null,
    txdate date references time_hierarchy initially deferred
    not null,
    amount number not null
    create dimension tx_dimension
    level day is time_hierarchy.day
    level mmyyyy is time_hierarchy.mmyyyy
    level qyyyy is time_hierarchy.qyyyy
    level yyyy is time_hierarchy.yyyy
    hierarchy time_rollup (
    day child of
    mmyyyy child of
    qyyyy child of
    yyyy
    create materialized view tx_mv
    build immediate
    refresh on demand
    enable query rewrite
    as
    select a.shopid shopid,
    count(*) txnum,
    sum(a.amount) amountsum,
    b.mmyyyy mmyyyy
    from tx a join time_hierarchy b on a.txdate = b.day
    group by a.shopid, b.mmyyyy;
    i populated the tables and did a complete analyze.
    this query was rewritten:
    select /*+ REWRITE */ a.shopid shopid,
    b.mmyyyy mmyyyy,
    count(*) txnum,
    sum(a.amount) amountsum
    from tx a join time_hierarchy b on a.txdate = b.day
    group by a.shopid, b.mmyyyy
    order by a.shopid, b.mmyyyy;
    but this not:
    select /*+ REWRITE */ a.shopid shopid,
    b.qyyyy qyyyy,
    count(*) txnum,
    sum(a.amount) amountsum
    from tx a join time_hierarchy b on a.txdate = b.day
    group by a.shopid, b.qyyyy
    order by a.shopid, b.qyyyy;
    an explain_mview gave me this;
    MESSAGE
    QSM-01082: Joining materialized view, TX_MV, with table, TIME_HIERARCHY, not pos
    sible
    QSM-01102: materialized view, TX_MV, requires join back to table, TX, on column,
    AMOUNT
    QSM-01102: materialized view, TX_MV, requires join back to table, TX, on column,
    TXDATE
    QSM-01102: materialized view, TX_MV, requires join back to table, TIME_HIERARCHY
    , on column, YYYY
    MESSAGE
    QSM-01102: materialized view, TX_MV, requires join back to table, TIME_HIERARCHY
    , on column, QYYYY
    QSM-01102: materialized view, TX_MV, requires join back to table, TIME_HIERARCHY
    , on column, DAY
    QSM-01086: dimension(s) not present or not used in ENFORCED integrity mode
    QSM-01063: query has a dictionary table or view
    if i set integrity to trusted, the latter query will be rewritten too.
    can anyone explain me why? i'm clueless.
    Best,
    -ap

    yes, see definition of tx, relation tx.txdate->time_hierarchy.day
    what puzzles me is the fact, that mmyyyy works but not qyyyy.
    both are levels of day.
    -j

  • XPath query rewrite and insertChildXML (10g2): Help?

    Hi all,
    I have a registered schema for a <log> document. The schema defines an element /log/logData and under that there, /log/logData/data having maxOccurs="unbounded". So the form of documents is
    <log>
    <logData>
    <data>a</data>
    <data>b</data>
    <data>c</data>
    </logData>
    </log>
    In the schema definition I have enabled "storeArrayAsVArray='true'". The <data> elements are stored in a nested table. Every type is stored object-relationally.
    To an instance documen, I add <data> elements so:
    UPDATE log SET object_value = insertChildXML(object_value, 'log/logData', 'data', '<data>foo</data>') WHERE existsNode ('/log[@uid="foo"]')
    The call succeeds but the problem is that I need to make this call thousands of times. Initiallly the call might take 100 ms, but after a few hundred inserts inserting a single element takes over a second, and the time keeps increasing.
    I believe at least part of the problem is that XPath query rewrites are not working for this call.
    Following Chapter 6 of Oracle XML DB Developer's Guide, to debug the problem I have switched on
    ALTER SESSION SET EVENTS '19021 trace name context forever, level 1';
    and then every invocation causes:
    ORA-19022: XML XPath functions are disabled
    indicating that Oracle could not rewrite the query. Then I set
    ALTER SESSION SET EVENTS '19027 TRACE NAME CONTEXT FOREVER, LEVEL 8192'
    to obtain a trace file. Here is a portion of that trace:
    *** 2005-10-30 16:54:44.984
    *** ACTION NAME:() 2005-10-30 16:54:44.968
    *** MODULE NAME:(SQL*Plus) 2005-10-30 16:54:44.968
    *** SERVICE NAME:(SYS$USERS) 2005-10-30 16:54:44.968
    *** SESSION ID:(137.38364) 2005-10-30 16:54:44.968
    NO REWRITE
         Reason ==> xseq:not optuop
    NO REWRITE
         Reason ==> xseq:not optuop
    NO REWRITE
         Reason ==> not SQLX operand
    NO REWRITE
         Reason ==> non sqlx expression input
    NO REWRITE
         Reason ==> non rewritable sqlx input
    NO REWRITE
    (the rest of the file repeats these lines)
    I have no idea how to interpet that! Can anyone out there offer some insight/assistance?
    Thanks if you can,
    Hugh

    Below is a complete example script. Cannot seem to get rewrite for even this simple case. By that I mean, executing both of the SELECT statements near the end yield the ORA-19022, and that executing the insertChildXML takes progressively longer to execute the more child elements there are.
    Any pointers appreciated!
    Hugh
    set echo on
    ALTER SESSION SET EVENTS '19021 trace name context forever, level 1';
    ALTER SESSION SET EVENTS '19027 TRACE NAME CONTEXT FOREVER, LEVEL 8192';
    exec dbms_xmlschema.deleteSchema('http://hughw.net/foo', dbms_xmlschema.DELETE_CASCADE_FORCE);
    begin
    dbms_xmlschema.registerSchema('http://hughw.net/foo', XMLType('<?xml version="1.0" encoding="UTF-8"?>
    <xs:schema targetNamespace="http://hughw.net/foo" xmlns="http://hughw.net/foo" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"
    xmlns:xdb="http://xmlns.oracle.com/xdb" xdb:storeVarrayAsTable="true">
    <xs:element name="root" xdb:defaultTable="ROOT" >
    <xs:complexType xdb:SQLType="ROOT_T">
    <xs:sequence>
    <!-- could use out of line storage -->
    <!-- commented out
    <xs:element name="child" type="childType" minOccurs="0" maxOccurs="unbounded"
    xdb:SQLName="CHILDREN"
    xdb:SQLInline="false" xdb:defaultTable="CHILD"
    />
    -->
    <!-- use nested table -->
    <xs:element name="child" type="childType" minOccurs="0" maxOccurs="unbounded"
    xdb:SQLName="CHILDREN"
    xdb:SQLCollType="CHILD_V"
    />
    </xs:sequence>
    <xs:attribute name="uid" type="xs:string" use="required"/>
    </xs:complexType>
    </xs:element>
    <xs:complexType name="childType" xdb:SQLType="CHILD_T">
    <xs:simpleContent>
    <xs:extension base="xs:string"/>
    </xs:simpleContent>
    </xs:complexType>
    </xs:schema>'));
    end;
    insert into root values( XMLType('<?xml version="1.0" encoding="UTF-8"?>
    <root uid="1" xmlns="http://hughw.net/foo"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://hughw.net/foo http://hughw.net/foo">
    <child>a</child>
    <child>b</child>
    <child>c</child>
    </root>'));
    CREATE INDEX ROOT_INDEX ON ROOT (extractValue(object_value,'/root/@uid'));
    select insertChildXML(object_value, '/root', 'child', '<child>x</child>>') FROM root;
    explain plan for select insertChildXML(object_value, '/root', 'child', '<child>x</child>>') FROM root;
    SELECT PLAN_TABLE_OUTPUT FROM table(DBMS_XPLAN.display('plan_table', NULL, 'serial'));
    select insertChildXML(object_value, '/root', 'child', '<child>x</child>>') FROM root
    WHERE existsNode(object_value, '/root[@uid="1"]') = 1;
    explain plan for select insertChildXML(object_value, '/root', 'child', '<child>x</child>>') FROM root
    WHERE existsNode(object_value, '/root[@uid="1"]') = 1;
    SELECT PLAN_TABLE_OUTPUT FROM table(DBMS_XPLAN.display('plan_table', NULL, 'serial'));

  • How to handle a phrase like a single word in a query rewrite template?

    Hi there,
    i would like to handle a case like this:
    "hewlett packard" printer
    I have a custom thesaurus where {hewlett packard} is a synonym of {hp}, and viceversa of course.
    Now, i can successfully find records like "hp printer" or "printer model hp 2575 "by issuing the following query:
    select * from sale_items
    where contains(item_name,
    'SYN(hewlett packard,cust_thes) AND printer')>0
    or its equivalent form containing curly braces:
    select * from sale_items
    where contains(item_name,
    'SYN({hewlett packard},cust_thes) AND printer')>0
    My problem is that i can't find a way of successfully passing the phrase "hewlett packard" as a single word in a query rewrite template like this one:
    select * from sale_items
    where contains(item_name,
    '<query>
    <textquery grammar="CONTEXT">{hewlett packard} printer
    <progression>
    <seq><rewrite>transform((TOKENS, "SYN(", ",cust_thes)", " AND "))</rewrite></seq>
    </progression>
    </textquery>
    <score datatype="INTEGER" algorithm="COUNT"/>
    </query>')>0
    When i run this query i get no rows.
    The same happens if i substitute curly braces with double quotes.
    So, how to get phrases to be recognized in the query rewrite template?
    Bye,
    Flavio

    I am unable to find out a way to search on the particular phrase like I have a document containing the text
    “Oracle Text is a good searching tool.” Now if searches for the phrase “searching tool”, it doesn’t return any rows but if I search for “searching” and “tool” in 2 separate queries, it gives the document.
    If any one has done it before, please tell me the solution.
    I have used the following queries to create index on BLOB column:-
    CREATE INDEX doc_ indx ON doctest(document)
    INDEXTYPE IS ctxsys.CONTEXT PARAMETERS('lexer doc_lexer sync (on commit) ');
    select * from doc_test where contains(document, 'searching tool') > 0;
    select * from doc_test where contains(document,'searching') > 0;
    select * from doc_test where contains(document,'tool') > 0;
    Should I specify something while creating the indexes ?
    Regards
    Inderjeet

  • 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 Lukacs

    Hi 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

  • Are Cube organized materialized view with Year to Date calculated measure eligible for Query Rewrite

    Hi,
    Will appreciate if someone can help me with a question regarding Cube organized MV (OLAP).
    Does cube organized materialized view with calculated measures based on time series  Year to date, inception to date  eg.
    SUM(FCT_POSITION.BASE_REALIZED_PNL) OVER (HIERARCHY DIM_CALENDAR.CALENDAR BETWEEN UNBOUNDED PRECEDING AND CURRENT MEMBER WITHIN ANCESTOR AT DIMENSION LEVEL DIM_CALENDAR."YEAR")
    are eligible for query rewrites or these are considered advanced for query rewrite purposes.
    I was hoping to find an example with YTD window function on physical fact dim tables  with optimizer rewriting it to Cube Org. MV but not much success.
    Thanks in advance

    I dont think this is possible.
    (My own reasoning)
    Part of the reason query rewrite works for base measures only (not calc measures in olap like ytd would be) is due to the fact that the data is staged in olap but its lineage is understandable via the olap cube mappings. That dependency/source identification is lost when we build calculated measures in olap and i think its almost impossible for optimizer to understand the finer points relating to an olap calculation defined via olap calculation (olap dml or olap expression) and also match it with the equivalent calculation using relational sql expression. The difficulty may be because both the olap ytd as well as relational ytd defined via sum() over (partition by ... order by ...) have many non-standard variations of the same calculation/definition. E.g: You can choose to use or choose not to use the option relating to IGNORE NULLs within the sql analytic function. OLAP defn may use NASKIP or NASKIP2.
    I tried to search for query rewrite solutions for Inventory stock based calculations (aggregation along time=last value along time) and see if olap cube with cube aggregation option set to "Last non-na hierarchical value" works as an alternative to relational calculation. My experience has been that its not possible. You can do it relationally or you can do it via olap but your application needs to be aware of each and make the appropriate backend sql/call. In such cases, you cannot make olap (aw/cubes/dimensions) appear magically behind the scenes to fulfill the query execution while appearing to work relationally.
    HTH
    Shankar

  • Problem related to variable substitution

    Dear all ,
    I have a query related to variable substitution :
    The requirement is that filename should be in the following format : <b>%<store_code>%_%<Date>%</b>_master.xml .
    I have done the configuration for Store_code in the variable substitution from Payload .
    But I am facing <b>problem related to Date as the requirement is that we should not take the date from the payload</b> . We used "ADDtimestamp" in the file mode creation but it is generating file with time stamp also which doesn;t serve the purpose .
    Can somebody suggest <b>how to take the current date without taking it from payload .</b> <b>Does anyone know something about standard current date which I can assign directly to Var2 .</b>
    Thanks in advance
    Regards
    Prabhat

    Hi,
    In your mapping, have a user defined function which has a bit of Java to get the current date and time and pass back out to a variable.
    Here is a code snippet I got in Google to do this :
    import java.text.*;
    import java.util.*;
    public class Dtest {
    public static void main(String args[]) {
       SimpleDateFormat df = (SimpleDateFormat)
         DateFormat.getDateInstance(DateFormat.SHORT);
       System.out.println("The short date format is  " + df.toPattern());
       Locale loc = Locale.ITALY;
       df = (SimpleDateFormat)
         DateFormat.getDateInstance(DateFormat.SHORT, loc);
       System.out.println("The short date format is  " + df.toPattern());
    Cheers
    Colin.

  • 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 PM

    Bingo!
    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 rewrites with Nested materialized views with different aggregations

    Platform used : Oracle 11g.
    Here is a simple fact table (with measures m1,m2) and dimensions (a) Location (b) Calendar and (c) Product. The business problem is that aggregation operator for measure m1,m2 are different along location dimension and Calendar dimension. The intention is to preaggregate the measures for a product along the calendar dimension and Location dimension and store it as materialized views.
    The direct option is to define a materialized view with Inline queries (Because of the different aggrergation operator, it is not possible to write a query without Inline query). http://download-uk.oracle.com/docs/cd/B28359_01/server.111/b28313/qradv.htm#BABEAJBF documents the limitations that it works only for 'Text match' and 'Equivalent queries' and that is too limiting.
    So decided to have nested materialized view, with first view having just joins(my_dim_mvw_joins), the second view having aggregations along Calendar dimension (my_dim_mvw_calendar) and third view having aggregations along the Location dimension(my_dim_mvw_location). Obviously I do not want the query I fire to know about materialized views and I fire it against the fact table. I see that for the fired query (Which needs aggregations along both Calendar and Location), is rewritten with just second materialized view but not the third. (Had set QUERY_REWRITE_INTEGRITY as TRUSTED) .
    Wanted to know whether there are limitations on Query Writes with nested materialized views? Thanks
    (Have given a simple testable example below. Pls ignore the values given in 'CALENDAR_IDs', 'PRODUCT_IDs' etc as they are the same for all the queries)
    -- Calendar hierarchy table
    CREATE TABLE CALENDAR_HIERARCHY_TREE
    (     "CALENDAR_ID" NUMBER(5,0) NOT NULL ENABLE,
    "HIERARCHY1_ID" NUMBER(5,0),
    "HIERARCHY2_ID" NUMBER(5,0),
    "HIERARCHY3_ID" NUMBER(5,0),
    "HIERARCHY4_ID" NUMBER(5,0),
    CONSTRAINT "CALENDAR_HIERARCHY_TREE_PK" PRIMARY KEY ("CALENDAR_ID")
    -- Location hierarchy table
    CREATE TABLE LOCATION_HIERARCHY_TREE
    (     "LOCATION_ID" NUMBER(3,0) NOT NULL ENABLE,
    "HIERARCHY1_ID" NUMBER(3,0),
    "HIERARCHY2_ID" NUMBER(3,0),
    "HIERARCHY3_ID" NUMBER(3,0),
    "HIERARCHY4_ID" NUMBER(3,0),
    CONSTRAINT "LOCATION_HIERARCHY_TREE_PK" PRIMARY KEY ("LOCATION_ID")
    -- Product hierarchy table
    CREATE TABLE PRODUCT_HIERARCHY_TREE
    (     "PRODUCT_ID" NUMBER(3,0) NOT NULL ENABLE,
         "HIERARCHY1_ID" NUMBER(3,0),
         "HIERARCHY2_ID" NUMBER(3,0),
         "HIERARCHY3_ID" NUMBER(3,0),
         "HIERARCHY4_ID" NUMBER(3,0),
         "HIERARCHY5_ID" NUMBER(3,0),
         "HIERARCHY6_ID" NUMBER(3,0),
         CONSTRAINT "PRODUCT_HIERARCHY_TREE_PK" PRIMARY KEY ("PRODUCT_ID")
    -- Fact table
    CREATE TABLE RETAILER_SALES_TBL
    (     "PRODUCT_ID" NUMBER,
    "PRODUCT_KEY" VARCHAR2(50 BYTE),
    "PLAN_ID" NUMBER,
    "PLAN_PERIOD_ID" NUMBER,
    "PERIOD_ID" NUMBER(5,0),
    "M1" NUMBER,
    "M2" NUMBER,
    "M3" NUMBER,
    "M4" NUMBER,
    "M5" NUMBER,
    "M6" NUMBER,
    "M7" NUMBER,
    "M8" NUMBER,
    "LOCATION_ID" NUMBER(3,0),
    "M9" NUMBER,
    CONSTRAINT "RETAILER_SALES_TBL_LOCATI_FK1" FOREIGN KEY ("LOCATION_ID")
    REFERENCES LOCATION_HIERARCHY_TREE ("LOCATION_ID") ENABLE,
    CONSTRAINT "RETAILER_SALES_TBL_PRODUC_FK1" FOREIGN KEY ("PRODUCT_ID")
    REFERENCES PRODUCT_HIERARCHY_TREE ("PRODUCT_ID") ENABLE,
    CONSTRAINT "RETAILER_SALES_TBL_CALEND_FK1" FOREIGN KEY ("PERIOD_ID")
    REFERENCES CALENDAR_HIERARCHY_TREE ("CALENDAR_ID") ENABLE
    -- Location dimension definition to promote query rewrite
    create DIMENSION LOCATION_DIM
    LEVEL CHAIN IS LOCATION_HIERARCHY_TREE.HIERARCHY1_ID
    LEVEL CONSUMER_SEGMENT IS LOCATION_HIERARCHY_TREE.HIERARCHY3_ID
    LEVEL STORE IS LOCATION_HIERARCHY_TREE.LOCATION_ID
    LEVEL TRADING_AREA IS LOCATION_HIERARCHY_TREE.HIERARCHY2_ID
    HIERARCHY PROD_ROLLUP (
    STORE CHILD OF
    CONSUMER_SEGMENT CHILD OF
    TRADING_AREA CHILD OF
    CHAIN
    -- Calendar dimension definition
    create DIMENSION CALENDAR_DIM
    LEVEL MONTH IS CALENDAR_HIERARCHY_TREE.HIERARCHY3_ID
    LEVEL QUARTER IS CALENDAR_HIERARCHY_TREE.HIERARCHY2_ID
    LEVEL WEEK IS CALENDAR_HIERARCHY_TREE.CALENDAR_ID
    LEVEL YEAR IS CALENDAR_HIERARCHY_TREE.HIERARCHY1_ID
    HIERARCHY CALENDAR_ROLLUP (
    WEEK CHILD OF
    MONTH CHILD OF
    QUARTER CHILD OF
    YEAR
    -- Materialized view with just joins needed for other views
    CREATE MATERIALIZED VIEW my_dim_mvw_joins build immediate refresh complete enable query rewrite as
    select product_id, lht.HIERARCHY1_ID, lht.HIERARCHY2_ID, lht.HIERARCHY3_ID, lht.location_id, cht.HIERARCHY1_ID year,
    cht.HIERARCHY2_ID quarter, cht.HIERARCHY3_ID month, cht.calendar_id week, m1, m3, m7, m9
    from retailer_sales_tbl RS, calendar_hierarchy_tree cht, location_hierarchy_tree lht
    WHERE RS.period_id = cht.CALENDAR_ID
    and RS.location_id = lht.location_id
    and cht.CALENDAR_ID in (10,236,237,238,239,608,609,610,611,612,613,614,615,616,617,618,619,1426,1427,1428,1429,1430,1431,1432,1433,1434,1435,1436,1437,1438,1439,1440,1441,1442,1443,1444,1445,1446,1447,1448,1449,1450,1451,1452,1453,1454,1455,1456,1457,1458,1459,1460,1461,1462,1463,1464,1465,1466,1467,1468,1469,1470,1471,1472,1473,1474,1475,1476,1477)
    AND product_id IN (5, 6, 7, 8, 11, 12, 13, 14, 17, 18, 19, 20)
    AND lht.location_id IN (2, 3, 11, 12, 13, 14, 15, 4, 16, 17, 18, 19, 20)
    -- Materialized view which aggregate along calendar dimension
    CREATE MATERIALIZED VIEW my_dim_mvw_calendar build immediate refresh complete enable query rewrite as
    select product_id, HIERARCHY1_ID , HIERARCHY2_ID , HIERARCHY3_ID ,location_id, year, quarter, month, week,
    sum(m1) m1_total, sum(m3) m3_total, sum(m7) m7_total, sum(m9) m9_total,
    GROUPING_ID(product_id, location_id, year, quarter, month, week) dim_mvw_gid
    from my_dim_mvw_joins
    GROUP BY product_id, HIERARCHY1_ID , HIERARCHY2_ID , HIERARCHY3_ID , location_id,
    rollup (year, quarter, month, week);
    -- Materialized view which aggregate along Location dimension
    CREATE MATERIALIZED VIEW my_dim_mvw_location build immediate refresh complete enable query rewrite as
    select product_id, year, quarter, month, week, HIERARCHY1_ID, HIERARCHY2_ID, HIERARCHY3_ID, location_id,
    sum(m1_total) m1_total_1, sum(m3_total) m3_total_1, sum(m7_total) m7_total_1, sum(m9_total) m9_total_1,
    GROUPING_ID(product_id, HIERARCHY1_ID, HIERARCHY2_ID, HIERARCHY3_ID, location_id, year, quarter, month, week) dim_mvw_gid
    from my_dim_mvw_calendar
    GROUP BY product_id, year, quarter, month, week,
    rollup (HIERARCHY1_ID, HIERARCHY2_ID, HIERARCHY3_ID, location_id)
    -- SQL Query Fired (for simplicity have used SUM as aggregation operator for both, but they will be different)
    select product_id, year, HIERARCHY1_ID, HIERARCHY2_ID,
    sum(m1_total) m1_total_1, sum(m3_total) m3_total_1, sum(m7_total) m7_total_1, sum(m9_total) m9_total_1
    from
    select product_id, HIERARCHY1_ID , HIERARCHY2_ID , year,
    sum(m1) m1_total, sum(m3) m3_total, sum(m7) m7_total, sum(m9) m9_total
    from
    select product_id, lht.HIERARCHY1_ID , lht.HIERARCHY2_ID , lht.HIERARCHY3_ID ,lht.location_id, cht.HIERARCHY1_ID year, cht.HIERARCHY2_ID quarter, cht.HIERARCHY3_ID month, cht.calendar_id week,m1,m3,m7,m9
    from
    retailer_sales_tbl RS, calendar_hierarchy_tree cht, location_hierarchy_tree lht
    WHERE RS.period_id = cht.CALENDAR_ID
    and RS.location_id = lht.location_id
    and cht.CALENDAR_ID in (10,236,237,238,239,608,609,610,611,612,613,614,615,616,617,618,619,1426,1427,1428,1429,1430,1431,1432,1433,1434,1435,1436,1437,1438,1439,1440,1441,1442,1443,1444,1445,1446,1447,1448,1449,1450,1451,1452,1453,1454,1455,1456,1457,1458,1459,1460,1461,1462,1463,1464,1465,1466,1467,1468,1469,1470,1471,1472,1473,1474,1475,1476,1477)
    AND product_id IN (5, 6, 7, 8, 11, 12, 13, 14, 17, 18, 19, 20)
    AND lht.location_id IN (2, 3, 11, 12, 13, 14, 15, 4, 16, 17, 18, 19, 20)
    GROUP BY product_id, HIERARCHY1_ID , HIERARCHY2_ID , HIERARCHY3_ID , location_id, year
    ) sales_time
    GROUP BY product_id, year,HIERARCHY1_ID, HIERARCHY2_ID
    This Query rewrites only with my_dim_mvw_calendar. (as saw in Query Plan and EXPLAIN_MVIEW). But we would like it to use my_dim_mvw_location as that has aggregations for both dimensions.

    blackhole001 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.

  • Materialized views on prebuilt tables - query rewrite

    Hi Everyone,
    I am currently counting on implementing the query rewrite functionality via materialized views to leverage existing aggregated tables.
    Goal*: to use aggregate-awareness for our queries
    How*: by creating views on existing aggregates loaded via ETL (+CREATE MATERIALIZED VIEW xxx on ON PREBUILT TABLE ENABLE QUERY REWRITE+)
    Advantage*: leverage oracle functionalities + render logical model simpler (no aggregates)
    Disadvantage*: existing ETL's need to be written as SQL in view creation statement --> aggregation rule exists twice (once on db, once in ETL)
    Issue*: Certain ETL's are quite complex via lookups, functions, ... --> might create overy complex SQLs in view creation statements
    My question: is there a way around the issue described? (I'm assuming the SQL in the view creation is necessary for oracle to know when an aggregate can be used)
    Best practices & shared experiences are welcome as well of course
    Kind regards,
    Peter

    streefpo wrote:
    I'm still in the process of testing, but the drops should not be necessary.
    Remember: The materialized view is nothing but a definition - the table itself continues to exist as before.
    So as long as the definition doesn't change (added column, changed calculation, ...), the materialized view doesn't need to be re-created. (as the data is not maintained by Oracle)Thanks for reminding me but if you find a documented approach I will be waiting because this was the basis of my argument from the beginning.
    SQL> select * from v$version ;
    BANNER                                                                                                                                                                    
    Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 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 - Production                                                                                                                                    
    SQL> desc employees
    Name                                                                                            Null?    Type
    EMPLOYEE_ID                                                                                     NOT NULL NUMBER(6)
    FIRST_NAME                                                                                               VARCHAR2(20)
    LAST_NAME                                                                                       NOT NULL VARCHAR2(25)
    EMAIL                                                                                           NOT NULL VARCHAR2(25)
    PHONE_NUMBER                                                                                             VARCHAR2(20)
    HIRE_DATE                                                                                       NOT NULL DATE
    JOB_ID                                                                                          NOT NULL VARCHAR2(10)
    SALARY                                                                                                   NUMBER(8,2)
    COMMISSION_PCT                                                                                           NUMBER(2,2)
    MANAGER_ID                                                                                               NUMBER(6)
    DEPARTMENT_ID                                                                                            NUMBER(4)
    SQL> select count(*) from employees ;
      COUNT(*)                                                                                                                                                                
           107                                                                                                                                                                
    SQL> create table mv_table nologging as select department_id, sum(salary) as totalsal from employees group by department_id ;
    Table created.
    SQL> desc mv_table
    Name                                                                                            Null?    Type
    DEPARTMENT_ID                                                                                            NUMBER(4)
    TOTALSAL                                                                                                 NUMBER
    SQL> select count(*) from mv_table ;
      COUNT(*)                                                                                                                                                                
            12                                                                                                                                                                
    SQL> create materialized view mv_table on prebuilt table with reduced precision enable query rewrite as select department_id, sum(salary) as totalsal from employees group by department_id ;
    Materialized view created.
    SQL> select count(*) from mv_table ;
      COUNT(*)                                                                                                                                                                
            12                                                                                                                                                                
    SQL> select object_name, object_type from user_objects where object_name = 'MV_TABLE' ;
    OBJECT_NAME                                                                                                                      OBJECT_TYPE                              
    MV_TABLE                                                                                                                         TABLE                                    
    MV_TABLE                                                                                                                         MATERIALIZED VIEW                        
    SQL> insert into mv_table values (999, 100) ;
    insert into mv_table values (999, 100)
    ERROR at line 1:
    ORA-01732: data manipulation operation not legal on this view
    SQL> update mv_table set totalsal = totalsal * 1.1 where department_id = 10 ;
    update mv_table set totalsal = totalsal * 1.1 where department_id = 10
    ERROR at line 1:
    ORA-01732: data manipulation operation not legal on this view
    SQL> delete from mv_table where totalsal <= 10000 ;
    delete from mv_table where totalsal <= 10000
    ERROR at line 1:
    ORA-01732: data manipulation operation not legal on this view While investigating for this thread I actually made my own question redundant as the answer became gradually clear:
    When using complex ETL's, I just need to make sure the complexity is located in the ETL loading the detailed table, not the aggregate
    I'll try to clarify through an example:
    - A detailed Table DET_SALES exists with Sales per Day, Store & Product
    - An aggregated table AGG_SALES_MM exists with Sales, SalesStore per Month, Store & Product
    - An ETL exists to load AGG_SALES_MM where Sales = SUM(Sales) & SalesStore = (SUM(Sales) Across Store)
    --> i.e. the SalesStore measure will be derived out of a lookup
    - A (Prebuilt) Materialized View will exist with the same column definitions as the ETL
    --> to allow query-rewrite to know when to access the table
    My concern was how to include the SalesStore in the materialized view definition (--> complex SQL!)
    --> I should actually include SalesStore in the DET_SALES table, thus:
    - including the 'Across Store' function in the detailed ETL
    - rendering my Aggregation ETL into a simple GROUP BY
    - rendering my materialized view definition into a simple GROUP BY as wellNot sure how close your example is to your actual problem. Also don't know if you are doing an incremental/complete data load and the data volume.
    But the "SalesStore = (SUM(Sales) Across Store)" can be derived from the aggregated MV using analytical function. One can just create a normal view on top of MV for querying. It is hard to believe that aggregating in detail table during ETL load is the best approach but what do I know?

Maybe you are looking for