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

Similar Messages

  • DAX - Year to Date Calculation

    Hi All,
    We have a requirement of calculating the Year to Date Calculation in DAX for No. of Sales. Below is the table that we are using:
    Year
    Quarter
    Date
    No. of Sales
     If we browse the tabular model with Year/Quarter then formula should calculate the value at quarter levels.
    Formula Definition for Q1 = (Total of “No. of Sales” in current Year upto Q1  / Total of “No. of Sales” in previous Year upto Q1) -1
    Formula Definition for Q2 = (Total of “No. of Sales” in current Year upto Q2  / Total of “No. of Sales” in
    previous Year upto Q2) -1
    Formula Definition for Q3 = (Total of “No. of Sales” in current Year upto Q3  / Total of “No. of Sales” in
    previous Year upto Q3) -1
    Formula Definition for Q4 = (Total of “No. of Sales” in current Year upto Q4  / Total of “No. of Sales” in
    previous Year upto Q4) -1
    Can anybody help us in building this formula in DAX which should be able to browse with any columns of the table.
    Thanks,
    Vishal Jharwade

    Hi Vishal,
    According to your description, we are still not clear about your requirement. Do you want to calculate the "current YTD/previous YTD -1"? Or you want to calculate the Total Year to Quarter for each quarter?
    If you want to calculate the first one, please use the formula below:
    =[Sales](DATESYTD(DimDate[Datekey])) - [Sales](DATEADD(DATESYTD(DimDate[Datekey]),-1,YEAR))
    If you want to calculate the second one, you can use TotalYTD() function to calculate the Year to Quarter value:
    =TOTALYTD(SUM(InternetSales_USD[SalesAmount_USD]),DateTime[DateKey], ALL(‘DateTime’), “3/31”)
    However, since we have to specify a literal string with a date that defines the year-end date, it can't make it dynamically to show the Year to Quarter for the current quarter. So you still can't use only one formula to deal with all quarters.
    Reference:
    First steps in DAX: Year-To-Date
    Time Intelligence Functions in DAX
    Best Regards,
    Simon Hou
    TechNet Community Support

  • 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 View with "error in exporting/importing data"

    My system is a 10g R2 on AIX (dev). When I impdp a dmp from other box, also 10g R2, in the dump log file, there is an error about the materialized view:ORA-31693: Table data object "BAANDB"."MV_EMPHORA" failed to load/unload and is being skipped due to error:
    ORA-02354: error in exporting/importing data
    Desc mv_emphora
    Name              Null?    Type
    C_RID                      ROWID
    P_RID                      ROWID
    T$CWOC            NOT NULL CHAR(6)
    T$EMNO            NOT NULL CHAR(6)
    T$NAMA            NOT NULL CHAR(35)
    T$EDTE            NOT NULL DATE
    T$PERI                     NUMBER
    T$QUAN                     NUMBER
    T$YEAR                     NUMBER
    T$RGDT                     DATEAs i ckecked here and Metalink, I found the info is less to do with the MV? what was the cause?

    The total lines are 25074. So I used the GREP from the OS to get the lines involved with the MV. Here are:
    grep -n -i "TTPPPC235201" impBaanFull.log
    5220:ORA-39153: Table "BAANDB"."TTPPPC235201" exists and has been truncated. Data will be loaded but all dependent metadata will be skipped due to table_exists_action of truncate
    5845:ORA-39153: Table "BAANDB"."MLOG$_TTPPPC235201" exists and has been truncated. Data will be loaded but all dependent meta data will be skipped due to table_exists_action of truncate
    8503:. . imported "BAANDB"."TTPPPC235201"                     36.22 MB  107912 rows
    8910:. . imported "BAANDB"."MLOG$_TTPPPC235201"               413.0 KB    6848 rows
    grep -n -i "TTCCOM001201" impBaanFull.log
    4018:ORA-39153: Table "BAANDB"."TTCCOM001201" exists and has been truncated. Data will be loaded but all dependent metadata will be skipped due to table_exists_action of truncate
    5844:ORA-39153: Table "BAANDB"."MLOG$_TTCCOM001201" exists and has been truncated. Data will be loaded but all dependent metadata will be skipped due to table_exists_action of truncate
    9129:. . imported "BAANDB"."MLOG$_TTCCOM001201"               9.718 KB      38 rows
    9136:. . imported "BAANDB"."TTCCOM001201"                     85.91 KB     239 rows
    grep -n -i "MV_EMPHORA" impBaanFull.log
    8469:ORA-39153: Table "BAANDB"."MV_EMPHORA" exists and has been truncated. Data will be loaded but all dependent metadata will be skipped due to table_exists_action of truncate
    8558:ORA-31693: Table data object "BAANDB"."MV_EMPHORA" failed to load/unload and is being skipped due to error:
    8560:ORA-12081: update operation not allowed on table "BAANDB"."MV_EMPHORA"
    25066:ORA-31684: Object type MATERIALIZED_VIEW:"BAANDB"."MV_EMPHORA" already exists
    25072: BEGIN dbms_refresh.make('"BAANDB"."MV_EMPHORA"',list=>null,next_date=>null,interval=>null,implicit_destroy=>TRUE,lax=>
    FALSE,job=>44,rollback_seg=>NULL,push_deferred_rpc=>TRUE,refresh_after_errors=>FALSE,purge_option => 1,parallelism => 0,heap_size => 0);
    25073:dbms_refresh.add(name=>'"BAANDB"."MV_EMPHORA"',list=>'"BAANDB"."MV_EMPHORA"',siteid=>0,export_db=>'BAAN'); END;the number in front of each line is the line number of the import log.
    Here is my syntax of import pmup:impdp user/pw  SCHEMAS=baandb DIRECTORY=baanbk_data_pump DUMPFILE=impBaanAll.dmp  LOGFILE=impBaanAll.log TABLE_EXISTS_ACTION=TRUNCATEYes I can create the MV manually and I have no problem to refresh manually it after the inmport.

  • Very high parse times for query rewrite using cube materialized views

    We recently upgraded to version 11.2.0.2 (both AWM and Oracle database server). We are using cube materialized views with query rewrite enabled. Some observations of changes that took place when we rebuilt all the dimensions and cubes in this version:
    1. Queries against the base tables take about 35 seconds to parse. Then they execute in a tenth of a second. Even simple queries that just get a sum of the amount from the fact table (which is joined to all the dimensions) takes that long to parse. Once parsed, the queries fly.
    2. I noticed that the materialized views used to use grouping sets in the group by clause in version 11.2.0.1, but now they use group by rollup, rollup, rollup...
    If we disable query rewrite on the MV or for my session, parse times drop to less than a second. Ideas?

    There does appear to be a slow down in parse times between 11.1.0.7 and 11.2. We are still investigating this, but in the meantime here is a way to force the code in 11.2 to generate a GROUPING SETS clause instead of the new ROLLUP syntax.
    The trick is to create a dummy hierarchy containing only the leaf level. This is necessary for all dimensions that currently have a single hierarchy. As a simple example I created a dimension, PROD, with three levels, A, B, and C, in a single hierarchy. I then created a one dimensional cube, PC. Here is the SELECT statement for the MV in 11.2. Note the ROLLUP clause in the GROUP BY.
    SELECT
      GROUPING_ID(T3."CLASS_ID", T3."FAMILY_ID", T3."ITEM_ID")  SYS_GID,
      (CASE GROUPING_ID(T3."CLASS_ID", T3."FAMILY_ID", T3."ITEM_ID")
       WHEN 3
       THEN TO_CHAR(('A_' || T3."CLASS_ID") )
       WHEN 1
       THEN TO_CHAR(('B_' || T3."FAMILY_ID") )
       ELSE TO_CHAR(('C_' || T3."ITEM_ID") )  END)     "PROD",
      T3."CLASS_ID" "D1_PROD_A_ID",
      T3."FAMILY_ID" "D1_PROD_B_ID",
      T3."ITEM_ID" "D1_PROD_C_ID",
      SUM(T2."UNIT_PRICE")     "PRICE",
      COUNT(T2."UNIT_PRICE")  "COUNT_PRICE",
      COUNT(*)  "SYS_COUNT"
    FROM
      GLOBAL."PRICE_AND_COST_FACT" T2,
      GLOBAL."PRODUCT_DIM" T3
    WHERE
      (T3."ITEM_ID" = T2."ITEM_ID")
    GROUP BY
      (T3."CLASS_ID") ,
      ROLLUP ((T3."FAMILY_ID") , (T3."ITEM_ID") )Next I modified the dimension to add a new hierarchy, DUMMY, containing just the leaf level, C. Once I have mapped the new level and re-enabled MVs, I get the following formulation.
    SELECT
      GROUPING_ID(T3."CLASS_ID", T3."FAMILY_ID", T3."ITEM_ID")  SYS_GID,
      (CASE GROUPING_ID(T3."CLASS_ID", T3."FAMILY_ID", T3."ITEM_ID")
       WHEN 3
       THEN ('A_' || T3."CLASS_ID")
       WHEN 1
       THEN ('B_' || T3."FAMILY_ID")
       WHEN 0
       THEN ('C_' || T3."ITEM_ID")
       ELSE NULL END)  "PROD",
      T3."CLASS_ID" "D1_PROD_A_ID",
      T3."FAMILY_ID" "D1_PROD_B_ID",
      T3."ITEM_ID" "D1_PROD_C_ID",
      SUM(T2."UNIT_PRICE")     "PRICE",
      COUNT(T2."UNIT_PRICE")  "COUNT_PRICE",
      COUNT(*)  "SYS_COUNT"
    FROM
      GLOBAL."PRICE_AND_COST_FACT" T2,
      GLOBAL."PRODUCT_DIM" T3
    WHERE
      (T3."ITEM_ID" = T2."ITEM_ID")
    GROUP BY
      GROUPING SETS ((T3."CLASS_ID") , (T3."FAMILY_ID", T3."CLASS_ID") , (T3."ITEM_ID", T3."FAMILY_ID", T3."CLASS_ID") )This puts things back the way they were in 11.1.0.7 when the GROUPING SETS clause was used in all cases. Note that the two queries are logically equivalent.

  • Materialized View with Joins

    Dear Dev/DBAs,
    I have the following scenario:
    SQL> CREATE TABLE T1 (ID NUMBER(3),NAME VARCHAR2(10));
    SQL> CREATE TABLE T2 (ID NUMBER(3),NAME VARCHAR2(10));
    The T1 contains records having the ID num from 10 to 80 and the T2 having the ID from 90 to 170
    SQL> SELECT * FROM T1 JOIN ALL SELECT * FROM T2
    It give all records in the 2 tables.
    I'm planning to create a materialized view (like CREATE MATERIALIZED VIEW V_TAB REFRESH ON COMMIT AS SELECT * FROM T1 JOIN ALL SELECT * FROM T2) and it seems i can't do with the error ORA-12054, further the oracle documentation says that materialized view can only be used with a simple join.
    Do you have another solution??
    Note that the materialized views can be used to improve queries.
    Thank you in advance

    Straight from the link I posted:
    *Restrictions on Fast Refresh on Materialized Views with UNION ALL*Materialized views with the UNION ALL set operator support the REFRESH FAST option if the following conditions are satisfied:
    * The defining query must have the UNION ALL operator at the top level.
    The UNION ALL operator cannot be embedded inside a subquery, with one exception: The UNION ALL can be in a subquery in the FROM clause provided the defining query is of the form SELECT * FROM (view or subquery with UNION ALL) as in the following example:
    CREATE VIEW view_with_unionall AS
    (SELECT c.rowid crid, c.cust_id, 2 umarker
    FROM customers c WHERE c.cust_last_name = 'Smith'
    UNION ALL
    SELECT c.rowid crid, c.cust_id, 3 umarker
    FROM customers c WHERE c.cust_last_name = 'Jones');
    CREATE MATERIALIZED VIEW unionall_inside_view_mv
    REFRESH FAST ON DEMAND AS
    SELECT * FROM view_with_unionall;
    Note that the view view_with_unionall satisfies the requirements for fast refresh.
    * Each query block in the UNION ALL query must satisfy the requirements of a fast refreshable materialized view with aggregates or a fast refreshable materialized view with joins.
    The appropriate materialized view logs must be created on the tables as required for the corresponding type of fast refreshable materialized view.
    Note that the Oracle Database also allows the special case of a single table materialized view with joins only provided the ROWID column has been included in the SELECT list and in the materialized view log. This is shown in the defining query of the view view_with_unionall.
    * The SELECT list of each query must include a maintenance column, called a UNION ALL marker. The UNION ALL column must have a distinct constant numeric or string value in each UNION ALL branch. Further, the marker column must appear in the same ordinal position in the SELECT list of each query block.
    * Some features such as outer joins, insert-only aggregate materialized view queries and remote tables are not supported for materialized views with UNION ALL.
    * Partiton Change Tracking (PCT)-based refresh is not supported for UNION ALL materialized views.
    * The compatibility initialization parameter must be set to 9.2.0 or higher to create a fast refreshable materialized view with UNION ALL.

  • Materialized View  with Joins and Possibilities of Partitioning

    Hi,
    We have a materialized view which has a data to query around 12 gb. The query goes some thing like
    this.
      Select a.c1,a.c2,b.c1,b.c2,c.c1,c.c2
      from a,b,c
      where a.c1=b.c1
      --and the where condition goes on...
      --i.e Basically joining 3 different tables with complex join conditions but without any group functions and subqueries.
       Now i have few questions here.
    Firstly as the Mview is created with joins we will have to create separate logs for each tables and we have did the same. The logs are created with rowid and sequence for better performance during refresh.
    Question No 1
    Is this is a best approach for materializing a query with complex join conditions. Or Is it better to make 3 different materialized views for each 3 tables and join those 3 MViews and write a query for my report. I ask this question just to make sure the refresh time is brought down by this method. But as such as we have created 3 different logs for 3 tables the refresh must be happening separately.
    Question No 2
    Data is likely to grow faster and faster on this. So we have a idea of making this a partitioned Mview. Can the Pro's advice me on this and suggest me the right practice for the same and help me to correct links from where i can pick a example and continue from there.
    Question No 3
    How to find whether the materialized view has refreshed on a fast mode or complete mode after the last refresh.
    Apart from querying and checking the rowid which i feel is quiet cumbersome for a mview with a data volume like what we have. By default when i see the info in TOAD for this Mview it shows Refresh Mode as "Force". But how to ascertain this.
    Thanks in anticipation for a good round of discussion

    Hi,
    We have a materialized view which has a data to query around 12 gb. The query goes some thing like
    this.
      Select a.c1,a.c2,b.c1,b.c2,c.c1,c.c2
      from a,b,c
      where a.c1=b.c1
      --and the where condition goes on...
      --i.e Basically joining 3 different tables with complex join conditions but without any group functions and subqueries.
       Now i have few questions here.
    Firstly as the Mview is created with joins we will have to create separate logs for each tables and we have did the same. The logs are created with rowid and sequence for better performance during refresh.
    Question No 1
    Is this is a best approach for materializing a query with complex join conditions. Or Is it better to make 3 different materialized views for each 3 tables and join those 3 MViews and write a query for my report. I ask this question just to make sure the refresh time is brought down by this method. But as such as we have created 3 different logs for 3 tables the refresh must be happening separately.
    Question No 2
    Data is likely to grow faster and faster on this. So we have a idea of making this a partitioned Mview. Can the Pro's advice me on this and suggest me the right practice for the same and help me to correct links from where i can pick a example and continue from there.
    Question No 3
    How to find whether the materialized view has refreshed on a fast mode or complete mode after the last refresh.
    Apart from querying and checking the rowid which i feel is quiet cumbersome for a mview with a data volume like what we have. By default when i see the info in TOAD for this Mview it shows Refresh Mode as "Force". But how to ascertain this.
    Thanks in anticipation for a good round of discussion

  • Materialized view with join

    In 10g release 2,I tried to create following materialized view with join:
    test_link is a normal table
    test_geom is a table contains a column in SDO_GEOMETRY
    CREATE MATERIALIZED VIEW LOG ON test_link with rowid
    CREATE MATERIALIZED VIEW LOG ON test_geom with rowid,primary key
    CREATE MATERIALIZED VIEW MV_LINK USING INDEX REFRESH FAST ON DEMAND AS
    SELECT li.rowid link_rowid,geom.rowid geom_rowid,li.link_id,geom.link
    FROM test_link li, test_geom geom
    WHERE li.link_id=geom.link_id
    But I always got an error like:
    ORA-12015: cannot create a fast refresh materialized view from a complex query
    If I change the geometry table to another table, everything works fine.
    Anyone have ideas?

    Unfortunately, creating a fast refreshable materialized view on a join with one of the select columns being a user defined type (sdo_geometry is a user defined type) is not allowed. See 5303489 in the metalink bug database.
    You could do like the workaround in the article suggests and create two materialized views and then create a regular view on top.
    In our scenario, our materialized view also contains unions, so we would really like to have one physical object at the end of the day. One approach that we are currently investigating is to create the materialized view (MV1) without the geometry column, which makes it fast refreshable, and also create a materialized view (MV2) on the table containing the geometry column. MV2 is also fast refreshable. We then create a table (T3) that contains all of the columns from MV1, plus a geometry column. An insert, update, delete trigger on MV1 is created. The trigger is used to push all of the columns from MV1 to T3 and the geometry column from MV2 to T3. I have created the above in one of our test environments and haven't encountered any issues yet.
    Let me know if you come up with a better approach.

  • Materialized view with tables in different schemas

    Hello,
    I want to create a materialized view with a table from a different schema in the SELECT statement. For materialized view I would like to apply the "REFRESH COMPLETE ON COMMIT" option.
    Here the code:
    CREATE MATERIALIZED VIEW S1.MV_EXAMPLE
    TABLESPACE TS1
    PCTFREE 0
    BUILD IMMEDIATE
    REFRESH COMPLETE ON COMMIT
    AS
    SELECT T.COLUMN1 COLUMN
    FROM S2.TABLE1 T
    I can't execute this SQL because I get an "insufficient privileges" error to this table:
    FROM S2.TABLE1 T
    FEHLER in Zeile 9:
    ORA-01031: Insufficient privileges
    User S1 has the following privileges:
    CREATE SESSION
    CREATE SNAPSHOT
    CREATE TABLE
    CREATE QUERY REWRITE
    SELECT ANY TABLE
    User S2 has the following privileges:
    CREATE SESSION
    CREATE SNAPSHOT
    CREATE TABLE
    CREATE QUERY REWRITE
    ALTER ANY SNAPSHOT
    Which privileges are missing?
    Thanks, Mathias

    Thanks Kamal for your answer!
    S1 has the grant select directly. But I solveld the problem. The system privilege "ON COMMIT REFRESH" was missing for S1. This has to be set if any of the tables are outside the owner's schema of the materialized view (ORACLE documentation - Data Warehouse Guide).
    But one thing is not clear to me yet and the ORACLE documentation doesn't give me an answer. I can set the refresh-attribute ON COMMIT on a materialized view containing only joins when a group by clause is set. If the group by clause is missing I can't! Why?
    Regards, Mathias

  • Create Materialized View with GROUP BY

    I have a table
    treecluster NUMBER(3)
    treenumber NUMBER(3)
    treedate DATE
    nestnumber NUMBER(3)
    eggs NUMBER(3)
    nestlings NUMBER(3)
    fledglings NUMBER(3)
    nestfate VARCHAR2(10)
    nestfailurecode NUMBER(2)
    I want to group the data by treecluster, treenumber, year, and nest number and get a max eggs, nestlings, fledglings. I am doing this with the following:
    SELECT treecluster, treenumber, to_char(nestchecksdate, 'YYYY'), nestnumber,
    max(eggs), max(nestlings), max(fledglings)
    FROM nestchecks
    GROUP BY treecluster, treenumber, to_char(nestchecksdate, 'YYYY'), nestnumber;
    This works fine.
    The last record for a year has a nestfate and nestfailurecode value.
    I need to group all information and then also get the nestfate and nestfailurecode for the year. How do I go about getting this information?
    I am really trying to create a summary materialized view based on a data table so the select I am trying to craft would be a part of a create materialized view as SELECT...
    so I want to be able to group the info and add in the nestfate and failurecode in one step.
    Thanks!

    try this:
    SQL> select * from table_tree;
    TREECLUSTER TREENUMBER NESTCHECK NESTNUMBER       EGGS  NESTLINGS FLEDGLINGS NESTFATE   NESTFAILURECODE
              4        167 17-MAY-00          2          0          0
              4        167 24-MAY-00          2          3          0
              4        167 30-MAY-00          2          3          0
              4        167 12-JUN-00          2          0          1            FAILURE                  2
    select a.treecluster, a.treenumber, to_char(a.nestyear,'YYYY') nestyear,
           a.nestnumber, a.eggs, a.nestlings, a.fledglings, a.nestfate, a.nestfailurecode
    from   (select max(treecluster) treecluster, max(treenumber) treenumber,
                   max(nestchecksdate) nestyear,
                   max(nestnumber) nestnumber, max(eggs) eggs, max(nestlings) nestlings,
                   nvl(max(FLEDGLINGS),0) FLEDGLINGS, max(NESTFATE) NESTFATE,
                   max(nestfailurecode) nestfailurecode
            from table_tree) a
    TREECLUSTER TREENUMBER NEST NESTNUMBER       EGGS  NESTLINGS FLEDGLINGS NESTFATE   NESTFAILURECODE
              4        167 2000          2          3          1          0 FAILURE                  2hope this helps

  • Updatable Materialized View with Union ALL

    (please don't ask about db structure)
    DB: 11gR2
    create table table_1  (
        id number primary key,
        val varchar2(100)
    create table table_2  (
        id number primary key,
        val varchar2(100)
    insert into table_1(id) values (0);
    insert into table_1(id) values (2);
    insert into table_1(id) values (3);
    insert into table_1(id) values (4);
    insert into table_1(id) values (5);
    insert into table_2(id) values (10);
    insert into table_2(id) values (12);
    insert into table_2(id) values (13);
    insert into table_2(id) values (14);
    insert into table_2(id) values (15);
    update table_1 set val='Table1 val:'||id;
    update table_2 set val='Table2 val:'||id;
    create view v_table_all as
    select * from table_1
    view V_TABLE_ALL created.
    select * from v_table_all;
    ID                     VAL                                                                                                 
    0                      Table1 val:0                                                                                        
    2                      Table1 val:2                                                                                        
    3                      Table1 val:3                                                                                        
    4                      Table1 val:4                                                                                        
    5                      Table1 val:5                                                                                        
    select column_name, updatable, insertable, deletable
    from user_updatable_columns
    where table_name = 'V_TABLE_ALL'
    COLUMN_NAME                    UPDATABLE INSERTABLE DELETABLE
    ID                             YES       YES        YES      
    VAL                            YES       YES        YES      
    update v_table_all set val='XXX changed' where id = 3;
    1 row updated.
    select * from table_1;
    ID                     VAL                                                                                                 
    0                      Table1 val:0                                                                                        
    2                      Table1 val:2                                                                                        
    3                      XXX changed                                                                                         
    4                      Table1 val:4                                                                                        
    5                      Table1 val:5                                                                                        
    rollback;
    select * from table_1;
    ID                     VAL                                                                                                 
    0                      Table1 val:0                                                                                        
    2                      Table1 val:2                                                                                        
    3                      Table1 val:3                                                                                        
    4                      Table1 val:4                                                                                        
    5                      Table1 val:5                                                                                        
    create or replace view v_table_all as
    select * from table_1
    union select * from table_2;
    view V_TABLE_ALL created.
    select * from v_table_all;
    ID                     VAL                                                                                                 
    0                      Table1 val:0                                                                                        
    2                      Table1 val:2                                                                                        
    3                      Table1 val:3                                                                                        
    4                      Table1 val:4                                                                                        
    5                      Table1 val:5                                                                                        
    10                     Table2 val:10                                                                                       
    12                     Table2 val:12                                                                                       
    13                     Table2 val:13                                                                                       
    14                     Table2 val:14                                                                                       
    15                     Table2 val:15  
    select column_name, updatable, insertable, deletable
    from user_updatable_columns
    where table_name = 'V_TABLE_ALL'
    COLUMN_NAME                    UPDATABLE INSERTABLE DELETABLE
    ID                             NO        NO         NO       
    VAL                            NO        NO         NO       
    trying update:
    update v_table_all set val='XXX changed' where id = 3;
    SQL-Fehler: ORA-01732: Datenmanipulationsoperation auf dieser View nicht zulässig
    01732. 00000 -  "data manipulation operation not legal on this view"
    *Cause:   
    *Action:
    drop view v_table_all;
    view V_TABLE_ALL dropped.all is ok before this point.
    now we want create a new materialized view with some query
    create  materialized view v_table_all
    as
    select * from table_1
    union all select * from table_2 ;
    materialized view V_TABLE_ALL created.
    select column_name, updatable, insertable, deletable
    from user_updatable_columns
    where table_name = 'V_TABLE_ALL'
    COLUMN_NAME                    UPDATABLE INSERTABLE DELETABLE
    ID                             YES       YES        YES      
    VAL                            YES       YES        YES       it seems to be ok with update.
    but...
    update v_table_all set val='XXX changed' where id = 3;
    SQL-Fehler: ORA-01732: Datenmanipulationsoperation auf dieser View nicht zulässig
    01732. 00000 -  "data manipulation operation not legal on this view"
    *Cause:   
    *Action:How can solve this issue??
    Any suggestion

    Looks like user_updatable_columns sort of thinks the MV is just a table - I don't know about that...
    An MV on a single table can be updated - I tried that and it works:
    create materialized view mv_table_1 for update
    as
    select * from table_1;I noticed [url http://download.oracle.com/docs/cd/E11882_01/server.112/e16579/advmv.htm#sthref294]examples stating the UNION ALL needs a "marker" so Oracle can know from the data which source table a row in the MV originates from - like this:
    create materialized view v_table_all for update
    as
    select 'T1' tab_id, table_1.* from table_1
    union all
    select 'T2' tab_id, table_2.* from table_2 ;But that also fails (the "marker" requirement was specifically for FAST REFRESH, so it was just a long shot ;-) )
    What are you planning to do?
    <li>Create the MV.
    <li>Update records in the MV - which then is no longer consistent with the source data.
    <li>Schedule a complete refresh once in a while - thereby overwriting/losing the updates in the MV.
    If that is the case, I suggest using a true table rather than an MV.
    <li>Create table t_table_all as select ... .
    <li>Update records in the table - which then is no longer consistent with the source data.
    <li>Schedule a job to delete table and insert into table select ... once in a while - thereby overwriting/losing the updates in the table.
    In other words a kind of "do it yourself MV".
    I cannot see another way at the moment? But perhaps try in the data warehousing forum - the people there may have greater experience with MV's ;-)

  • Create materialized view with specific column sizes

    Hi all,
    I'm trying to create a materialized view with a specific a column size. Something like
    create materialized view test_mv
    refresh force on demand
    as
    select id,
           cast(my_compound_field as nvarchar2(50))
    from ( select id,
                  field1 || field2 my_compound_field
           from   my_table);But Oracle seems to ignore the cast and takes the maximum size it finds for field1 || field2 in the select query. The resulting table has a column nvarchar2(44) instead of nvarchar2(50).
    This can give a problem when the view is refreshed... there could be new data that exceeds the current size, i.e. where length(field1 || field2) > 44.
    How can I override the column size of a field in a materialized view?
    Edit: Some additional info to clarify my case:
    field1 and field2 are defined as nvarchar2(25). field1 || field2 can theoretically have a length of 50, but there is currently no data in my table that results in that length, the max is 44. I am afraid that there will be data in the future that exceeds 44, resulting in an error when the MV is refreshed!
    Edited by: Pleiadian on Jan 25, 2011 2:06 PM

    Cannot reproduce what you are saying is happening.
    SQL> create table t (a nvarchar2(50), b nvarchar2(50));
    Table created.
    SQL> create materialized view tmv as
      2  select a, b, a || b c from t;
    Materialized view created.
    SQL> desc tmv
    Name                                      Null?    Type
    A                                                  NVARCHAR2(50)
    B                                                  NVARCHAR2(50)
    C                                                  NVARCHAR2(100)
    SQL> drop materialized view tmv;
    Materialized view dropped.
    SQL> create materialized view tmv as
      2  select a, b, substr(a || b, 1, 10) c from t;
    Materialized view created.
    SQL> desc tmv
    Name                                      Null?    Type
    A                                                  NVARCHAR2(50)
    B                                                  NVARCHAR2(50)
    C                                                  NVARCHAR2(10)
    SQL> select * from v$version;
    BANNER
    Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - 64bit Production
    PL/SQL Release 11.1.0.7.0 - Production
    CORE    11.1.0.7.0      Production
    TNS for Linux: Version 11.1.0.7.0 - Production
    NLSRTL Version 11.1.0.7.0 - Production
    SQL>Edited by: 3360 on Jan 25, 2011 8:10 AM
    And with data
    SQL> insert into t values ('3123423423143hhshgvcdcvw', 'ydgeew  gdfwe   dfefde  wfjjjjjjj');
    1 row created.
    SQL> commit;
    Commit complete.
    SQL> desc tmv
    Name                                      Null?    Type
    A                                                  NVARCHAR2(50)
    B                                                  NVARCHAR2(50)
    C                                                  NVARCHAR2(10)
    SQL> select * from tmv;
    A
    B                                                  C
    3123423423143hhshgvcdcvw
    ydgeew  gdfwe   dfefde  wfjjjjjjj                      3123423423

  • Import materialized view with rowid

    We are upgrading the Data Warehouse and Source Database from Oracle 9i to 10G.
    For replication we use materialized views. Since in our source database we don't have any primary keys, we maintain materialized view logs based on rowids in Source/Master Database.
    Similarly in the Staging schema of or data warehouse we have defined materialized views with rowid and fast refresh option.
    We are using imp/exp utility for migrating both the source database and the data warehouse.
    Since our replication is based on rowid, when we migrate to new environment, all the rowids of the source tables would be redifined making the materialized view tables rowid obsolete.
    One way is to do compete refresh to bring both source database and Staging database baack in sync, but that would be expensive and time consuming.
    Is there any way we can preserve rowid or keep the materialized views in staging in sync with source database ?
    Thanks.

    WITH ROWID is used with REFRESH clause. Try
    SQL> CREATE MATERIALIZED VIEW xxca_project_status_mv
    2 REFRESH WITH ROWID
    3 AS
    do I have to create a log.Only if you want a fast refresh.

  • Creating Materialized View with the Distinct key word

    Hi
    I need help to create Materialized View with the Distinct key word.
    my query is
    select distinct col1, col2 from t1;
    is there any alternate of DISTINCT because I can not refresh the MV as fast on demand if I am using it and Fast on Demand is a must for me.

    hi
    check out this link
    http://download-west.oracle.com/docs/cd/B12037_01/server.101/b10736/basicmv.htm#i1007299
    http://download.oracle.com/docs/cd/B19306_01/server.102/b14220/intro.htm#sthref183
    hope this helps
    CHeers

  • How to create materialized  view with parameter and index ?

    Hi all,
    i am using oracle 11g.
    i want to create  parameter materialized view  with two parameter (STORED_VALUE, LOV_NAME) with  an index .
    i have below view
    CREATE OR REPLACE FORCE VIEW SR_MY_TEST(DISPLAYED_VALUE, STORED_VALUE, LOV_NAME) AS
      SELECT  DISPLAYED_VALUE , LOVVALUE.STORED_VALUE , lovname.lov_name
               FROM (SELECT T.LOV_VALUE_ID,
          T.LOV_ID,
          T.ORG_ENTITY_ID,
          T.STORED_VALUE,
          T.DISPLAYED_VALUE,
          T.ENTERPRISE_ID
         FROM MS_QS_LIST_OF_VALUES_T T) lovvalue, ms_qs_lov_names lovname
              WHERE lovvalue.lov_id = lovname.lov_id
                AND lovvalue.org_entity_id = 1
                and LOVVALUE.ENTERPRISE_ID = 100000
                AND LOVNAME.ENTERPRISE_ID = 100000;
    i want to create index on   STORED_VALUE, LOV_NAME
    Thanks
    Damby

    No.AFAIK, there's nothing called as "parameterized MV".
    Materialized View store data like tables (and not like Views). So, does it make sense when you say - "table with parameters" ?
    Could you please explain your business requirement?
    What is the purpose behind those 2 parameters?

Maybe you are looking for