http://www.sqlsnippets.com/en/home.

htm l PL/SQL Collections
The chart below lists the properties of the three collection types on a set of parameters such as size, ease of modification, persistence, etc. Index By Tables Nested Tables Varrays Bounded i.e. holds a declared number of elements, though this number can be changed at runtime Sequential numbers, starting from one Can be stored in the database using equivalent SQL types, and manipulated through SQL (but with less ease than nested tables) Standard subscripting syntax e.g. color(3) is the 3rd color in varray color

Size Unbounded i.e. the number Unbounded i.e. the of elements it can hold is not number of elements it pre-defined can hold is not predefined Subscript Can be arbitrary numbers or Characteristics strings. Need not be sequential. Database Index by tables can be used Storage in PL/SQL programs only, cannot be stored in the database. Sequential numbers, starting from one Can be stored in the database using equivalent SQL types, and manipulated through SQL.

Referencing Works as key-value pairs. and lookups e.g. Salaries of employees can be stored with unique employee numbers used as subscripts sal(102) := 2000;

Similar to one-column database tables. Oracle stores the nested table data in no particular order. But when you retrieve the nested table into a PL/SQL variable, the rows are given consecutive subscripts starting at 1. Almost like index-by tables, except that

Flexibility to Most flexible. Size can changes increase/ decrease

Not very flexible. You must retrieve and

dynamically. Elements can be added to any position in the list and deleted from any position. Mapping with Hash tables other programming languages

subscript values are not as flexible. Deletions are possible from noncontiguous positions. Sets and bags

update all the elements of the varray at the same time. Arrays

Which Collection Type to Use?
You have all the details about index by tables, nested tables and varrays now. Given a situation, will one should you use for your list data? Here are some guidelines. Use index by tables when:
• • • • •

Your program needs small lookups The collection can be made at runtime in the memory when the package/ procedure is initialized The data volume is unknown beforehand The subscript values are flexible (e.g. strings, negative numbers, non-sequential) You do not need to store the collection in the database

Use nested tables when:
• • • • •

The data needs to be stored in the database The number of elements in the collection is not known in advance The elements of the collection may need to be retrieved out of sequence Updates and deletions affect only some elements, at arbitrary locations Your program does not expect to rely on the subscript remaining stable, as their order may change when nested tables are stored in the database.

Use varrays when:
• • • •

The data needs to be stored in the database The number of elements of the varray is known in advance The data from the varray is accessed in sequence Updates and deletions happen on the varray as a whole and not on arbitrarily located elements in the varray

Sample Code

Associative Array
Declare

Nested Table

Varray

Declare a collection variable.
aa_0 p.aa_type ;

nt_0 p.nt_type ;

va_0 p.va_type ;

Declare, initialize, and load a collection variable.
aa p.aa_type ; -- cannot load values in -- declaration begin nt p.nt_type := p.nt_type( 'a', 'b' ) ; va p.va_type := p.va_type( 'a', 'b' ) ;

Let's inspect the variables to see what they look like at this point (NULL means the variable is not initialized).
p.print( p.print( p.print( p.print( p.print( 'aa_0 is ' ); aa_0 ); ' ' ); 'aa is ' ); aa ); p.print( p.print( p.print( p.print( p.print( 'nt_0 is ' ); nt_0 ); ' ' ); 'nt is ' ); nt ); p.print( p.print( p.print( p.print( p.print( 'va_0 is ' ); va_0 ); ' ' ); 'va is ' ); va );

aa_0 is NOT NULL .first = .last = .count = .limit = aa is NOT NULL .first = .last = .count = .limit =

and empty NULL NULL 0 NULL and empty NULL NULL 0 NULL

nt_0 is NULL nt is (1) a (2) b .first .last .count .limit

va_0 is NULL va is (1) a (2) b .first .last .count .limit

= = = =

1 2 2 NULL

= = = =

1 2 2 10

count .create two copies -. va(4) := 'd' .last = .extend .print( va_0 ). -.first = . nt(4) := 'd' . select into from where val nt(2) t val = 'B' .limit = = = = 1 7 7 NULL (1) a (2) b (3) c (4) d (5) e (6) e (7) e .count = .print( va ).Initialize a collection after it has been declared.of row #5 va.print( nt ). -.of row #5 aa(6) := 'e' . NOT NULL .last .first = 1 . p. aa(7) := 'e' .extend(2. nt(3) := 'c' . nt.add 1 row nt. -. p. aa(3) := 'c' .limit = 10 Load a single value from the database into a collection row.va_type(). -. va(3) := 'c' .nt_type() . aa(4) := 'd' . -.add 1 row va.limit = and empty NULL NULL 0 NULL va_0 := p. va(5) := 'e' .print( nt_0 ).count = 7 . -.count = .first = . . NOT NULL .add 2 rows va.last .count . (1) a (2) b (3) c (4) d (5) e (6) e (7) e .extend(2) . aa(5) := 'e' .first .print( aa ).extend . p. p. -.add 1 row at a time aa(1) := 'a' . -. p.add 2 rows nt.first .n/a nt_0 := p.create two copies -.5) . select into from where val aa(2) t val = 'B' .extend(2) .extend(2.limit = = = = 1 7 7 NULL (1) a (2) b (3) c (4) d (5) e (6) e (7) e . nt(5) := 'e' . select into from where val va(2) t val = 'B' .last = . aa(2) := 'b' .limit = and empty NULL NULL 0 10 Add individual rows to a collection.last = 7 .5) .

first = 1 .first = 1 .last = 7 .print ( 'va.count = 7 .first .print( nt ). (1) A (2) B (3) C (4) D (5) E (6) F (7) G .print ( 'aa.count = 7 p.last = 7 .tf( nt.print ( 'nt.limit = = = = 1 7 7 NULL (1) a (2) B (3) c (4) d (5) e (6) e (7) e .print ( 'aa.print( aa ).first = 1 .exists(9) is '|| p.tf( aa. ).tf( va.tf( va. ).first = 1 .last .exists(3) is '|| p.limit = NULL .exists(3) is TRUE va. select val bulk collect into nt from t .exists(3) ) ).exists(9) is FALSE . p.exists(9) is '|| p.tf( nt. select val bulk collect into va from t .exists(3) is '|| p.exists(9) ) p.print( va ).limit = 10 Initialize a collection and load it with multiple database values (pre-existing contents will be lost).exists(9) is '|| p.exists(9) is FALSE nt.last .exists(3) ) ).exists(9) ) p.limit = NULL . aa.limit = = = = 1 7 7 NULL (1) a (2) B (3) c (4) d (5) e (6) e (7) e .limit = 10 Test a row's existence by subscript.first .last = 7 .count .count . p.print ( 'nt.print( va ).exists(3) is TRUE nt.p.exists(9) ) ).exists(3) ) ). p. p.last = 7 .print ( 'va. p.print( nt ). p. (1) A (2) B (3) C (4) D (5) E (6) F (7) G .print( aa ).exists(9) is FALSE va. p. select val bulk collect into aa from t .exists(3) is '|| p.tf( aa.count = 7 . (1) a (2) B (3) c (4) d (5) e (6) e (7) e . (1) A (2) B (3) C (4) D (5) E (6) F (7) G .exists(3) is TRUE aa.count = 7 p.

(1) a (2) B (3) c (4) D (5) E (6) F (7) G . p.count = 7 p. nt(3) := 'c' .cannot use "=" with -.tf( 'X' member of nt ) -.count = 7 p. nt(1) := 'a' . va(1) := 'a' .Test a row's existence by content. -.two associative arrays nt_0 := nt . aa(3) := 'c' .tf( 'C' member of nt ) ).first = 1 .limit = NULL .limit = 10 Remove rows from the middle of a collection.first = 1 . (1) a (2) B (3) c (4) D (5) E (6) F (7) G .last = 7 .print ( '''C'' member of nt is '|| p. 'C' member of nt is TRUE 'X' member of nt is FALSE Compare two collections for equality.two varrays end if.use a loop (see below) p.print ( '''X'' member of nt is '|| p.print( aa ). -.print( nt ).print( 'equal' ). aa(1) := 'a' .print( 'not equal' ). else p. va(3) := 'c' .limit = NULL . p. -.last = 7 .use a loop (see below) ). equal Update a collection row.cannot use "=" with if nt_0 = nt then p. . (1) a (2) B (3) c (4) D (5) E (6) F (7) G .print( va ).count = 7 .last = 7 .first = 1 . -.

2. p. nt.limit = NULL . i := aa.delete(2).first .limit = NULL . end loop.0) .4).print( nt ).print( aa ). 1. F 7.print ( i ||'. (1) a (5) E (6) F (7) G .print( va ). begin i := nt. a 5.aa.first. aa. nt. G 1.trim.limit = NULL .count = 1 p.not possible p.next(i) .count = 1 p. (4) D .first . a 5.print ( i ||'.print( va ). '|| nt(i) ). (1) a (2) B (3) c (4) D (5) E (6) F (7) G .delete(3.delete(3.last = 4 . va.next(i) . declare i binary_integer . end loop. '|| aa(i) ). 1.4).last = 4 .delete(7).first = 4 . while i is not null loop p.count = 4 p. aa. nvl(va.last = 7 .print ( i ||'. G Remove row(s) from the end of a collection.print( aa ). nt.6).trim.print( nt ). (1) a (5) E (6) F (7) G . F end. 7.count = 4 p. declare i binary_integer . while i is not null loop p. '|| va(i) ). i := nt. 5.limit = NULL . E 6.last = 7 .first = 1 .delete(5.delete(2). -. E 6.. end. 3.last.first = 4 .first = 4 .trim(2). for i in nvl(va. end loop. begin i := aa.trim(2). 4.first = 4 .count = 7 .first = 1 . aa. nt. (4) D . 6. (1) a (2) B (3) c (4) D . a B c D E F va. G 7.limit = 10 Loop through all rows in the collection.last = 7 .-1) loop p.

extend(3) nt(5) := 'E' nt(6) := 'F' nt(7) := 'G' .3.7 -. aa(7) := 'G' .first = 1 . . aa(5) := 'E' .last = 7 .last = 7 .note we do not need to -.".count = and empty NULL NULL 0 p.extend(3) va(5) := 'E' va(6) := 'F' va(7) := 'G' p.we do need to call -. .extend" for rows 5.trim" .last = .6. aa(2) := 'B' .print( nt ).count = 4 . aa(6) := 'F' .trim" nt.count = 7 (1) a (2) B (3) c (4) D (5) E (6) F (7) G .print( nt ). va.delete . -.limit = NULL .call ".6.first = 1 .last = 7 .first = 1 . nt(3) := 'C' .first = .count = 7 (1) A (2) B (3) C (4) D (5) E (6) F (7) G . .. aa(4) := 'D' .7).which were removed with -.extend" first since 5.delete .last = 4 . p.4 which were -.limit = 10 Delete all rows in the collection (frees memory too). (1) A (2) B (3) C (4) D (5) E (6) F (7) G . -. .count = and empty NULL NULL 0 . aa. aa(3) := 'C' . NOT NULL .print( aa ).first = . nt.last = .4) and trim operations (rows 5.print( va ). va. NOT NULL . nt(4) := 'D' .3.delete" nt(2) := 'B' . . p.delete .count = and empty NULL NULL 0 p. p. NOT NULL .limit = 10 Reuse rows left vacant by earlier delete operations (rows 2.limit = NULL .extend" for rows -.".count = 7 .6.2.7 were removed with ".print( va ).removed with ". ----we need to call ".first = .last = . .print( aa ).

g.variable instead declare nt_null p. (for Integers) The collection can be defined to hold a predefined maximum number of rows. -. uninitialized state. The collection can be manipulated in PL/SQL with MULTISET Operators e. use a null -.limit = 10 Set a collection to NULL. Two collections can be compared for equality with the "=" operator. The collection can be unnested in a query using the TABLE() collection expression. The collection can be initialized with multiple rows of data using a single command (i. end. The collection must be extended before a new row is added.e.8.nt_type . 1. e.print( nt ).va_type ..work. Y The collection must be initialized before used..limit = NULL . There can be gaps between subscripts.not possible -.. end. p. Legal subscript datatypes. begin va := va_null . MULTISET UNION.e.2**31 Varray Y Y Integer 1. begin nt := nt_null . any -2**31.limit = NULL ."nt := null" will not -. Nested Table Y Integer 1. i. Rows in the collection retain their order when the entire n/a collection is saved in a database column.variable instead declare va_null p..work. Associative Characteristic Array The entire collection can be saved in a database column.print( va ). NULL / The next table presents operational characteristics of each collection type."va := null" will not -. use a null -. NULL end. p.3. a constructor). MULTISET INTERSECT.g. -.2**31 Legal subscript value ranges.2**31 Y Y Y Y Y Y Y Y Y Y Y Y .

6 had only the DECODE function.1. . CASE is capable of more logical comparisons such as < > etc. more meaningful and more powerful function. To achieve the same effect with DECODE. though. An example of putting employees in grade brackets based on their salaries – this can be done elegantly with CASE. CASE can work with logical operators other than ‘=’ DECODE can do an equality check only. ranges of data had to be forced into discrete form making unwieldy code.6. 1. which DECODE cannot – which we’ll see in this article. case 3 when sal < 1000 4 then 'Grade I' 5 when (sal >=1000 and sal < 2000) 6 then 'Grade II' 7 when (sal >= 2000 and sal < 3000) 8 then 'Grade III' 9 else 'Grade IV' 10 end sal_grade 11 from emp 12 where rownum < 4.The Difference Between DECODE and CASE DECODE and CASE statements in Oracle both provide a conditional construct. CASE can. Everything DECODE can do. as a standard. SQL> select ename 2 . There is a lot more that you can do with CASE.1. of this form: if A = n1 then A1 else if A = n2 then A2 else X Databases before Oracle 8. CASE was introduced in Oracle 8.

empno) 11 then 'Managers' 12 else 13 'General Employees' 14 end emp_category 15 from emp e 16 where rownum < 5.put_line('very good'). .ename in ('KING'.'SMITH'. CASE can work with predicates and searchable subqueries DECODE works with expressions which are scalar values only.'WARD') 6 then 'Top Bosses' 7 -.ename. 3 begin 4 grade := 'b'. 8 when 'c' then dbms_output. 5 case grade 6 when 'a' then dbms_output.searchable subquery 8 -. CASE can work with predicates and subqueries in searchable form.predicate with "in" 4 -. illustrating these two uses of CASE.identify if this emp has a reportee 9 when exists (select 1 from emp emp1 10 where emp1.mgr = e. 9 when 'd' then dbms_output. 10 when 'f' then dbms_output.put_line('poor').mark the category based on ename list 5 when e. 2 case 3 -.put_line('fair'). SQL> declare 2 grade char(1). ENAME ---------SMITH ALLEN WARD JONES EMP_CATEGORY ----------------Top Bosses General Employees Top Bosses Managers 3. SQL> select e.ENAME ---------SMITH ALLEN WARD SAL_GRADE --------Grade I Grade II Grade II 2. CASE can work as a PL/SQL construct DECODE can work as a function inside SQL only. 7 when 'b' then dbms_output.put_line('good'). An example of categorizing employees based on reporting relationship. CASE can be a more efficient substitute for IFTHEN-ELSE in PL/SQL.put_line('excellent').

column 7: PL/SQL: Statement ignored SQL> exec proc_test(case :a when 'THREE' then 3 else 0 end). end. output = 3 PL/SQL procedure successfully completed. CASE can even work as a parameter to a procedure call. / PL/SQL procedure successfully completed.'THREE'. * ERROR at line 1: ORA-06550: line 1.0)).'THREE'. SQL> exec :a := 'THREE'. 4.3. END. 'NULL' 3 . SQL> var a varchar2(5). 'NOT NULL' 4 ) null_test 5 from dual.3. BEGIN proc_test(decode(:a. PL/SQL procedure successfully completed. NULL ---NULL . SQL> SQL> 2 3 4 5 6 create or replace procedure proc_test (i number) as begin dbms_output.11 12 13 14 else dbms_output. end. / Procedure created. SQL> exec proc_test(decode(:a. Careful! CASE handles NULL differently Check out the different results with DECODE vs NULL.put_line('no such grade').0)). null. while DECODE cannot.put_line('output = '||i). end case. SQL> select decode(null 2 . column 17: PLS-00204: function or pseudo-column 'DECODE' may be used inside a SQL statement only ORA-06550: line 1.

. T ---------2 SQL> select case 2 when 1 then '1' 2 when '2' then '2' 3 else '3' 4 end 5 from dual. SQL> select decode(2.SQL> select case null 2 when null 3 then 'NULL' 4 else 'NOT NULL' 5 end null_test 6 from dual. DECODE does not Compare the two examples – DECODE gives you a result. DECODE is proprietary to Oracle. though. CASE is ANSI SQL-compliant CASE complies with ANSI SQL.1. CASE gives a data type mismatch error. SQL> select case 2 when null is null 3 then 'NULL' 4 else 'NOT NULL' 5 end null_test 6* from dual SQL> / NULL_TES -------NULL 5. 2 '2'. 3 '3') t 4 from dual.'2'. CASE expects data type consistency. NULL_TES -------NOT NULL The “searched CASE” works as does DECODE. when '2' then '2' * ERROR at line 2: ORA-00932: inconsistent datatypes: expected NUMBER got CHAR 6.1.

'Sales'. 4 30. even if technically achievable. .DECODE is cleaner SQL> select ename 2 . as in: SQL> -. and SQL> -. unreadable code – while the same can be achieved elegantly using CASE.7. decode (deptno. 10. DECODE is shorter and easier to understand than CASE. 5 'Unknown') as department 6 from emp 7 where rownum < 4. case deptno 3 when 10 then 'Accounting' 4 when 20 then 'Research' 5 when 30 then 'Sales' 6 else 'Unknown' 7 end as department 8 from emp 9 where rownum < 4. is a recipe for messy. ENAME ---------SMITH ALLEN WARD DEPARTMENT ---------Research Sales Sales In complex situations. 'Accounting'. Complicated processing in DECODE.An example where DECODE and CASE SQL> -. 3 20.can work equally well. ENAME ---------SMITH ALLEN WARD DEPARTMENT ---------Research Sales Sales SQL> select ename 2 . CASE is shorter and easier to understand. 'Research'. The difference in readability In very simple situations.

count(*). Doing so has no effect on the result. count(*).---------. GRP_B order by grp_a. sum( val ) from t GROUP BY GRP_A. select grp_a. grp_b. grp_b.---------b1 2 20 30 b2 3 50 120 b3 3 32 66 Parentheses may be added around the GROUP BY expression list.---------. select grp_a. GRP_B ) order by grp_a. select grp_a. grp_b .---------. max( val ). MAX. . GRP_A ---------a1 a1 a1 a1 a1 a2 a2 a2 GRP_B VAL ---------. GRP_A ---------a1 a1 a2 GRP_B COUNT(*) MAX(VAL) SUM(VAL) ---------.---------.---------b1 10 b1 20 b2 30 b2 40 b2 50 b3 12 b3 22 b3 32 GROUP BY allows us to group rows together so that we can include aggregate functions like COUNT. grp_b . sum( val ) from t GROUP BY GRP_A order by grp_a . grp_b. select grp_a. count(*).---------a1 5 50 150 a2 3 32 66 We can specify multiple columns in the GROUP BY clause to produce a different set of groupings. grp_b . max( val ). sum( val ) from t GROUP BY ( GRP_A.Grouping Rows with GROUP BY GROUP BY Consider a table like this one. val from t order by grp_a. GRP_A COUNT(*) MAX(VAL) SUM(VAL) ---------. max( val ). and SUM in the result set.

max( val ). grp_b . the same result is usually produced by specifying DISTINCT instead of using GROUP BY. GRP_A ---------a1 a1 a2 GRP_B ---------b1 b2 b3 However.GRP_A ---------a1 a1 a2 GRP_B COUNT(*) MAX(VAL) SUM(VAL) ---------.---------. COUNT(*) MAX(VAL) SUM(VAL) ---------.---------. select count(*). like this. sum( val ) from t . grp_b from t order by grp_a. grp_b from t GROUP BY GRP_A. max( val ). select DISTINCT grp_a. Parentheses are mandatory when specifying an empty set. This groups all rows retrieved by the query into a single group. sum( val ) from t GROUP BY () . grp_b .---------8 50 216 GROUP BY and DISTINCT We can use GROUP BY without specifying any aggregate functions in the SELECT list. GRP_B order by grp_a. GRP_A ---------a1 a1 a2 GRP_B ---------b1 b2 b3 . COUNT(*) MAX(VAL) SUM(VAL) ---------. select grp_a.---------8 50 216 The last example is equivalent to specifying no GROUP BY clause at all.---------.---------b1 2 20 30 b2 3 50 120 b3 3 32 66 The GROUP BY expression list may be empty.---------. select count(*).

According to Tom Kyte the two approaches are effectively equivalent (see AskTom "DISTINCT VS. grp_b from t union all select distinct grp_a. For example. grp_b ) having grouping_id( grp_a. select grp_a. Queries that use DISTINCT are typically easier to understand. null as grp_b from t union all select distinct null. to produce a result set that is the union of: • distinct values in GRP_A • distinct values in GRP_B • distinct values in GRP_A + GRP_B the following query would be required if we used DISTINCT select distinct grp_a. 2 . grp_b ) != 3 order by 1. but the GROUP BY approach can provide an elegant solution to otherwise cumbersome queries when more than one set of groupings is required. GROUP BY"). GRP_A ---------a1 a1 a1 a2 a2 GRP_B ---------b1 b2 b3 b1 b2 b3 but a GROUP BY query could produce the same result with fewer lines of code. grp_b from t order by 1. GRP_A ---------a1 a1 a1 a2 a2 GRP_B ---------b1 b2 b3 b1 b2 b3 (We will learn about the CUBE and GROUPING_ID features later in this tutorial. grp_b from t group by cube( grp_a. 2 .) GROUP BY and Ordering .

---------b1 2 b2 3 b3 3 returns the same results as this one. . count(*) from t group by grp_a.of the Setup topic insert into t values ( 'a2' . 'd2'. What happens if we use no ORDER BY clause at all? select grp_a. count(*) from t GROUP BY GRP_A.columns have been reversed order by grp_a. changing the order in which columns appear in the GROUP BY clause has no effect on the way the result set is grouped. 'c2'. GRP_A ---------a1 a1 a2 GRP_B COUNT(*) ---------. This is an illusion which is easily proved with the following snippet. insert into t values ( 'a2' . grp_b . 'c2'. GRP_B order by grp_a. 'b3' .---------b1 2 b2 3 b3 3 Gotcha: GROUP BY with no ORDER BY The last two snippets used the same ORDER BY clause in both queries. Some programmers interpret this as meaning that GROUP BY returns an ordered result set. 'b3' . '12' ) . count(*) from t GROUP BY GRP_B. For example. '22' ) . grp_b. Note how the same query now returns rows in a random order given new conditions. insert into t values ( 'a2' .All other things being equal. -. grp_b . grp_b. truncate table t. this query select grp_a. GRP_A ---------a1 a1 a2 GRP_B COUNT(*) ---------. 'd2'.---------b1 2 b2 3 b3 3 The results are still ordered.this time we insert rows into T using a different order from that -. 'c2'. GRP_A -----a1 a1 a2 GRP_B COUNT(*) -----. 'b3' . GRP_A -. select grp_a. '32' ) . grp_b . grp_b. 'd2'.

'd1'.) Duplicate Columns If a column is used more than once in the SELECT clause it does not need to appear more than once in the GROUP BY clause. 'c2'. . upper(grp_a). 'd1'. count(*) from t group by GRP_A order by grp_a . . To order the groupings. "The GROUP BY clause groups rows but does not guarantee the order of the result set.---------a1 A1 5 . select grp_a. GRP_A order by grp_a . 'c1'. . Group by behavior in 10GR2 for another discussion of this issue. use the ORDER BY clause. 'd1'. 'c1'. 'c1'. . 'c1'. GRP_A ---------a1 a2 UPPER(GRP_ COUNT(*) ---------. .(your results may vary) The actual behaviour of GROUP BY without ORDER BY is documented in the SQL Reference Manual as follows. . GRP_A -----a1 a1 a2 GRP_B COUNT(*) -----." (See AskTom . into into into into into t t t t t values values values values values ( ( ( ( ( 'a1' 'a1' 'a1' 'a1' 'a1' .insert insert insert insert insert commit. . '50' '40' '30' '20' '10' ) ) ) ) ) . grp_b .---------. select grp_a. grp_b. GRP_A UPPER(GRP_ COUNT(*) ---------. 'd1'. 'd1'. count(*) from t group by grp_a. .---------b2 3 b1 2 b3 3 -. count(*) from t group by GRP_A. upper(grp_a). . . select grp_a. . 'b2' 'b2' 'b2' 'b1' 'b1' .---------A1 5 A2 3 If we did include the same column two or more times in the GROUP BY clause it would return the same results as the query above. .

as with GRP_A in this example. count(*) from t group by grp_a.a2 A2 3 While there is no practical use for the latter syntax in the upcoming topic GROUP_ID we will see how duplicate columns in a GROUPING SETS clause do produce different results than a distinct column list. GRP_A COUNT(*) -----. select GRP_A. 123 ---------123 123 123 'XY --XYZ XYZ XYZ SYSDATE ---------2009-06-07 2009-06-07 2009-06-07 GRP_A -----a1 a1 a2 GRP_B COUNT(*) -----. 'XYZ'. grp_b order by grp_a. SYSDATE.---------b1 2 b2 3 b3 3 and we can GROUP BY constant columns that are not in the SELECT list. count(*) from t . like GRP_B in the example below. grp_b. count(*) from t GROUP BY GRP_B . grp_a. 'XYZ'. SELECT Lists We may group by table columns that are not in the SELECT list. grp_b . select GRP_A. select grp_a. grp_a. As with table based columns we can include constant columns in the GROUP BY clause select 123. count(*) * ERROR at line 1: ORA-00979: not a GROUP BY expression Constants The rules for columns based on constant expressions differ slightly from those for table columns. grp_b .---------a1 2 a1 3 a2 3 However we may not select table columns that are absent from the GROUP BY list. grp_b. GRP_B order by grp_a. SYSDATE. select grp_a. count(*) from t group by 123.

'XYZ'. count(*) from t GROUP BY GRP_A. HAVING When Oracle processes a GROUP BY query the WHERE clause is applied to the result set before the rows are grouped together. however. 'b3' ) group by grp_a order by grp_a . the following will not work. grp_b. 'XYZ'.---------b1 2 b2 3 b3 3 Note how all three queries returned the same number of rows. prevent us from using conditions that involve aggregate values like COUNT(*) that are calculated after the GROUP BY clause is applied. grp_b . GRP_B order by grp_a.---------b1 2 b2 3 b3 3 Unlike table based columns we can select constant columns that are absent from the GROUP BY list.group by 123. grp_b . grp_a. For example. select grp_a. select grp_a. SYSDATE. grp_a. 123 ---------123 123 123 'XY --XYZ XYZ XYZ SYSDATE ---------2009-06-07 2009-06-07 2009-06-07 GRP_A -----a1 a1 a2 GRP_B COUNT(*) -----. This allows us to use WHERE conditions involving columns like GRP_B in the query below. grp_b order by grp_a. GRP_A COUNT(*) ---------. which is not listed in the GROUP BY clause. SYSDATE.---------a1 3 a2 3 Thia does. select 123. count(*) from t WHERE COUNT(*) > 4 group by grp_a order by grp_a . GRP_A -----a1 a1 a2 GRP_B COUNT(*) -----. WHERE COUNT(*) > 4 * ERROR at line 3: . count(*) from t WHERE GRP_B in ( 'b2'.

count(*) from t group by grp_a HAVING COUNT(*) > 4 order by grp_a .---------a2 3 but doing so yields the same result as using a WHERE clause.---------a2 3 .---------a1 5 Note that the HAVING clause cannot reference table columns like VAL that are not listed in the GROUP BY clause. select grp_a. GRP_A COUNT(*) ---------. on the other hand. count(*) from t group by grp_a HAVING VAL > 5 order by grp_a . GRP_A COUNT(*) ---------. reference table columns like GRP_A that are in the GROUP BY clause. select grp_a. select grp_a. HAVING VAL > 5 * ERROR at line 4: ORA-00979: not a GROUP BY expression It can. count(*) from t WHERE GRP_A = 'a2' group by grp_a order by grp_a . GRP_A COUNT(*) ---------. count(*) from t group by grp_a HAVING GRP_A = 'a2' order by grp_a .ORA-00934: group function is not allowed here For these types of conditions the HAVING clause can be used. select grp_a.

if not all. 2 .---------b1 2 b2 3 b3 3 UNION ALL could be used. grp_b. cases. GRP_B COUNT(*) ---------. GRP_A NULL COUNT(*) ---------. count(*) from t GROUP BY GROUPING SETS ( GRP_A. count(*) from t group by grp_b order by grp_b . GROUPING SETS There are times when the results of two or more different groupings are required from a single query.---------a1 5 a2 3 select grp_b. select grp_a. GRP_A COUNT(*) ---------.Given a choice between the last two snippets I expect using a WHERE clause provides the best performance in most. count(*) from t group by grp_a order by grp_a . count(*) from t group by grp_a UNION ALL select null. grp_b. GRP_B ) .---------a1 5 a2 3 b1 2 b2 3 b3 3 but as of Oracle 9i a more compact syntax is available with the GROUPING SETS extension of the GROUP BY clause. For example. With it the last query can be written as follows. count(*) from t group by grp_b order by 1. select grp_a. null.---------. like this select grp_a. say we wanted to combine the results of these two queries.

grp_b . GRP_A ---------a1 a1 a2 GRP_B COUNT(*) ---------. GRP_B ) order by grp_a. grp_b. grp_b . grp_b .---------. grp_b ) in the next query. grp_b. GRP_B order by grp_a. count(*) from t GROUP BY GRP_A order by grp_a . null. select grp_a. GRP_A ---------a1 a1 a2 GRP_B COUNT(*) ---------. count(*) from t GROUP BY ( GRP_A. count(*) from t GROUP BY GRP_A. GRP_A GRP_B COUNT(*) ---------.order by grp_a. GROUPING SETS..---------b1 2 b2 3 b3 3 select grp_a. and Empty Sets Composite Columns You can treat a collection of columns as an individual set by using parentheses in the GROUPING SETS clause. GRP_B).---------a1 5 a2 3 b1 2 b2 3 b3 3 It is important to understand how the clause grouping sets( grp_a. GRP_A N COUNT(*) ---------. Composite Columns. grp_b ) used in the last query differs from the clause group by ( grp_a. to write a query that returns the equivalent of these two queries select grp_a. For example.---------a1 5 a2 3 .---------b1 2 b2 3 b3 3 Note how the last query returned different rows than the GROUPING SETS query did even though both used the term (GRP_A.

select grp_a. GRP_B) is no different than the same expression without parentheses. grp_b. count(*) from t GROUP BY GROUPING SETS ( (GRP_A. count(*) from t GROUP BY GROUPING SETS ( (GRP_A. grp_b. grp_b. or CUBE clause. GRP_B).---------b1 2 b2 3 b3 3 8 Gotcha . GRP_A ---------a1 a1 a2 GRP_B COUNT(*) ---------. GRP_A ---------a1 a1 a1 a2 a2 GRP_B COUNT(*) ---------. GRP_A order by grp_a. grp_b . GRP_A ---------a1 a1 a2 GRP_B COUNT(*) ---------.---------b1 2 b2 3 b3 3 returns the same results as this query . GRP_B). In the example below the last row is generated by the empty set grouping. GRP_B) is called a "composite column" when it appears inside a GROUPING SETS. For example this query select grp_a. GRP_B). count(*) from t GROUP BY (GRP_A. ROLLUP.Parentheses without GROUPING SETS Outside a GROUPING SETS clause (or ROLLUP or CUBE clauses) a parenthesized expression like (GRP_A. () ) order by grp_a.we could use the following GROUPING SETS clause. grp_b .---------b1 2 b2 3 5 b3 3 3 The term (GRP_A. grp_b . can be used. select grp_a. Empty Sets To add a grand total row to the result set an empty set. GRP_A ) order by grp_a. specified as ().

I later learnt that an empty set term. GRP_A GRP_B COUNT(*) ---------. grp_b . grp_b. GRP_A order by grp_a. "()". count(*) from t GROUP BY GROUPING SETS ( GRP_A. grp_b .---------b1 2 b2 3 b3 3 which in turn has the same result set as this one. GRP_B. GRP_B order by grp_a. GRP_A ---------a1 a1 a2 GRP_B COUNT(*) ---------. count(*) from t GROUP BY GRP_A. select grp_a. grp_b . GRP_A ---------a1 a1 a2 GRP_B COUNT(*) ---------.---------a1 5 a2 3 b1 2 b2 3 b3 3 8 The last row in the result set is generated by the "0" grouping. 0 ) order by grp_a. both approaches seemed to produce the same results.select grp_a.---------b1 2 b2 3 b3 3 Gotcha: GROUPING SETS with Constants When I first started using GROUPING SETS I used constants to produce grand total rows in my result sets. .---------. grp_b . GRP_B. select grp_a . count(*) from t GROUP BY GRP_A. grp_b. like this. After all. was actually a more appropriate syntactic choice than a constant but I continued to use constants out of habit.

count(*) from t GROUP BY GROUPING SETS ( GRP_A. I later ran into a case where the two actually produced different results. 0 ) order by order by grp_a.---------. count(*) . . 1. grp_b . grp_b grp_a.---------(null) 5 (null) 3 1 2 1 3 1 3 0 8 Note how Query 2 returns "(null)" in the NVL2_GRP_B column and Query 1 does not. GRP_A -----a1 a2 (null) (null) (null) (null) GRP_B NVL2_GRP_B COUNT(*) -----.---------a1 5 a2 3 b1 2 b2 3 b3 3 8 However.---------(null) 0 5 (null) 0 3 b1 1 2 b2 1 3 b3 1 3 (null) 0 8 GRP_A -----a1 a2 (null) (null) (null) (null) GRP_B -----(null) (null) b1 b2 b3 (null) NVL2_GRP_B COUNT(*) ---------. () ) GROUPING SETS ( GRP_A. count(*) from from t t GROUP BY GROUP BY GROUPING SETS ( GRP_A. grp_b . grp_b . GRP_B. nvl2( grp_b.---------. 0 ) nvl2_grp_b . grp_b . GRP_A GRP_B COUNT(*) ---------. nvl2( grp_b. This is because "0" appears in both the SELECT list and the GROUP BY clause.select grp_a . Query 1 set null '(null)' Query 2 set null '(null)' select select grp_a grp_a . 1. Readers who want to understand more about why these two queries differ can reverse engineer the two into their . GRP_B. GRP_B. 0 ) nvl2_grp_b . grp_b . () ) order by grp_a.

count(*) from t group by grouping sets ( ( grp_a. count(*) from t . When aggregates are required at each level a query like this can be used. grp_c . 3 . set null '(null)' select grp_a . do not use a constant. grp_b . grp_b ) . () ) order by 1. Readers who don't simply need to remember this rule of thumb .always use an empty set term to generate a grand total row. ROLLUP It often happens that a query will have a group A which is a superset of group B which in turn is a superset of group C. select grp_a .---------c1 2 (null) 2 c1 2 c2 1 (null) 3 (null) 5 c2 3 (null) 3 (null) 3 (null) 8 This arrangement is common enough that SQL actually provides a shortcut for specifying these types of GROUPING SETS clauses. GRP_A ---------a1 a1 a1 a1 a1 a1 a2 a2 a2 (null) GRP_B ---------b1 b1 b2 b2 b2 (null) b3 b3 (null) (null) GRP_C COUNT(*) ---------.UNION ALL equivalents using the instructions at Reverse Engineering GROUPING BY Queries. 2. Here is how the query above looks when implemented with ROLLUP. grp_b. grp_c . ( grp_a. It uses the ROLLUP operator. grp_c ) . ( grp_a ) . grp_b .

GRP_A ---------a1 a1 a1 a1 a1 GRP_B ---------b1 b1 b2 b2 b2 GRP_C COUNT(*) ---------. GRP_A ---------a1 a1 a1 a1 a1 a1 a2 a2 a2 (null) GRP_B ---------b1 b1 b2 b2 b2 (null) b3 b3 (null) (null) GRP_C COUNT(*) ---------. count(*) from t group by grouping sets ( ( grp_a. ( grp_a ) . grp_c ) . grp_b ) . 3 . ( grp_c ) . as in this query. 2. ( grp_a. GRP_C ) order by 1. () ) order by 1. ( grp_b ) . grp_c ) . 3 .---------c1 2 (null) 2 c1 2 c2 1 (null) 3 .group by ROLLUP( GRP_A. set null '(null)' select grp_a . ( grp_b. 2. GRP_B. ( grp_a.---------c1 2 (null) 2 c1 2 c2 1 (null) 3 (null) 5 c2 3 (null) 3 (null) 3 (null) 8 CUBE There are times when all combinations of a collection of grouping columns are required. grp_c . grp_c ) . grp_b . grp_b.

GRP_C ) order by 1. GRP_A ---------a1 a1 a1 a1 a1 a1 a1 a1 a2 a2 a2 a2 (null) (null) (null) (null) (null) (null) (null) (null) GRP_B ---------b1 b1 b2 b2 b2 (null) (null) (null) b3 b3 (null) (null) b1 b1 b2 b2 b2 b3 b3 (null) GRP_C COUNT(*) ---------. 3 .a1 a1 a1 a2 a2 a2 a2 (null) (null) (null) (null) (null) (null) (null) (null) (null) (null) (null) (null) (null) b3 b3 (null) (null) b1 b1 b2 b2 b2 b3 b3 (null) (null) (null) c1 c2 (null) c2 (null) c2 (null) c1 (null) c1 c2 (null) c2 (null) c1 c2 (null) 4 1 5 3 3 3 3 2 2 2 1 3 3 3 4 4 8 This arrangement is common enough that SQL provides a shortcut called the CUBE operator to implement it.---------c1 2 (null) 2 c1 2 c2 1 (null) 3 c1 4 c2 1 (null) 5 c2 3 (null) 3 c2 3 (null) 3 c1 2 (null) 2 c1 2 c2 1 (null) 3 c2 3 (null) 3 c1 4 . select grp_a . grp_b . grp_c . Here is how the query above looks after re-writing it to use CUBE. 2. count(*) from t group by CUBE( GRP_A. GRP_B.

set null '(null)' select grp_a .---------- . grp_b ) . count(*) from t group by grouping sets ( ( grp_a. grp_b . grp_c ) ) order by 1. With it one can re-write a query like this one. GRP_A GRP_B GRP_C COUNT(*) ---------. 3 . 2. grp_c ) order by 1.---------. ( grp_a.---------.---------. 2.(null) (null) (null) (null) c2 (null) 4 8 Concatenated Groupings The last type of grouping shortcut we will examine is called a Concatenated Grouping. 3 . GRP_A ---------a1 a1 a1 a1 a2 a2 GRP_B GRP_C COUNT(*) ---------. grp_b . count(*) from t group by grp_a . grp_c . select grp_a .---------b1 2 b2 3 c1 4 c2 1 b3 3 c2 3 into one like this. grouping sets( grp_b. which effectively performs a cross-product of GRP_A with GRP_B and GRP_C. grp_c .

grp_c . count(*) from t group by grouping sets( grp_a. ( grp_a. grp_b ) . ( grp_b. 2. GRP_A ---------a1 a1 a1 a2 a2 (null) (null) (null) (null) (null) (null) (null) ) ) ) ) GRP_B ---------(null) (null) (null) (null) (null) b1 b1 b2 b2 b2 b3 b3 GRP_C COUNT(*) ---------. count(*) from t group by grouping sets ( ( grp_a. grp_d .---------c1 4 c2 1 (null) 5 c2 3 (null) 3 c1 2 (null) 2 c1 2 c2 1 (null) 3 c2 3 (null) 3 is re-written into one like this. grp_b .a1 a1 a1 a1 a2 a2 b1 b2 (null) (null) b3 (null) (null) (null) c1 c2 (null) c2 2 3 4 1 3 3 The cross-product effect is more apparent when a query like this one select grp_a . grp_d ) order by 1. grp_d ) order by . grp_b . grouping sets( grp_c. select grp_a . 3 . grp_c . ( grp_b. grp_c . grp_c .

GROUP_ID Unlike a regular GROUP BY clause. count(*) from t GROUP BY GROUPING SETS ( GRP_A. GRP_A ) order by grp_a .---------a1 5 a1 5 a1 5 a2 3 a2 3 a2 3 . Concatenated groupings can. See Concatenated Groupings for more information. GRP_A COUNT(*) ---------. GRP_A ) order by grp_a . GRP_A COUNT(*) ---------. I find that specifically listing the desired groupings in a single GROUPING SETS clause or using a single ROLLUP or CUBE operator makes my queries easier to understand and debug.. GRP_A. 2. 3 GRP_B ---------(null) (null) (null) (null) (null) b1 b1 b2 b2 b2 b3 b3 GRP_C COUNT(*) ---------. prove useful in data warehouse queries that deal with hierarchical cubes of data. however.---------c1 4 c2 1 (null) 5 c2 3 (null) 3 c1 2 (null) 2 c1 2 c2 1 (null) 3 c2 3 (null) 3 GRP_A ---------a1 a1 a1 a2 a2 (null) (null) (null) (null) (null) (null) (null) Personally I have never found the need to use concatenated groupings. count(*) from t GROUP BY GROUPING SETS ( GRP_A. 1. select grp_a. including the same column more than once in a GROUPING SETS clause produces duplicate rows.---------a1 5 a1 5 a2 3 a2 3 select grp_a.

GROUP_ID() from t GROUP BY GROUPING SETS ( GRP_A. There are times when more complex GROUP BY clauses can return duplicate rows however.---------.---------X1 10 X2 40 (null) 20 (null) 30 (null) 50 . count(*). group_id() .---------a1 5 0 a2 3 0 b1 2 0 b2 3 0 b3 3 0 Grouping by NULL Values In the examples used thus far in the tutorial our base table had no null values in it.The GROUP_ID function can be used to distinguish duplicates from each other. GRP_A. GRP_B ) order by grp_a. count(*). select grp_a.---------a1 5 0 a1 5 1 a1 5 2 a2 3 0 a2 3 1 a2 3 2 In the trivial example above it seems there would be little practical use for GROUP_ID. GRP_A GRP_B COUNT(*) GROUP_ID() ---------. set null '(null)' select * from t2 order by grp_a. GRP_A ---------A1 A1 A1 A1 A1 GRP_B VAL ---------. GROUP_ID() from t GROUP BY GROUPING SETS ( GRP_A. It is in such queries that GROUP_ID proves useful. select grp_a. grp_b .---------. grp_b . GRP_A COUNT(*) GROUP_ID() ---------. GRP_A ) order by grp_a. Let's now look at grouping a table that does contain null values.---------. Note that GROUP_ID will always be 0 in a result set that contains no duplicates. grp_b.

A2 (null) 60 Now consider the following GROUP BY query. count(*) from t2 GROUP BY GROUPING SETS( (GRP_A. count(*) from t2 GROUP BY GROUPING SETS( (GRP_A. select grp_a. 1. grp_b order by grp_a. GRP_A ---------A1 A1 A1 A1 A2 GRP_B TEST COUNT(*) ---------.grp_b. grp_b. GRP_A ---------A1 A1 A1 A2 GRP_B COUNT(*) ---------. grp_b . NVL( t2. but let's use GROUPING SETS next and see what happens. GRP_B). nvl2( t2.---------. GRP_A ) order by grp_a. 'n/a' ) AS GRP_B .GRP_B.NVL and NVL2 One might expect that NVL() or NVL2 could be used to distinguish the two nulls.---------X1 1 X2 1 (null) 3 (null) 1 So far so good. GRP_A ) order by grp_a. grp_b. GRP_B). one representing the null values stored in T2. count(*) from t2 group by grp_a. select grp_a.GRP_B. GRP_A ---------A1 A1 A1 A1 A2 A2 GRP_B COUNT(*) ---------.---------X1 1 X2 1 (null) 3 (null) 5 (null) 1 (null) 1 We now have two rows with "(null)" under GRP_B for each GRP_A value. grp_b .---------X1 1 1 X2 1 1 n/a 0 5 n/a 0 3 n/a 0 1 . like this select grp_a . Gotcha . grp_b . 0 ) as test .GRP_B and the other representing the set of all values in T2.

grp_b). In the output of the following query two of the four nulls represent the set of all GRP_B values. select grp_a as "Group A" . set null '(null)' select grp_a . Here is one example. ROLLUP. GROUPING can be used with DECODE to insert labels like "Total" into the result set. GROUPING The GROUPING function tells us whether or not a null in a result set represents the set of all values produced by a GROUPING SETS.---------. 2 . count(*) as "Count" . grp_b ) as "Group B" . 1. 'Total:' . In the next topic we see how the GROUPING function can help us distinguish the two types of nulls. A value of "1" tells us it does. GRP_A ---------A1 A1 A1 A1 A2 A2 GRP_B COUNT(*) GROUPING_GRP_A GROUPING_GRP_B ---------. not before.A2 n/a 0 1 but this is not the case because functions in the SELECT list operate on an intermediate form of the result set created after the GROUP BY clause is applied.-------------X1 1 0 0 X2 1 0 0 (null) 3 0 0 (null) 5 0 1 (null) 1 0 0 (null) 1 0 1 Of course adding a column with zeros and ones to a report isn't the most user friendly way to distinguish grouped values.-------------. GROUPING( GRP_A ) GROUPING_GRP_A . GROUPING( GRP_B ) GROUPING_GRP_B from t2 group by grouping sets( (grp_a. or CUBE operation. grp_b . However. a value of "0" tells us it does not. count(*) . grp_a ) order by 1 . decode ( GROUPING( GRP_B ) .

---------X1 1 X2 1 (null) 3 Total: 5 (null) 1 Total: 1 Nulls and Aggregate Functions In this topic we explored working with null values in GROUP BY columns. count(*) from t2 group by grouping sets( (grp_a. select grp_a . 1. To work around the error we can either prefix the column name with its table name select . but Oracle interprets it as referring to the column alias. decode( grouping( grp_b ). grp_b). Gotcha . hence the ORA00979 error. decode( grouping( grp_b ). In the ORDER BY GROUPING( GRP_B ) clause one might expect the "GRP_B" term to refer to the table column. say we attempted this query. grp_b ) AS GRP_B * ERROR at line 3: ORA-00979: not a GROUP BY expression Note how the table has a column called GRP_B and the SELECT list has a column alias also called GRP_B. GROUPING( GRP_B ) .ORA-00979 When using ORDER BY we need to be careful with the selection of column aliases. grp_b). . Group A ---------A1 A1 A1 A1 A2 A2 Group B Count ---------. grp_b ) AS GRP_B . To learn how aggregate functions like COUNT() and SUM() deal with null values in non-GROUP BY columns see Nulls and Aggregate Functions. grp_b . 1. 'Total:'.from t2 group by grouping sets( (grp_a. grp_a ) order by grouping( GRP_B ) . 'Total:'. grp_a ) order by grp_a . For example.

What if we wanted to distinguish entire rows from each other? We could use a number of different GROUPING() calls like this column bit_vector format a10 select TO_CHAR( GROUPING( GRP_A ) ) || TO_CHAR( GROUPING( GRP_B ) ) AS BIT_VECTOR . count(*) as "Count" from t2 group by grouping sets( (grp_a. DECODE ( TO_CHAR( GROUPING( GRP_A ) ) || TO_CHAR( GROUPING( GRP_B ) ) . grp_a ) order by grouping( GRP_B ) . decode( grouping( grp_b ). or CUBE operation. '01'. ROLLUP. grp_b). 1. count(*) from t2 group by grouping sets( (grp_a. grp_b ) AS GRP_B . grp_a ) order by grouping( T2.---------(null) 3 X1 1 X2 1 (null) 1 Total: 5 Total: 1 or change the column alias. 'Total:'. decode( grouping( grp_b ). 'Group "' || GRP_A || '" Total' . grp_b ) AS "Group B" . grp_b). 'Total:'.grp_a .GRP_B ) . select grp_a as "Group A" . GRP_A ---------A1 A1 A1 A2 A1 A2 GRP_B COUNT(*) ---------.---------(null) 3 X1 1 X2 1 (null) 1 Total: 5 Total: 1 GROUPING_ID In the preceding topic we saw how the GROUPING function could be used to identify null values representing the set of all values produced by a GROUPING SETS. 1. Group A ---------A1 A1 A1 A2 A1 A2 Group B Count ---------.

---------.---------. grp_b. '10'. . count(*) from t2 group by grouping sets ( grp_a. () order by GROUPING( GRP_A ) .GRP_B) GRP_A GRP_B COUNT(*) ---------.---------01 1 A1 5 01 1 A2 1 10 2 X1 1 10 2 X2 1 10 2 4 . GROUPING( GRP_B ) . () ) order by GROUPING_ID( GRP_A. GRP_B ) . grp_b . GRP_B ) . grp_b . grp_b. BIT_VECTOR GROUPING_ID(GRP_A. grp_a . grp_b . . grp_a . grp_a . It yields the decimal value of a bit vector (a string of zeros and ones) formed by concatenating all the GROUPING values for its parameters.this column is only included for clarity . select to_char( grouping( grp_a ) ) || to_char( grouping( grp_b ) ) as bit_vector -. GROUPING_ID( GRP_A. 'Group "' || GRP_B || '" Total' '11'. 'Grand Total' NULL AS LABEL count(*) from t2 group by grouping sets ( grp_a.. BIT_VECTOR ---------01 01 10 10 10 11 ) LABEL COUNT(*) -----------------------. Fortunately for us the GROUPING_ID function exists.-----------------------. The following example shows how it works.---------Group "A1" Total 5 Group "A2" Total 1 Group "X1" Total 1 Group "X2" Total 1 Group "" Total 4 Grand Total 6 but if the number of grouping sets were large concatenating all the required GROUPING() terms together would get cumbersome. ) .

GRP_B ) . NULL ) AS LABEL . GRP_B ) .---------Group "A1" Total 5 Group "A2" Total 1 Group "X1" Total 1 Group "X2" Total 1 Group "" Total 4 Grand Total 6 Composite Columns The following example shows how GROUPING_ID works when a composite column. grp_a .11 3 6 Here is how we could use GROUPING_ID to streamline our original query. is included in the GROUPING SETS clause. grp_b). () ) order by GROUPING_ID( GRP_A. GRP_B). (GRP_A. grp_a.---------X1 1 X2 1 . LABEL COUNT(*) -----------------------. () order by 1 . grp_a . grp_b. 2. 'Grand Total' . GRP_B ) . GROUPING_ID(GRP_A. 2 . count(*) from t2 group by grouping sets ( grp_a.GRP_B) -----------------------0 0 GRP_A ---------A1 A1 ) GRP_B COUNT(*) ---------. grp_b . grp_b. select GROUPING_ID( GRP_A. grp_b . 1. select DECODE ( GROUPING_ID( GRP_A. 3. count(*) from t2 group by grouping sets ( (grp_a. 'Group "' || GRP_A || '" Total' . 'Group "' || GRP_B || '" Total' . 3 .

grp_b . grp_b ) . we started with a query like this one select grouping_id( grp_a.---------X1 1 X2 1 3 1 5 1 X1 1 X2 1 4 6 and then we wanted to exclude the empty set grouping (the one with a GROUPING_ID of "3"). GRP_B ) != 3 order by . 3 . grp_a .GRP_B) -----------------------0 0 0 0 1 1 2 2 2 3 GRP_A ---------A1 A1 A1 A2 A1 A2 GRP_B COUNT(*) ---------. 2. select grouping_id( grp_a. Say. for example. grp_a . count(*) from t2 group by cube( grp_a. grp_b ) . grp_b . count(*) from t2 group by cube( grp_a. GROUPING_ID(GRP_A. grp_b ) order by 1. grp_b ) HAVING GROUPING_ID( GRP_A.0 0 1 1 2 2 2 3 A1 A2 A1 A2 X1 X2 3 1 5 1 1 1 4 6 GROUPING_ID and HAVING GROUPING_ID can also be used in the HAVING clause to filter out unwanted groupings. We simply add a HAVING clause as follows.

grp_b . GRP_B ) order by grp_a . This is not as easy as it first may seem. grp_b . grp_b . nvl2( grp_b. A query like this.---------b1 1 2 b2 1 3 a1 0 5 b3 1 3 a2 0 3 (null) 0 8 is not simply the result of unioning together three identical subqueries with different GROUP BY clauses. set null (null) select grp_a . GRP_A ---------a1 a1 a1 a2 a2 (null) GRP_B ---------b1 b2 (null) b3 (null) (null) NVL_GRP_A_ NVL2_GRP_B COUNT(*) ---------. grp_a ) as nvl_grp_a_b . for example. 2.---------X1 1 X2 1 3 1 5 1 X1 1 X2 1 4 GROUPING_ID(GRP_A. 1. 1.. nvl( grp_b. 3 GRP_A ---------A1 A1 A1 A2 A1 A2 GRP_B COUNT(*) ---------. grp_a ) as nvl_grp_a_b .---------. count(*) from t GROUP BY ROLLUP ( GRP_A. set null '(null)' select grp_a . 0 ) as nvl2_grp_b . nvl( grp_b.GRP_B) -----------------------0 0 0 0 1 1 2 2 2 Reverse Engineering GROUPING BY Queries At times we are faced with a complex GROUP BY query written by someone else and figuring out the equivalent UNION ALL query can help us better understand its results.

grp_b . grp_a * ERROR at line 2: ORA-00979: not a GROUP BY expression As you can see. grp_a ) as nvl_grp_a_b . 0 ) as nvl2_grp_b . nvl2( grp_b. nvl2( grp_b. In our example the query select grp_a . To determine the real equivalent UNION query we can use the following algorithm. such a query produces an error because the first and second subqueries select columns that are not in the GROUP BY clause. nvl( grp_b. grp_b . 0 ) as nvl2_grp_b . GRP_B ) order by . GRP_B ) order by grp_a . 0 ) as nvl2_grp_b . Step 1 Replace any ROLLUP or CUBE operators with their equivalent GROUPING SETS operator. count(*) from t GROUP BY ( GRP_A. nvl2( grp_b. 1. count(*) from t GROUP BY () UNION ALL select grp_a . nvl( grp_b. 0 ) as nvl2_grp_b . grp_b . 1.. nvl( grp_b. 1. 1. count(*) from t GROUP BY ( GRP_A ) UNION ALL select grp_a . grp_b . count(*) from t GROUP BY ROLLUP ( GRP_A. grp_a ) as nvl_grp_a_b . nvl2( grp_b. grp_a ) as nvl_grp_a_b .

GRP_A ---------a1 a1 a1 a2 a2 (null) GRP_B ---------b1 b2 (null) b3 (null) (null) NVL_GRP_A_ NVL2_GRP_B COUNT(*) ---------.---------. 1. ( GRP_A. nvl2( grp_b.---------b1 1 2 b2 1 3 a1 0 5 b3 1 3 a2 0 3 (null) 0 8 Step 2a Next start with a query that groups by only the first term in the GROUPING SETS clause. count(*) from t . nvl( grp_b. ( GRP_A ) . which is an empty set in our example. 0 ) as nvl2_grp_b . count(*) from t GROUP BY GROUPING SETS ( () . grp_a ) as nvl_grp_a_b . grp_b . nvl2( grp_b. grp_b . 0 ) as nvl2_grp_b . select grp_a . grp_b .grp_a . grp_b . GRP_A ---------a1 a1 a1 a2 a2 (null) GRP_B ---------b1 b2 (null) b3 (null) (null) NVL_GRP_A_ NVL2_GRP_B COUNT(*) ---------.---------. grp_a ) as nvl_grp_a_b . GRP_B ) ) order by grp_a . nvl( grp_b.---------b1 1 2 b2 1 3 a1 0 5 b3 1 3 a2 0 3 (null) 0 8 is replaced with select grp_a . 1.

-----. nvl2( NULL. nvl( NULL.---------. If the SELECT list contains columns that are not in the GROUP BY clause then replace those columns with NULL. 0 ) as nvl2_grp_b .---------(null) (null) (null) 0 8 Step 2b Now we repeat the first step using the second term in the GROUPING SETS clause. NULL ) as nvl_grp_a_b . GRP_A GRP_B NVL_GRP_A_B NVL2_GRP_B COUNT(*) -----. count(*) from t GROUP BY ( GRP_A ) . select grp_a . grp_b .---------. NULL as grp_b . count(*) from t GROUP BY ( GRP_A ) . count(*) from t GROUP BY () . column column column column grp_a grp_b nvl_grp_a_b nvl2_grp_b format format format format a6 a6 a11 999999999 select NULL as grp_a . 1.GROUP BY () .----------. nvl( grp_b.-----. nvl2( grp_b. grp_a ) as nvl_grp_a_b . 0 ) as nvl2_grp_b . NULL as grp_b .---------a1 (null) a1 0 5 . 1.----------. 1. grp_a ) as nvl_grp_a_b . nvl( NULL. select grp_a . In the query above both GRP_A and GRP_B are absent from the GROUP BY clause so we replace all occurrences of these columns in the SELECT list with NULL. 0 ) as nvl2_grp_b . This time GRP_B is in the SELECT list but it is not in the GROUP BY list. GRP_A GRP_B NVL_GRP_A_B NVL2_GRP_B COUNT(*) -----. nvl2( NULL. ( GRP_A ). We therefore need to replace GRP_B with NULL.

nvl2( NULL. count(*) from t group by ( grp_a ) UNION ALL select grp_a . grp_a ) as nvl_grp_a_b .a2 (null) a2 0 3 Step 2c For the last set in the GROUPING SETS clause all selected columns are listed in the GROUP BY clause so no further transformation is needed. nvl( grp_b. 0 ) as nvl2_grp_b . select NULL as grp_a . grp_b . nvl( grp_b. grp_a ) as nvl_grp_a_b . count(*) from t GROUP BY ( GRP_A. 0 ) as nvl2_grp_b . count(*) from . grp_a ) as nvl_grp_a_b . grp_b . 1. We can use the original SELECT list as-is.---------b1 1 2 b2 1 3 b3 1 3 Step 3 The next step is to combine the three step 2 queries with UNION ALL and add an ORDER BY clause. NULL ) as nvl_grp_a_b . NULL as grp_b . nvl( NULL. 0 ) as nvl2_grp_b . 1. count(*) from t group by () UNION ALL select grp_a . GRP_A -----a1 a1 a2 GRP_B -----b1 b2 b3 NVL_GRP_A_B NVL2_GRP_B COUNT(*) ----------. 1. 1. GRP_B ) . 0 ) as nvl2_grp_b . nvl2( grp_b. nvl( NULL. nvl2( grp_b.---------. NULL as grp_b . select grp_a . nvl2( NULL.

NULL AS NVL_GRP_A_B . GRP_A GRP_B NVL_GRP_A_B NVL2_GRP_B COUNT(*) . NULL ) and nvl2( NULL . select null as grp_a . nvl2( grp_b. GRP_B . 0 AS NVL2_GRP_B .---------b1 1 2 b2 1 3 a1 0 5 b3 1 3 a2 0 3 (null) 0 8 Step 4 (Optional) Lastly we reduce expressions like nvl( NULL. grp_b . 1. null as grp_b . 0 ) as nvl2_grp_b . equivalent terms. 1. grp_b ) order by grp_a . count(*) from t group by ( grp_a ) union all select grp_a . count(*) from t group by ( grp_a.t group by ( grp_a. count(*) from t group by () union all select grp_a . grp_b ) ORDER BY GRP_A . 0 ) to simpler. GRP_A -----a1 a1 a1 a2 a2 (null) GRP_B -----b1 b2 (null) b3 (null) (null) NVL_GRP_A_B NVL2_GRP_B COUNT(*) ----------. 0 AS NVL2_GRP_B . GRP_A AS NVL_GRP_A_B . nvl( grp_b. grp_a ) as nvl_grp_a_b . grp_b . null as grp_b .---------.

. 'c2'. '10' '20' '30' '40' '50' '12' ) ) ) ) ) ) . create table t ( grp_a varchar2(10) . . .---------. 'd1'. . val number ) . 'c1'. nvl( grp_b. grp_b . 1.---------b1 1 2 b2 1 3 a1 0 5 b3 1 3 a2 0 3 (null) 0 8 Setup Run the code on this page in SQL*Plus to create the sample tables. . . grp_c varchar2(10) . 'd1'. etc.---------b1 1 2 b2 1 3 a1 0 5 b3 1 3 a2 0 3 (null) 0 8 Result The end result of the last step is a query which returns the same rows as the original GROUPING SETS query. select grp_a . 'b1' 'b1' 'b2' 'b2' 'b2' 'b3' . 'c1'. . nvl2( grp_b. GRP_B ) order by grp_a . . 'd2'. grp_a ) as nvl_grp_a_b . . . 0 ) as nvl2_grp_b . . . . grp_b . data. 'c1'.---------. . grp_d varchar2(10) . 'c2'. 'd1'. . grp_b varchar2(10) . count(*) from t GROUP BY ROLLUP ( GRP_A. . which is repeated below for your convenience. insert insert insert insert insert insert into into into into into into t t t t t t values values values values values values ( ( ( ( ( ( 'a1' 'a1' 'a1' 'a1' 'a1' 'a2' . 'd1'. used by the examples in this section. 'd1'. 'c1'. GRP_A -----a1 a1 a1 a2 a2 (null) GRP_B -----b1 b2 (null) b3 (null) (null) NVL_GRP_A_B NVL2_GRP_B COUNT(*) ----------.-----a1 a1 a1 a2 a2 (null) -----b1 b2 (null) b3 (null) (null) ----------.

. COLUMN. drop table t2 . '22' ) . . . procedures. . 'c2'. . . 'b3' . '32' ) .g. 'd2'. and VARIABLE commands) exit your SQL*Plus session after running these cleanup commands. . '10' '40' '20' '30' '50' '60' ) ) ) ) ) ) . those made by SET. . . exit . create table t2 ( grp_a varchar2(10) . . 'c2'. . created in earlier parts of this section. grp_b varchar2(10) . val number ) . .insert into t values ( 'a2' . Cleanup Run the code on this page to drop the sample tables. 'd2'. commit . insert into t values ( 'a2' . . insert insert insert insert insert insert into into into into into into t2 t2 t2 t2 t2 t2 values values values values values values ( ( ( ( ( ( 'A1' 'A1' 'A1' 'A1' 'A1' 'A2' . . . commit . 'X1' 'X2' null null null null . 'b3' . etc. To clear session state changes (e. drop table t .

KEY ---------nls demo mesg server bin config log ctx admin data delx enlx eslx mig PARENT_KEY ---------(null) nls nls (null) server server config server ctx ctx data data data ctx It is often useful to order and display such rows using the hierarchical relationship. /nls /nls/demo /nls/mesg . An example of hierarchical data is shown below. Connecting Rows Say we wanted to take the following directory names from a file system and store them in a database table. Doing so yields a result set that looks like this (KEY values are indented to highlight the hierarchy).Hierarchical Data This section presents various topics related to hierarchical data (also known as "tree structured" data). KEY_INDENTED --------------nls demo mesg server bin config log ctx admin data delx enlx eslx mig KEY_PATH ------------------------/nls /nls/demo /nls/mesg /server /server/bin /server/config /server/config/log /server/ctx /server/ctx/admin /server/ctx/data /server/ctx/data/delx /server/ctx/data/enlx /server/ctx/data/eslx /server/ctx/mig In this tutorial we explore various Oracle mechanisms for working with hierarchical data.

The following snippet returns rows sorted hierarchically. . We are bending the rules here for illustrative purposes.) select * from t . and a PARENT_KEY column./server /server/bin /server/config /server/config/log /server/ctx /server/ctx/admin /server/ctx/data /server/ctx/data/delx /server/ctx/data/enlx /server/ctx/data/eslx /server/ctx/mig To do this we could use a table with a KEY column. START WITH identifies the topmost rows in the hierarchy. which holds the directory name. which connects the directory to its parent directory. KEY ---------nls demo mesg server bin config log ctx admin data delx enlx eslx mig PARENT_KEY ---------(null) nls nls (null) server server config server ctx ctx data data data ctx NAME ---------NLS DATA DEMO SERVER BIN CONFIG LOG CTX ADMIN DATA DELX ENLX ESLX MESG To connect and order the data in this table using the PARENT_KEY hierarchy we can create a Hierarchical Query using the START WITH and CONNECT BY clauses of the SELECT command. select key . CONNECT BY identifies all subsequent rows in the hierarchy. level from t START WITH parent_key is null CONNECT BY parent_key = prior key . starting from the root rows (those with no parents) on down through to the leaf rows (those with no children). (Directory names like these would not typically be used as primary keys.

level from t START WITH parent_key is null CONNECT BY parent_key = prior key .-----nls 1 demo 2 mesg 2 server 1 bin 2 config 2 log 3 ctx 2 admin 3 data 3 delx 4 enlx 4 eslx 4 mig 3 The PRIOR operator in hierarchical queries gives us access to column information from the parent of the current row. .-----nls 1 demo 2 mesg 2 server 1 bin 2 config 2 log 3 ctx 2 admin 3 data 3 delx 4 enlx 4 eslx 4 mig 3 The LEVEL pseudocolumn in the previous result indicates which level in the hierarchy each row is at. KEY_INDENTED LEVEL --------------. level-1 ) || key as key_indented . select lpad( ' '. like this. It can be used outside the CONNECT BY clause if required. To better illustrate hierarchical relationships the LEVEL column is commonly used to indent selected values. level-1 ) || key as key_indented .KEY LEVEL ---------. select lpad( ' '. The topmost level is assigned a LEVEL of 1.

level from t START WITH KEY = 'delx' connect by key = PRIOR PARENT_KEY . PRIOR_NAME ---------(null) NLS NLS (null) SERVER SERVER CONFIG SERVER CTX CTX DATA DATA DATA CTX Changing Direction To traverse the tree in the opposite direction. KEY_INDENTED LEVEL --------------. level-1 ) || key as key_indented .PRIOR key PRIOR name from t start with parent_key is null connect by parent_key = prior key .-----delx 1 data 2 ctx 3 server 4 Gotchas . select lpad( ' '. KEY_INDENTED --------------nls demo mesg server bin config log ctx admin data delx enlx eslx mig PRIOR_KEY ---------(null) nls nls (null) server server config server ctx ctx data data data ctx as prior_key as prior_name . from leaf to root. simply choose a leaf row as the starting point and apply the PRIOR operator to the PARENT_KEY column instead of the KEY column.

The following two snippets demonstrate how this order of operations affects query results when filter conditions are in the WHERE clause versus when they are in the CONNECT BY clause. level-1 ) || key as key_indented . level-1 ) || key as key_indented . level from t --start with key = 'server' CONNECT BY parent_key = prior key and LEVEL != 3 .-----delx 1 data 2 ctx 3 server 4 Order of Operations The clauses in hierarchical queries are processed in the following order. Filter Condition in CONNECT BY select lpad(' '. 1. . join conditions (either in the FROM clause or the WHERE clause) 2. level-1 ) || key as key_indented . level from t WHERE LEVEL != 3 start with key = 'server' connect by parent_key = prior key -. CONNECT BY clause 4. WHERE clause conditions that are not joins. level from t start with key = 'delx' connect by key = PRIOR PARENT_KEY and KEY <> 'delx' . select lpad( ' '. In the following snippet note how the KEY <> 'delx' condition did not filter out the row with a KEY value of 'delx'.CONNECT BY conditions are not applied to rows in level 1 of the hierarchy. KEY_INDENTED LEVEL --------------. Filter Condition in WHERE select lpad(' '. START WITH clause 3.

Doing so would negate the need for START WITH and CONNECT BY in the first place. Regular Query select key from t ----ORDER BY NAME . . given data with the following hierarchies KEY_INDENTED --------------nls demo mesg server bin config log ctx admin data delx enlx eslx mig the ORDER BY clause in the hierarchical query on the left below destroys the hierarchical order. For example. in your hierarchical queries. Hierarchical Query select key from t start with parent_key is null connect by parent_key = prior key ORDER BY NAME .-----server 1 bin 2 config 2 ctx 2 Sorting Since START WITH and CONNECT BY apply a hierarchical sorting scheme to your data.-----server 1 bin 2 config 2 ctx 2 delx 4 enlx 4 eslx 4 KEY_INDENTED LEVEL --------------. you should generally not use any features that apply other sorting schemes. It yields the same results as if CONNECT BY was not used at all.KEY_INDENTED LEVEL --------------. such as ORDER BY or GROUP BY.

level-1) || key as key_indented from t start with parent_key is null connect by parent_key = prior key ORDER SIBLINGS BY KEY ASC . KEY_INDENTED --------------nls demo mesg server bin config log ctx admin data delx enlx eslx Descending Siblings select lpad(' '. KEY_INDENTED --------------server ctx mig data eslx enlx delx admin config log bin nls mesg . the ORDER SIBLINGS BY clause will not destroy the hierarchical ordering of queries. It allows you to control the sort order of all rows with the same parent (aka "siblings"). Ascending Siblings select lpad(' '. level-1) || key as key_indented from t start with parent_key is null connect by parent_key = prior key ORDER SIBLINGS BY KEY DESC . The following examples show how ORDER SIBLINGS BY can be used to sort siblings in ascending and descending order respectively.KEY ---------admin bin config ctx data demo delx mesg enlx eslx log mig nls server KEY ---------admin bin config ctx data demo delx mesg enlx eslx log mig nls server ORDER SIBLINGS BY Unlike ORDER BY and GROUP_BY.

etc. if v_parent_key is null then return ( p_separator || v_key ).Note: --. ). -----------------------------------------------------------create or replace function KEY_PATH ( p_key t. level-1) || key lpad(' '. exception when no_data_found then return( null ). else return ( KEY_PATH( v_parent_key.mig demo Oracle 8i and Earlier The ORDER SIBLINGS BY clause is only available in Oracle version 9i or greater.In a real application more robust code would be needed -.parent_key%type . / show errors No errors. end.key%type . p_separator varchar2 default '/' ) return varchar2 is v_parent_key t. ------------------------------------------------------------. -. level-1) || key as key_indented as key_indented . For earlier versions a custom. -. recursive PL/SQL function can be used in place of ORDER SIBLINGS BY.This function is only for demonstration purposes. v_key t key = p_key .key%type . p_separator ) || p_separator || v_key end if. hierarchical loops in the data. begin select into from where parent_key. v_key t.to guard against things like separator characters -.appearing in KEY values. Ascending Siblings Descending Siblings select select lpad(' '. key v_parent_key.

'~' ) should use a length larger than any possible KEY_PATH value ("50" in this example) and it should use a padding character that sorts higher than all characters contained in T. CONNECT_BY_ISLEAF The CONNECT_BY_ISLEAF pseudocolumn returns 1 if the current row is a leaf of the tree defined by the CONNECT BY condition.KEY ("~" in this example).KEY 2. KEY_INDENTED --------------server ctx mig data eslx enlx delx admin config log bin nls mesg demo Gotchas KEY_PATH's p_separator character should be a character that 1. sorts lower than all characters that exist in T.from t start with parent_key is null connect by parent_key = prior key ORDER BY KEY_PATH( KEY. '~' ) DESC .KEY For descending siblings the code RPAD( KEY_PATH( KEY. 50. Violating these rules can result in incorrectly sorted output. 50. CONNECT_BY_ISLEAF from t start with key = 'server' connect by parent_key = prior key . '/' ). does not exist in values under T. 0 otherwise. '/' ). '/' ) ASC . select lpad(' '. level-1 ) || key as key_indented . KEY_INDENTED --------------nls demo mesg server bin config log ctx admin data delx enlx eslx mig from t start with parent_key is null connect by parent_key = prior key ORDER BY RPAD( KEY_PATH( KEY.

KEY_INDENTED CONNECT_BY_ISLEAF --------------. in table T the rows with a KEY of 'config' and 'ctx' have descendents (children and grandchildren) and are therefore not leaf nodes in that context..----------------server 0 bin 1 config 1 ctx 1 As we saw happen with the LEVEL column in the preceding tutorial. those same rows are considered leaf nodes (they have a CONNECT_BY_ISLEAF value of 1) because none of the descendents exist in the tree as defined by the CONNECT BY clause. In the following query however. " LEVEL <= 2 " is placed in the WHERE clause. connect_by_isleaf from t start with key = 'server' CONNECT BY parent_key = prior key and LEVEL <= 2 -. causing CONNECT_BY_ISLEAF to be 0 for "config" and "ctx" even though those rows look like leaf nodes in the end result. level-1 ) || key as key_indented . select lpad(' '. not the CONNECT BY clause as above. select lpad(' '. not that of the underlying table data. For example. level-1 ) || key as key_indented . The following example illustrates this. KEY_INDENTED CONNECT_BY_ISLEAF --------------. In it. connect_by_isleaf . They are filtered out by the " LEVEL <= 2 " condition. the order of evaluation of the CONNECT BY and WHERE clauses can also affect the behaviour of the CONNECT_BY_ISLEAF pseudo column.filters out descendents of "config" and "ctx" .----------------server 0 bin 1 config 0 log 1 ctx 0 admin 1 data 0 delx 1 enlx 1 eslx 1 mig 1 It is important to recognize that CONNECT_BY_ISLEAF only considers the tree defined by the CONNECT BY condition.

select lpad(' '. name . KEY_INDENTED LEVEL KEY --------------.---------nls 1 nls demo 2 demo mesg 2 mesg server 1 server bin 2 bin config 2 config log 3 log ctx 2 ctx admin 3 admin data 3 data delx 4 delx enlx 4 enlx eslx 4 eslx mig 3 mig NAME ---------NLS DATA DEMO SERVER BIN CONFIG LOG CTX ADMIN DATA DELX ENLX ESLX MESG ROOT_KEY ---------nls nls nls server server server server server server server server server server server ROOT_NAME ---------NLS NLS NLS SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER SERVER Gotchas .filter out descendents of "config" and "ctx".from t WHERE -. CONNECT_BY_ROOT key as root_key .-----. LEVEL <= 2 -. CONNECT_BY_ROOT name as root_name from t start with parent_key is null connect by parent_key = prior key . KEY_INDENTED CONNECT_BY_ISLEAF --------------. key .this time using the WHERE clause start with key = 'server' connect by parent_key = prior key . level-1 ) || key as key_indented . level .----------------server 0 bin 1 config 0 ctx 0 CONNECT_BY_ROOT The CONNECT_BY_ROOT operator returns column information from the root row of the hierarchy.

level-1 ) || key name . SYS_CONNECT_BY_PATH The SYS_CONNECT_BY_PATH function returns a single string containing all the column values encountered in the path from root to node. level .---------nls 1 nls demo 2 nls mesg 2 nls server 1 server bin 2 server config 2 server log 3 server ctx 2 server admin 3 server data 3 server mig 3 server The fact that the query above contradicts the documentation yet works without error in 10g suggests a bug in either the documentation or the SQL engine. While there would be little use for CONNECT_BY_ROOT in the START WITH condition. CONNECT_BY_ROOT key as root_key from t start with parent_key is null connect by parent_key = prior key and not ( level > 3 and connect_by_root key = 'server' ) . . The Gotchas section of topic CONNECT BY LEVEL Method has an example where using CONNECT_BY_ROOT in CONNECT BY does not work so well. which already operates on the root row itself. select lpad(' '.-----. In the following example we use CONNECT_BY_ROOT in the CONNECT BY condition to prevent any rows beyond level 3 under only the "server" root row from being included in the results. using CONNECT_BY_ROOT in the CONNECT BY condition can be useful and. select lpad(' '. actually works in some cases (as tested in Oracle 10g).The manual page for CONNECT_BY_ROOT states You cannot specify this operator in the START WITH condition or the CONNECT BY condition. in practice. level-1 ) || key as key_indented . as key_indented . KEY_INDENTED LEVEL ROOT_KEY --------------.

p_separator varchar2 default '/' ) return varchar2 is v_parent_key t. -. KEY_INDENTED --------------nls demo mesg server bin config log ctx admin data delx enlx eslx mig NAME ---------NLS DATA DEMO SERVER BIN CONFIG LOG CTX ADMIN DATA DELX ENLX ESLX MESG as key_path as name_path . recursive PL/SQL function can be used in place of SYS_CONNECT_BY_PATH. hierarchical loops in the data. -. KEY_PATH ------------------------/nls /nls/demo /nls/mesg /server /server/bin /server/config /server/config/log /server/ctx /server/ctx/admin /server/ctx/data /server/ctx/data/delx /server/ctx/data/enlx /server/ctx/data/eslx /server/ctx/mig NAME_PATH ------------------------/NLS /NLS/DATA /NLS/DEMO /SERVER /SERVER/BIN /SERVER/CONFIG /SERVER/CONFIG/LOG /SERVER/CTX /SERVER/CTX/ADMIN /SERVER/CTX/DATA /SERVER/CTX/DATA/DELX /SERVER/CTX/DATA/ENLX /SERVER/CTX/DATA/ESLX /SERVER/CTX/MESG SYS_CONNECT_BY_PATH is only available in Oracle version 9i or greater. key from t into where v_parent_key.key%type .key%type .etc.SYS_CONNECT_BY_PATH( key .parent_key%type . . '/' ) SYS_CONNECT_BY_PATH( name. begin select parent_key. v_key key = p_key .In a real application more robust code would be needed -.This function is only for demonstration purposes. v_key t.appearing in KEY values.to guard against things like separator characters -. For earlier versions a custom. ------------------------------------------------------------. -----------------------------------------------------------create or replace function KEY_PATH ( p_key t.Note: --. '/' ) from t start with parent_key is null connect by parent_key = prior key .

. '/' ) as KEY_PATH from t start with parent_key is null connect by parent_key = prior key . other than a path is required then the START WITH and CONNECT BY clauses can be omitted since KEY_PATH already knows how to traverse the hierarchy. LEVEL.g. like NAME. name . level-1 ) || key as key_indented . KEY_PATH( key. If no hierarchical information. select key . p_separator ) || p_separator || v_key end if. exception when no_data_found then return( null ).). if v_parent_key is null then return ( p_separator || v_key ). / show errors No errors. were required. select lpad(' '. end. else return ( KEY_PATH( v_parent_key. name . KEY_INDENTED --------------nls demo mesg server bin config log ctx admin data delx enlx eslx mig NAME ---------NLS DATA DEMO SERVER BIN CONFIG LOG CTX ADMIN DATA DELX ENLX ESLX MESG KEY_PATH ------------------------/nls /nls/demo /nls/mesg /server /server/bin /server/config /server/config/log /server/ctx /server/ctx/admin /server/ctx/data /server/ctx/data/delx /server/ctx/data/enlx /server/ctx/data/eslx /server/ctx/mig With this approach an additional function would be needed if the path for another column. e.

'nls' . 'nls' null 'server' 'server' 'config' 'server' 'ctx' 'ctx' 'data' 'data' 'data' 'ctx' . . . 'config'. 'admin' . null . varchar2(10) . 'enlx' . . ). ). . '/' ) as KEY_PATH from t order by KEY_PATH . . ). ). ). . 'data' . 'bin' . used by the examples in this section. insert into t values ( 'nls' insert into t values ( 'demo' insert into t values ( 'mesg' insert insert insert insert insert insert insert insert insert insert insert commit. 'mig' . ). 'log' . 'delx' . 'DATA' . 'SERVER' 'BIN' 'CONFIG' 'LOG' 'CTX' 'ADMIN' 'DATA' 'DELX' 'ENLX' 'ESLX' 'MESG' ). ). 'ctx' . ). KEY ---------nls demo mesg server bin config log ctx admin data delx enlx eslx mig NAME ---------NLS DATA DEMO SERVER BIN CONFIG LOG CTX ADMIN DATA DELX ENLX ESLX MESG KEY_PATH ------------------------/nls /nls/demo /nls/mesg /server /server/bin /server/config /server/config/log /server/ctx /server/ctx/admin /server/ctx/data /server/ctx/data/delx /server/ctx/data/enlx /server/ctx/data/eslx /server/ctx/mig Setup Run the code on this page in SQL*Plus to create the sample tables. ). column level format 99999 column key_indented format a15 column root_key format a10 into into into into into into into into into into into t t t t t t t t t t t values values values values values values values values values values values ( ( ( ( ( ( ( ( ( ( ( 'server'. ). data. ). . 'eslx' . etc.KEY_PATH( key. 'DEMO' . . ). create table ( key parent_key name ). . 'NLS' . varchar2(10) . . ). t varchar2(10) . .

drop table t . drop function KEY_PATH . COLUMN. those made by SET. procedures. and VARIABLE commands) exit your SQL*Plus session after running these cleanup commands. exit .column root_name column key_path column name_path set null '(null)' format a10 format a25 format a25 variable v_target_key varchar2(10) Cleanup Run the code on this page to drop the sample tables. created in earlier parts of this section. etc.g. To clear session state changes (e.

including: • • • • Denormalization Validation Data Warehousing Replication. e. thus remaining consistent with relational view terminology. For more complex applications links at the end of the tutorial will point to information on advanced features not covered here. refresh groups. partitioning. on the other hand. . You may still see this term in some Oracle 11g materialized view error messages. With materialized views. After completing it you should have enough information to use materialized views effectively in simple applications. This tutorial explores materialized view basics.Materialized Views A Materialized View is effectively a database table that contains the results of a query. This keyword is supported for backward compatibility. but should not be used in new code. updatable materialized views. these objects are either called "detail tables" (in data warehousing documentation) or "master tables" (in replication documentation and the Oracle Database SQL Reference guide). Oracle can automatically synchronize a materialized view's data with its source information as required with little or no programming effort. Since SQL Snippets is concerned mainly with relational uses of materialized views we will avoid the contradictory terms "master" and "detail" all together and instead use the term "base tables". The power of materialized views comes from the fact that.g. Materialized views can be used for many purposes. Terminology With relational views the FROM clause objects a view is based on are called "base tables". once created. Materialized Views were originally known as "Snapshots" in early releases of Oracle.

Views vs Materialized Views Like its predecessor the view. KEY ---------1 2 3 4 VAL ----a b c . KEY ---------1 2 3 4 VAL ----a b c select * from V . materialized views allow you to store the definition of a query in the database. Table View create view v as select * from t . select * from T . select * from MV . KEY ---------1 2 3 4 VAL ----a b c Materialized View create materialized view mv as select * from t .

KEY ---------1 2 3 4 VAL ----A B C View select * from V .refresh( 'MV' ). as below. on the other hand. KEY ---------1 2 3 4 VAL ----A B C select * from MV . In the following queries note how the rowid's for the table and the view are identical. This indicates the materialized view is returning a physically separate copy of the table data. indicating the view returns the exact same data stored in the table. ROWID -----------------AAAgZFAAEAAADyEAAA AAAgZFAAEAAADyEAAB AAAgZFAAEAAADyEAAC AAAgZFAAEAAADyEAAD The difference between views and materialized views becomes even more evident than this when table data is updated. differ from those of the table. Table select * from T . select * from T . KEY ---------1 2 3 4 VAL ----A B C Materialized View select * from MV . The rowids of the materialized view. KEY ---------1 2 3 4 VAL ----A B C select * from V . or automatically by Oracle in some cases. Table update t set val = upper(val). KEY ---------1 2 3 4 VAL ----A B C execute dbms_mview. Now that the materialized view has been refreshed its data matches that of its base table. . however. Refreshing can either be done manually. KEY ---------1 2 3 4 VAL ----a b c View Materialized View Note how. after the update. the view data matches the table data but the materialized view data does not.Unlike views. Data in materialized views must be refreshed to keep it synchronized with its base table. ROWID -----------------AAAgY9AAEAAAAVfAAA AAAgY9AAEAAAAVfAAB AAAgY9AAEAAAAVfAAC AAAgY9AAEAAAAVfAAD Materialized View select rowid from MV order by rowid . Table select rowid from T order by rowid . materialized views also store the results of the query in the database. ROWID -----------------AAAgY9AAEAAAAVfAAA AAAgY9AAEAAAAVfAAB AAAgY9AAEAAAAVfAAC AAAgY9AAEAAAAVfAAD View select rowid from V order by rowid .

REFRESH( LIST => 'MV'. val. Let's see a complete refresh in action now. commit. for Complete refresh.Cleanup drop materialized view mv . REFRESH COMPLETE There are various ways to refresh the data in a materialized view. drop view v . We will use the DBMS_MVIEW. METHOD => 'C' ). . KEY ---------1 2 3 4 VAL ----a b c ROWID -----------------AAAWgHAAEAAAAIEAAE AAAWgHAAEAAAAIEAAF AAAWgHAAEAAAAIEAAG AAAWgHAAEAAAAIEAAH Note how the rowids in the second query differ from those of the first. update t set val = lower(val). even when the new result set is identical to the old one. The "list" parameter accepts a list of materialized views to refresh (in our case we only have one) and the "method" parameter accepts a "C". the simplest way being a complete refresh. val. select key. rowid from mv . We will explore this type of refresh next. If a materialized view contains many rows and the base table's rows change infrequently refreshing the materialized view completely can be an expensive operation. In such cases it would be better to process only the changed rows. select key. rowid from mv . This is because complete refreshes create a whole new set of data. When a complete refresh occurs the materialized view's defining query is executed and the entire result set replaces the data currently residing in the materialized view. even though the data in table T was unchanged throughout. create materialized view mv REFRESH COMPLETE as select * from t . KEY ---------1 2 3 4 VAL ----a b c ROWID -----------------AAAWgHAAEAAAAIEAAA AAAWgHAAEAAAAIEAAB AAAWgHAAEAAAAIEAAC AAAWgHAAEAAAAIEAAD execute DBMS_MVIEW. The REFRESH COMPLETE clause tells Oracle to perform complete refreshes by default when a materialized view is refreshed.REFRESH procedure to initiate it.

KEY column mirrors the base table's primary key column T. This is called fast refreshing. This is because a table can only ever have one materialized view log related to it at a time. UPDATE t set val = upper( val ) where KEY = 1 . but showing it here helps illustrate materialized view log behaviour. Note how the materialized view log is not given a name. Fortunately there is a way to refresh only the changed rows in a materialized view's base table. To see what a materialized view log looks like we can examine the table used to implement it. T. In practice developers other than Dizwell never actually need to reference this table.-------KEY SNAPTIME$$ DMLTYPE$$ OLD_NEW$$ CHANGE_VECTOR$$ Type ---------------------NUMBER DATE VARCHAR2(1) VARCHAR2(1) RAW(255) The MLOG$_T. no rows selected The query above shows that a materialized view log is initially empty upon creation. so a name is not required. describe MLOG$_T Name Null? -------------------------------------------. Materialized View Logs As mentioned earlier. select * from MLOG$_T . complete refreshes of materialized views can be expensive operations. This mechanism is called a Materialized View Log. . like this.KEY. We can create a materialized view log on our test table. Rows are automatically added to MLOG$_T when base table T is changed. describe T Name Null? Type -------------------------------------------.------------------------------------KEY NOT NULL NUMBER VAL VARCHAR2(5) create materialized view log on t . Before a materialized view can perform a fast refresh however it needs a mechanism to capture any changes made to its base table.Cleanup drop materialized view mv . The other MLOG$ columns are system generated.

'e' ). create materialized view log on t WITH PRIMARY KEY . KEY ---------1 5 DMLTYPE$$ ---------U I If the changes affecting T are rolled back. create materialized view log on t WITH ROWID . drop materialized view log on t . dmltype$$ from MLOG$_T . rollback . This materialized view log is equivalent to the one created earlier in this topic. WITH ROWID To include rowids instead of primary keys WITH ROWID can be specified. select key. which did not have a WITH clause.-------KEY SNAPTIME$$ DMLTYPE$$ OLD_NEW$$ CHANGE_VECTOR$$ Type --------------------NUMBER DATE VARCHAR2(1) VARCHAR2(1) RAW(255) Note how MLOG$_T contains T's primary key column. drop materialized view log on t . dmltype$$ from MLOG$_T . column dmltype$$ format a10 select key. .INSERT into t ( KEY. desc mlog$_t Name Null? -------------------------------------------. Rollback complete. no rows selected WITH PRIMARY KEY To include the base table's primary key column in a materialized view log the WITH PRIMARY KEY clause can be specified. val ) values ( 5. T. because WITH PRIMARY KEY is the default option when no WITH clause is specified.KEY. so are the changes to MLOG$_T.

g.------------------------------------KEY M_ROW$$ SNAPTIME$$ DMLTYPE$$ OLD_NEW$$ CHANGE_VECTOR$$ Type NUMBER VARCHAR2(255) DATE VARCHAR2(1) VARCHAR2(1) RAW(255) In this case both KEY and M_ROW$$ appear in the log table. UPDATE T2 set amt = 333 where key = 60 . commit.desc mlog$_t Name Null? -------------------------------------------. update and delete. select SEQUENCE$$. WITH SEQUENCE A special SEQUENCE column can be include in the materialized view log to help Oracle apply updates to materialized view logs in the correct order when a mix of Data Manipulation (DML) commands. create materialized view log on t WITH ROWID. INSERT into T values ( 5.-------M_ROW$$ SNAPTIME$$ DMLTYPE$$ OLD_NEW$$ CHANGE_VECTOR$$ Type --------------------VARCHAR2(255) DATE VARCHAR2(1) VARCHAR2(1) RAW(255) Note how the KEY column was replaced by the M_ROW$$ column. 3. key. desc mlog$_t Name Null? -------------------------------------------. UPDATE T set val = upper(val) where key = 5 . drop materialized view log on t . drop materialized view log on t . are performed on multiple base tables in a single transaction. create materialized view log on t WITH SEQUENCE . insert. dmltype$$ from mlog$_T . e. 'e' ). PRIMARY KEY . A materialized view log can also be created with both a rowid and a primary key column. SEQUENCE$$ KEY DMLTYPE$$ . INSERT into T2 values ( 60. create materialized view log on t2 WITH SEQUENCE . which contains rowids from table T. 300 ).

drop materialized view log on t . column old_new$$ format a10 . create materialized view log on t WITH ( VAL ). Oracle recommends it.------------------------------------KEY VAL SNAPTIME$$ DMLTYPE$$ OLD_NEW$$ CHANGE_VECTOR$$ select * from t . "Oracle recommends that the keyword SEQUENCE be included in your materialized view log statement unless you are sure that you will never perform a mixed DML operation (a combination of INSERT.---------. or DELETE operations on multiple tables). In fact. KEY ---------1 2 3 4 5 VAL ----a b c E Type NUMBER VARCHAR2(5) DATE VARCHAR2(1) VARCHAR2(1) RAW(255) UPDATE t set val = 'f' where key = 5 . desc mlog$_t Name Null? -------------------------------------------.---------. key. dmltype$$ from mlog$_T2 . UPDATE." -. SEQUENCE$$ KEY DMLTYPE$$ ---------.from Creating Materialized Views: Materialized View Logs" WITH Column List The WITH clause can also contain a list of specific base table columns. In the next snippet we include the VAL column.---------.---------60082 60 I 60084 60 U Since mixed DML is a common occurrence SEQUENCE will be specified in most materialized view logs.---------60081 5 I 60083 5 U select SEQUENCE$$.

which Oracle uses internally during refreshes) to the old value. There is no need to store the new value for an update because it can be derived by applying the change vector (a RAW value stored in CHANGE_VECTOR$$.select key.----.g. create materialized view log on t with sequence. ( VAL ). KEY VAL OLD_NEW$$ ---------. val. VAL. We can do that using the INCLUDING NEW VALUES clause. drop materialized view log on t . ( VAL ). Gotcha . e. . The OLD_NEW$$ column identifies the value as either an old or a new value. key. column old_new$$ format a9 select sequence$$. primary key * ERROR at line 1: ORA-00922: missing or invalid option Omitting the comma before the column list works better. "( VAL )".Commas The syntax diagrams for the CREATE MATERIALIZED VIEW LOG command indicate a comma is required between each component of the WITH clause. it helps to have both the old value and the new value explicitly saved in the materialized view log. drop materialized view log on T . which we will identify in later topics. However this does not appear to be the case when the component is a column list. like this. old_new$$ from mlog$_t .---------. create materialized view log on t with sequence. primary key .----60085 5 f 60086 5 g OLD_NEW$$ --------O N Note how both the old and the new values are stored in the same column. create materialized view log on t with sequence ( VAL ) INCLUDING NEW VALUES . SEQUENCE$$ KEY VAL ---------. old_new$$ from mlog$_t order by sequence$$ . val. update t set val = 'g' where key = 5 . aka the "old" value. In some situations.---------5 E O INCLUDING NEW VALUES Clause In the last snippet we see that the VAL column contains values as they existed before the update operation.

No horizontal or vertical subsetting is permitted. Materialized view log created. First we use the REFRESH FAST clause to specify that the default refresh method should be fast. Gotcha . Earlier in this tutorial we saw how the rowids for each row in a materialized view changed after a complete refresh.1) . In Oracle 10g these restrictions are: • Tables with materialized view logs defined on them cannot be redefined online. primary key.from Oracle® Database Administrator's Guide 10g Release 2 (10.DBMS_REDEFINITION The DBMS_REDEFINITION package has certain restrictions related to materialized view logs. nor are any column transformations. -. -. • For materialized view logs and queue tables. This is also called "incremental" refreshing.2) . The only valid value for the column mapping string is NULL. the subsequent refresh of any dependent materialized view must be a complete refresh. nor are any column transformations. insert into t2 select * from t2_backup .from Oracle® Database Administrator's Guide 11g Release 1 (11. • For materialized view logs and queue tables. REFRESH FAST Now that we know how materialized view logs track changes to base tables we can use them to perform fast materialized view refreshes. Now let's see what happens to a materialized view's rowids after a fast refresh. insert into t select * from t_backup .create materialized view log on t with sequence ( VAL ). delete t . online redefinition is restricted to changes in physical properties. .e. No horizontal or vertical subsetting is permitted. i. commit. drop materialized view log on t . The only valid value for the column mapping string is NULL. refreshes where only the individual materialized view rows affected by base table changes are updated.Restrictions for Online Redefinition of Tables Cleanup delete t2 . drop materialized view log on t2 . online redefinition is restricted to changes in physical properties.Restrictions for Online Redefinition of Tables In Oracle 11g they are: • After redefining a table that has a materialized view log.

create materialized view log on t with sequence . with a fast refresh the materialized view data is not touched when no changes have been made to the base table. rowid from mv . create materialized view mv REFRESH FAST as select * from t . commit. Thus. method => 'F' ). KEY ---------1 2 3 4 VAL ----a b c ROWID -----------------AAAWm+AAEAAAAaMAAA AAAWm+AAEAAAAaMAAB AAAWm+AAEAAAAaMAAC AAAWm+AAEAAAAaMAAD The rowids did not change.refresh( list => 'MV'. unlike a complete refresh where each row would have been created anew.refresh( list => 'MV'. execute dbms_mview. KEY ---------1 2 3 4 VAL ----a b XX ROWID -----------------AAAWm+AAEAAAAaMAAA AAAWm+AAEAAAAaMAAB AAAWm+AAEAAAAaMAAC AAAWm+AAEAAAAaMAAD . update t set val = 'XX' where key = 3 . Now let's update a row in the base table. The "F" value for the "method" parameter ensures the refresh will be a Fast one. rowid from mv . val. val. KEY ---------1 2 3 4 VAL ----a b c ROWID -----------------AAAWm+AAEAAAAaMAAA AAAWm+AAEAAAAaMAAB AAAWm+AAEAAAAaMAAC AAAWm+AAEAAAAaMAAD Now we refresh the materialized view. execute dbms_mview. rowid from mv . select key. val. method => 'F' ). select key. select key.

val. val. This indicates that a complete refresh was performed. rowid from mv . KEY ---------1 2 VAL ----a b ROWID -----------------AAAWnBAAEAAAAaMAAA AAAWnBAAEAAAAaMAAB . A materialized view created with REFRESH FAST can still be refreshed completely if required though.refresh( list => 'MV'.Still no change in the rowids. method => 'F' ). In the following example note how. select key.refresh( list => 'MV'. create materialized view mv REFRESH COMPLETE as select * from t . execute dbms_mview. rowid from mv . select key. even though MV was created above with the REFRESH FAST clause. we'll learn more about this later). all its rowids change after the refresh. method => 'C' ). In row 3 we can see that VAL changed from "c" to "XX" though. Defaults The REFRESH FAST clause of the CREATE MATERIALIZED VIEW command tells Oracle what type of refresh to perform when no refresh option is specified. telling us that row 3 was updated during the refresh. val. drop materialized view mv . rowid from mv . KEY ---------1 2 3 4 VAL ----a b XX ROWID -----------------AAAWm+AAEAAAAaMAAE AAAWm+AAEAAAAaMAAF AAAWm+AAEAAAAaMAAG AAAWm+AAEAAAAaMAAH Similarly a materialized view created with REFRESH COMPLETE can be fast refreshed (assuming the materialized view is capable of being fast refreshed. select key. KEY ---------1 2 3 4 VAL ----a b XX ROWID -----------------AAAWnBAAEAAAAaMAAA AAAWnBAAEAAAAaMAAB AAAWnBAAEAAAAaMAAC AAAWnBAAEAAAAaMAAD execute dbms_mview.

3 XX 4 AAAWnBAAEAAAAaMAAC AAAWnBAAEAAAAaMAAD Note how none of the rowids in MV changed. Purging Materialized View Logs Oracle automatically purges rows in the materialized view log when they are no longer needed. method => 'F' ).PURGE_LOG can be used. select count(*) from mlog$_t . commit. . Cleanup drop materialized view mv . select count(*) from mlog$_t . create materialized view mv refresh fast as select * from t .PURGE_LOG If a materialized view log needs to be purged manually for some reason a procedure called DBMS_MVEW. COUNT(*) ---------0 insert into t values ( 5. select count(*) from mlog$_t . create materialized view log on t . commit . COUNT(*) ---------1 execute dbms_mview. update t set val = 'c' where key = 3 . drop materialized view log on t . 'e' ) . In the example below note how the log table is empty after the refresh.refresh( list => 'MV'. indicating a fast refresh. COUNT(*) ---------0 DBMS_MVEW.

num => 9999. END. select * from mv . method => 'C' ).DBMS_SNAPSHOT". select count(*) from mlog$_t .DBMS_SNAPSHOT". select * from mv . COUNT(*) ---------1 execute DBMS_MVIEW.PURGE_LOG( master => 'T'. Attempting a fast refresh will raise an error.refresh( list => 'MV'. method => 'F' ). line 2712 ORA-06512: at line 1 Such materialized views will need to be refreshed completely.DBMS_SNAPSHOT". execute dbms_mview.select count(*) from mlog$_t . line 2537 ORA-06512: at "SYS.refresh( list => 'MV'. . * ERROR at line 1: ORA-12034: materialized view log on "SCOTT". BEGIN dbms_mview. See the PURGE_LOG manual page for further details. KEY ---------1 2 3 4 5 VAL ----a b c e execute dbms_mview."T" younger than last refresh ORA-06512: at "SYS.refresh( list => 'MV'. Once a materialized view log has been purged any materialized views dependent on the deleted rows cannot be fast refreshed. commit. COUNT(*) ---------0 The "num" and "flag" parameters can be used to partially purge the log. method => 'F' ). select count(*) from mlog$_t . flag => 'delete' ) . line 2743 ORA-06512: at "SYS. COUNT(*) ---------0 update t set val = 'X' where key = 5 .

If we attempt to create such a materialized view we get an error. and will always be fast refreshed unless a complete refresh is explicitly requested. It can never be fast refreshed. KEY T_KEY AMT ---------. commit. 1.---------. create materialized view MV REFRESH FAST as select * from t2 . It can always be fast refreshed. It can be fast refreshed after certain kinds of changes to the base table but not others. 3. drop materialized view log on t . drop materialized view mv . as select * from t2 * ERROR at line 3: ORA-23413: table "SCOTT". REFRESH FAST Categories There are three ways to categorize a materialized view's ability to be fast refreshed. The next example demonstrates why. obviously. The third case is a little trickier. 2.KEY ---------1 2 3 4 5 VAL ----a b c X Cleanup delete from t where key = 5 . In the example below table T does not have a materialized view log on it. For the first case Oracle will raise an error if you try to create such a materialized view with its refresh method defaulted to REFRESH FAST."T2" does not have a materialized view log For the second case materialized views are created without error. Materialized views based on T cannot therefore be fast refreshed. select * from t2 .---------10 1 100 20 1 300 30 1 200 40 2 250 .

select rowid. Now let's try deleting a row from the base table. it worked as expected.DBMS_SNAPSHOT". amt ) including new values . Let's try inserting a row into the base table. 500 ). create materialized view mv REFRESH FAST as select t_key.---------AAAhMzAAEAAAEG8AAA 1 300 AAAhMzAAEAAAEG8AAB 2 250 So far everything works as expected.---------. method => 'F' ). method => 'F' ). rowid.50 2 150 create materialized view log on t2 with primary key.DBMS_SNAPSHOT".refresh( list => 'MV'. execute dbms_mview. select rowid.REFRESH command) and the materialized view correctly shows 500 as the maximum value for rows with T_KEY = 2.refresh( list => 'MV'. We created a materialized view log and created a materialized view with fast refresh as its default refresh method. commit. BEGIN dbms_mview. END.---------. t_key. insert into t2 values ( 5.refresh( list => 'MV'. t_key. line ORA-06512: at line 1 unsupported after deletes/updates 2255 2461 2430 . max( amt ) amt_max from t2 group by t_key .DBMS_SNAPSHOT". method => 'F' ). line ORA-06512: at "SYS. execute dbms_mview. sequence ( t_key. * ERROR at line 1: ORA-32314: REFRESH FAST of "SCOTT". ROWID T_KEY AMT_MAX -----------------."MV" ORA-06512: at "SYS. ROWID T_KEY AMT_MAX -----------------. amt_max from mv . 2. delete from t2 where key = 5 . commit. line ORA-06512: at "SYS.---------AAAhMzAAEAAAEG8AAA 1 300 AAAhMzAAEAAAEG8AAB 2 500 Again. amt_max from mv . The view was fast refreshed (the rowid's did not change after the DBMS_MVIEW.

refresh( list => 'MV'. Here are some of them. The reason is because this type of materialized view is an "insert-only" materialized view. • • • • • • CREATE MATERIALIZED VIEW . or never? One way would be to learn all the documented restrictions for fast refreshable materialized views. aggregates. it is only fast refreshable for inserts and direct loads. or NOT EXISTS • contains a CONNECT BY clause • references remote tables in different databases • references remote tables in a single database and defaults to the ON COMMIT refresh mode • references other materialized views which are not join or aggregate materialized views. etc.This time we received an error when we attempted a fast refresh. ALL. sometimes. i. (We will see why it is an insert-only view in the next topic. In general materialized views cannot be fast refreshed if the base tables do not have materialized view logs or the defining query: • contains an analytic function • contains non-repeating expressions like SYSDATE or ROWNUM • contains RAW or LONG RAW data types • contains a subquery in the SELECT clause • contains a MODEL clause • contains a HAVING clause • contains nested queries with ANY. The following links can help you find them if required though. They are documented in various sections of a few different manuals and are too numerous and complex to repeat here. DBMS_MVIEW. select rowid. amt_max from mv .---------AAAhMzAAEAAAEG8AAC 1 300 AAAhMzAAEAAAEG8AAD 2 250 Restrictions on Fast Refresh So how do we know whether a materialized view can be fast refreshed each time. execute dbms_mview.EXPLAIN_MVIEW.e. subqueries.) To synchronize an insert-only materialized view after a delete we need to do a complete refresh. ROWID T_KEY AMT_MAX -----------------.---------. UNION ALL. t_key. • There are even more restrictions for materialized views containing joins. method => 'C' ). not updates or deletes.FAST Clause General Restrictions on Fast Refresh Restrictions on Fast Refresh on Materialized Views with Joins Only Restrictions on Fast Refresh on Materialized Views with Aggregates Restrictions on Fast Refresh on Materialized Views with UNION ALL Restrictions for Materialized Views with Subqueries .

EXPLAIN_MVIEW As we saw in the preceding topic. via a table or via a varray. drop materialized view log on t2 . deleting old rows from MV_CAPABILITIES_TABLE 2.EXPLAIN_MVIEW with the table output method typically involves 1.sql. seq number ) . possible character(1) . Cleanup drop materialized view mv .EXPLAIN_MVIEW utility which we will explore next. related_text varchar(2000) . predicting whether or not a materialized view is fast refreshable can be complicated. The material below will help you use the utility effectively. documented CREATE TABLE command for MV_CAPABILITIES_TABLE can be found on UNIX systems at $ORACLE_HOME/rdbms/admin/utlxmv.• • • Restrictions for Materialized Views with Unions Containing Subqueries Restrictions for Using Multitier Materialized Views Restrictions for Materialized Views with Collection Columns Fortunately there is a second. MV_CAPABILITIES_TABLE There are two ways to get the output from DBMS_MVIEW. msgtxt varchar(2000) .Basic Materialized Views . running DBMS_MVIEW. related_num number . mvowner varchar(30) . VARRAY Output Using DBMS_MVIEW. Full details on how the utility works are available at the preceding link. simpler alternative for determining whether a materialized view is fast refreshable or not.Using MV_CAPABILITIES_TABLE (see Gotcha for a related bug). To use the table method the current schema must contain a table called MV_CAPABILITIES_TABLE. The full. capability_name varchar(30) . The DBMS_MVIEW. create table MV_CAPABILITIES_TABLE ( statement_id varchar(30) . It uses the DBMS_MVIEW. mvname varchar(30) . It is also available in Oracle's documentation at Oracle Database Data Warehousing Guide . Here is an abridged version. DBMS_MVIEW.EXPLAIN_MVIEW . msgno integer .EXPLAIN_MVIEW.EXPLAIN_MVIEW utility can simplify this task however.

To save time in this tutorial we will use DBMS_MVIEW.3.EXPLAIN_MVIEW's "mv" parameter -o it can contain either a query. v_capabilities sys.EXPLAIN_MVIEW's varray output option instead and supplement it with a custom function called MY_MV_CAPABILITIES.capabilities like REFRESH_FAST_PCT are included in the report -N . REWRITE. . -. p_linesize in number default 80 ) return clob as -------------------------------------------------------------------------------. CREATE MATERIALIZED VIEW command text.html --.sqlsnippets.Typical Usage: --set long 5000 -select my_mv_capabilities( 'MV_NAME' ) as mv_report from dual .capabilities like REFRESH_FAST_PCT are not included in the report --p_linesize -o the maximum size allowed for any line in the report output -o data that is longer than this value will be word wrapped --. any value big enough to contain the -report output will do -------------------------------------------------------------------------------pragma autonomous_transaction .ExplainMVArrayType .From http://www. p_capability_name_filter in varchar2 default '%' . -or a materialized view name --p_capability_name_filter -o use either REFRESH. or the default --p_include_pct_capabilities -Y . PCT. selecting new rows from MV_CAPABILITIES_TABLE.com/en/topic-12884.new line v_previous_possible char(1) := 'X' .Parameters: --p_mv -o this value is passed to DBMS_MVIEW. --o the value 5000 is arbitraty. create or replace function my_mv_capabilities ( p_mv in varchar2 . v_nl constant char(1) := unistr( '\000A' ). p_include_pct_capabilities in varchar2 default 'N' .

possible <> v_previous_possible then v_output := v_output || v_nl || case v_capability. seq ) loop ------------------------------------------------------------. ------------------------------------------------------------. related_text .print section body -----------------------------------------------------------declare ' ' of: ' of: ' || ':' .v_output clob . v_previous_possible := v_capability. end if. possible . msg_array => v_capabilities ) .possible end || v_nl . mvname . begin dbms_mview. msgtxt from table( v_capabilities ) where capability_name like '%' || upper( p_capability_name_filter ) || '%' and not ( capability_name like '%PCT%' and upper(p_include_pct_capabilities) = 'N' ) order by mvowner .explain_mview( mv => p_mv. possible desc . for v_capability in ( select capability_name .print section heading -----------------------------------------------------------if v_capability.possible when 'T' then 'Capable of: when 'Y' then 'Capable of: when 'F' then 'Not Capable when 'N' then 'Not Capable else v_capability.possible .

{1.related_text || ' ' .print capability name indented 2 spaces v_output := v_output || v_nl || ' ' || v_capability. -. end if. begin -.' || v_indented_line_size || '} |. end.capability_name || v_nl .5 ). -. end if. '(. .' || v_indented_line_size || '})' . end.related_text is not null then v_output := v_output || regexp_replace ( v_capability.' || v_indented_line_size || '} |. commit .' || v_indented_line_size || '})' . ' \1' || v_nl ) . '(.print related text indented 4 spaces and word wrapped if v_capability.{1. end loop. ' \1' || v_nl ) .{1.msgtxt || ' ' .{1.print message text indented 4 spaces and word wrapped if v_capability. return( v_output ).msgtxt is not null then v_output := v_output || regexp_replace ( v_capability.v_indented_line_size varchar2(3) := to_char( p_linesize .

a defining query 2.) The EXPLAIN_MVIEW output above shows that fast refresh is not possible in this case because T has no materialized view log. This completes our preparations./ show errors No errors. 'REFRESH' ) as mv_report from dual . DBMS_MVIEW.EXPLAIN_MVIEW With a Query DBMS_MVIEW.EXPLAIN_MVIEW can analyze three different types of materialized view code: 1. an existing materialized view. set long 5000 select my_mv_capabilities( 'SELECT * FROM T'. . A list of messages and related text is available at Table 8-8 MV_CAPABILITIES_TABLE Column Details. a CREATE MATERIALIZED VIEW command 3.EXPLAIN_VIEW in action. Here is an example that explains a simple query which could appear as the defining query in a CREATE MATERIALIZED VIEW command. Now let's see DBMS_MVIEW. MV_REPORT ------------------------------------------------------------------------------Capable of: REFRESH_COMPLETE Not Capable of: REFRESH_FAST REFRESH_FAST_AFTER_INSERT SCOTT.T the detail table does not have a materialized view log REFRESH_FAST_AFTER_ONETAB_DML see the reason why REFRESH_FAST_AFTER_INSERT is disabled REFRESH_FAST_AFTER_ANY_DML see the reason why REFRESH_FAST_AFTER_ONETAB_DML is disabled (Descriptions of each capability name are available at Table 8-7 CAPABILITY_NAME Column Details.

EXPLAIN_MVIEW With Existing Materialized View For our last example we will explain an existing materialized view. Rewrite capabilities will be covered in Query Rewrite Restrictions and Capabilities. For now we will only examine refresh capabilities. 'REFRESH' ) as mv_report from dual . create materialized view mv refresh fast as select t_key.EXPLAIN_MVIEW can report on a materialized view's refresh. max( amt ) amt_max from t2 group by t_key . select my_mv_capabilities ( 'CREATE MATERIALIZED VIEW MV REFRESH FAST AS SELECT * FROM T' . create materialized view log on t2 with primary key.EXPLAIN_MVIEW With CREATE MATERIALIZED VIEW Now let's create a materialized view log on T and then use EXPLAIN_MVIEW to explain the capabilities of an entire CREATE MATERIALIZED VIEW command. DBMS_MVIEW. rewrite. the insert-only one we saw in the preceding topic REFRESH FAST Categories. create materialized view log on t .Note that DBMS_MVIEW. DBMS_MVIEW. MV_REPORT ------------------------------------------------------------------------------Capable of: REFRESH_COMPLETE REFRESH_FAST REFRESH_FAST_AFTER_INSERT REFRESH_FAST_AFTER_ONETAB_DML REFRESH_FAST_AFTER_ANY_DML This time we see that a materialized view using our simple query could be fast refreshable in all cases. amt ) including new values . sequence ( t_key. and partition change tracking (PCT) capabilities. rowid.

POSSIBLE CHARACTER(1). commit. -. execute dbms_mview. MV_REPORT ------------------------------------------------------------------------------Capable of: REFRESH_COMPLETE REFRESH_FAST REFRESH_FAST_AFTER_INSERT Not Capable of: REFRESH_FAST_AFTER_ONETAB_DML mv uses the MIN or MAX aggregate functions REFRESH_FAST_AFTER_ONETAB_DML COUNT(*) is not present in the select list REFRESH_FAST_AFTER_ANY_DML see the reason why REFRESH_FAST_AFTER_ONETAB_DML is disabled Here we see that fast refresh is available after inserts.Basic Materialized Views .explain_mview( 'select * from t' ).. Gotcha Both the $ORACLE_HOME/rdbms/admin/utlxmv. 'REFRESH' ) as mv_report from dual . but not other types of DML..Using MV_CAPABILITIES_TABLE state the values in MV_CAPABILITIES_TABLE.sql file and the CREATE TABLE command at Oracle Database Data Warehousing Guide ... It does not mean the materialized view is fast refreshable in all cases. column possible format a8 . In actual use we can see the values are really "Y" and "N".T = capability is possible -. select my_mv_capabilities( 'MV'. CREATE TABLE MV_CAPABILITIES_TABLE . Note also that the "REFRESH_FAST" capability will appear whenever at least one of the other REFRESH_FAST_% capabilities is available.POSSIBLE will either be "T" or "F". delete from mv_capabilities_table .F = capability is not possible ..

The REFRESH FORCE method does just that. It performs a FAST refresh if possible.EXPLAIN_MVIEW we saw an insert-only materialized view which could be fast refreshed after inserts into the base table but needed a complete refresh after other types of DML. 2. used when DBMS_MVIEW. insert into t2 values ( 5. 500 ). With these types of materialized views it is often most convenient to let Oracle decide which refresh method is best. amt_max from mv . amt ) including new values . REFRESH FORCE In REFRESH FAST Categories and DBMS_MVIEW. execute dbms_mview. max( amt ) amt_max from t2 group by t_key .---------AAAWpLAAEAAAAaMAAA 1 300 AAAWpLAAEAAAAaMAAB 2 250 First let's try an insert and a refresh. drop materialized view log on t2 . otherwise it performs a COMPLETE refresh. rowid. t_key. POSSIBLE -------Y N The values "T" and "F" are. ROWID T_KEY AMT_MAX -----------------. . create materialized view mv REFRESH FORCE as select t_key.select distinct POSSIBLE from mv_capabilities_table . however.---------. Cleanup set long 80 drop materialized view mv .EXPLAIN_MVIEW output is saved to a varray. sequence ( t_key. commit.refresh( list => 'MV' ). select rowid. drop materialized view log on t . create materialized view log on t2 with primary key.

Now let's try a delete followed by a refresh. NEVER REFRESH If for some reason we need to prevent refresh operations of any sort. select * from mv . execute dbms_mview.refresh( list => 'MV' ).---------AAAWpLAAEAAAAaMAAC 1 300 AAAWpLAAEAAAAaMAAD 2 250 In the REFRESH FAST Categories topic we received an "ORA-32314: REFRESH FAST of "SCOTT". t_key. .select rowid. select rowid.---------. amt_max from mv .---------. on our materialized views we can use the NEVER REFRESH method. This time with REFRESH FORCE we did not. ROWID T_KEY AMT_MAX -----------------. ROWID T_KEY AMT_MAX -----------------. drop materialized view log on t2 . Cleanup drop materialized view mv . delete from t2 where key = 5 . FAST or COMPLETE. Instead Oracle performed a COMPLETE refresh (note how the rowids for each row changed).---------AAAWpLAAEAAAAaMAAA 1 300 AAAWpLAAEAAAAaMAAB 2 500 Since the rowids did not change but the AMT_MAX values did we can tell that a FAST refresh was performed."MV" unsupported after deletes/updates" error at this point. create materialized view mv NEVER REFRESH as select * from t . t_key. commit. KEY ---------1 2 3 4 VAL ----a b c Let's see what happens when we update the base table and then attempt a refresh. amt_max from mv .

commit . execute dbms_mview. Cleanup drop materialized view mv .update t set val = upper(val) . commit .REFRESH command. BEGIN dbms_mview. I cannot see a practical reason for having a materialized view with NEVER REFRESH set at all times. In other words this create materialized view mv as select * from t . is equivalent to this. update t set val = lower(val) .DBMS_SNAPSHOT".DBMS_SNAPSHOT".) NEVER REFRESH can come in handy though when refresh operations on a materialized view need to be prevented temporarily during maintenance or debugging operations.refresh( 'MV' ). To refresh ON DEMAND materialized views we explicitly call one of the following procedures. END. In this case the materialized view's refresh mode can be changed to NEVER REFRESH using the ALTER MATERIALIZED VIEW command. drop materialized view mv .refresh( 'MV' ). create materialized view mv REFRESH ON DEMAND as select * from t . line 2743 ORA-06512: at "SYS. (If you know of any please let me know using the Comments link below. ON DEMAND Up to this point in the tutorial we have always refreshed our materialized views manually with the DBMS_MVIEW. line 2712 ORA-06512: at line 1 Oracle prevented the refresh by raising an error. line 2537 ORA-06512: at "SYS. This is know as ON DEMAND refreshing and it is the default refresh mode when none is specified in the CREATE MATERIALIZED VIEW command. * ERROR at line 1: ORA-23538: cannot explicitly refresh a NEVER REFRESH materialized view ("MV") ORA-06512: at "SYS. .DBMS_SNAPSHOT".

This is possible using the ON COMMIT refresh mode.REFRESH( 'MV' ).REFRESH DBMS_MVIEW. 'e' ). ROWID KEY VAL -----------------.REFRESH_ALL_MVIEWS DBMS_MVIEW.----AAAXNGAAEAAAAasAAA 1 a AAAXNGAAEAAAAasAAB 2 b AAAXNGAAEAAAAasAAC 3 c AAAXNGAAEAAAAasAAD 4 Let's see what happens to the view in the course of an insert operation. create materialized view log on t . ON COMMIT In some situations it would be convenient to have Oracle refresh a materialized view automatically whenever changes to the base table are committed. 'e' ). select * from mv where key = 5 . commit. val from mv .---------.• • • DBMS_MVIEW. KEY VAL ---------. key.REFRESH_DEPENDENT Here is an example that uses DBMS_MVIEW. . insert into t values ( 5. select * from mv where key = 5 . select rowid. delete from t where key = 5 . no rows selected execute DBMS_MVIEW.REFRESH. create materialized view mv REFRESH FAST ON COMMIT as select * from t . insert into t values ( 5. commit. Here is an example.----5 e Cleanup drop materialized view mv .

key. val from mv . val. In the following example materialized view MV (created at the top of this page) was created with REFRESH FAST. val.this materialized view is not fast refreshable -. 2. Attempting a distributed transaction on its base table. select rowid. The base tables will never have any distributed transactions applied to them. will therefore raise an error. sys_xmlgen( val ) as val_xml from t . The first case produces an error during the CREATE MATERIALIZED VIEW command.----AAAXNGAAEAAAAasAAA 1 a AAAXNGAAEAAAAasAAB 2 b AAAXNGAAEAAAAasAAC 3 c AAAXNGAAEAAAAasAAD 4 Nothing happend yet.REFRESH was required.---------. ROWID KEY VAL -----------------. as select key. Restrictions Materialized views can only refresh ON COMMIT in certain situations. ROWID KEY VAL -----------------. 1. No call to DBMS_MVIEW. -. Let's issue a COMMIT. val from mv . sys_xmlgen( val ) as val_xml from t * ERROR at line 3: ORA-12054: cannot set the ON COMMIT refresh attribute for the materialized view The second case generates an error when a distributed transaction is attempted on the base table. key. . The materialized view cannot contain object types or Oracle-supplied types.---------.----AAAXNGAAEAAAAasAAA 1 a AAAXNGAAEAAAAasAAB 2 b AAAXNGAAEAAAAasAAC 3 c AAAXNGAAEAAAAasAAD 4 AAAXNGAAEAAAAatAAA 5 e Note how the materialized view was automatically fast refreshed after the COMMIT command.select rowid.because the materialized view contains an Oracle-supplied type create materialized view mv2 REFRESH FAST ON COMMIT as select key. T. commit.

KEY ---------1 2 3 4 5 11 12 13 14 15 VAL ----a b c e a b c e -. val from t . create materialized view mv2 REFRESH COMPLETE ON COMMIT as select key.insert into t select key+10. I also assumed that specifying "REFRESH ON COMMIT" is equivalent to specifying "REFRESH FAST ON COMMIT".) ON DEMAND materialized views have no such restriction.cleanup test data in preparation for next section delete from t where key >= 5 . alter materialized view mv refresh ON DEMAND . val from T@REMOTE . commit. as the following snippet demonstrates. commit. some remote DBs may be in-doubt ORA-02051: another session in same transaction failed (REMOTE is a database link which loops back to the current account.5632 rolled back. select * from t . commit * ERROR at line 1: ORA-02050: transaction 5. Gotcha The SQL Language Reference manual says this about the ON COMMIT clause." -.Oracle® Database SQL Language Reference: CREATE MATERIALIZED VIEW When I first read this I assumed it meant that "REFRESH COMPLETE ON COMMIT" is not allowed. . The following examples prove neither is correct however. insert into t select key+10. commit .21. "Specify ON COMMIT to indicate that a fast refresh is to occur whenever the database commits a transaction that operates on a master table of the materialized view. val from T@REMOTE .

Given these observations I can only conclude the documentation is either in error or misleading when it says "specify ON COMMIT to indicate that a fast refresh is to occur". select rowid. 'e' ). create materialized view mv2 REFRESH ON COMMIT as select key. not FAST.---------. Cleanup drop materialized view mv .----AAAXNMAAEAAAAakAAA 1 a AAAXNMAAEAAAAakAAB 2 b AAAXNMAAEAAAAakAAC 3 c AAAXNMAAEAAAAakAAD 4 insert into t values ( 5. drop materialized view log on t . select rowid. key. val from mv2 . The next example examines the behavior of "REFRESH ON COMMIT" without a specified refresh method. ROWID KEY VAL -----------------. . when no REFRESH method is specified the default behaviour is "REFRESH FORCE" regardless of whether ON COMMIT is used or not. val from t .As we can see the CREATE MATERIALZIED view command succeeded even though COMPLETE. commit .because it has no materialized view log drop materialized view mv2 . "REFRESH ON COMMIT" is not therefore equivalent to "REFRESH FAST ON COMMIT". drop materialized view mv2 . delete from t where key >= 5 . was specified with ON COMMIT. commit .---------. -. key. val from mv2 . ROWID KEY VAL -----------------.----AAAXNMAAEAAAAakAAE 1 a AAAXNMAAEAAAAakAAF 2 b AAAXNMAAEAAAAakAAG 3 c AAAXNMAAEAAAAakAAH 4 AAAXNMAAEAAAAakAAI 5 e The fact that all the rowid's in MV2 changed after the INSERT transaction committed confirms that a complete refresh took place during the commit. In fact.fast refreshable materialized views on T can no longer be created on T -.

create materialized view mv as select key. CONSTRAINT_NAME CONSTRAINT_TYPE INDEX_NAME -------------------. describe t2 Name ------------------------------------------------------------------------KEY T_KEY AMT Null? Type -------NOT NULL NUMBER NOT NULL NUMBER NOT NULL NUMBER create materialized view log on t2 with primary key. drop materialized view mv . val from t . column constraint_name format a20 column constraint_type format a15 column index_name format a15 select constraint_name. amt ) including new values . not to be confused with a base table). which is part of the materialized view also called "MV".Constraints System Generated Constraints When a materialized view is created Oracle may add system generated constraints to its underlying table (i. constraint_type. sequence ( t_key.e. the table containing the results of the query. constraint_type. rowid. column search_condition format a30 select constraint_name. search_condition .--------------. create materialized view mv refresh fast on commit as select t_key. index_name from user_constraints where TABLE_NAME = 'MV' .--------------SYS_C0019948 P SYS_C0019948 In the next example Oracle automatically adds a check constraint. In the following example note how Oracle automatically adds a primary key constraint to the table called "MV". count(*) row_count from t2 group by t_key .

search_condition from user_constraints where table_name = 'MV' . CONSTRAINT_NAME CONSTRAINT_TYPE SEARCH_CONDITION -------------------.from where user_constraints table_name = 'MV' . Let's see this in action by creating a check constraint on MV. commit.note we used "alter table" here add CONSTRAINT MY_CONSTRAINT CHECK ( ROW_COUNT <= 3 ) DEFERRABLE . select * from t2 . constraint_type.-----------------------------SYS_C0019949 C "T_KEY" IS NOT NULL Adding Your Own Constraints If necessary we can create our own constraints on materialized view tables in addition to the ones Oracle may add. insert into T2 values ( 5. 500 ).MY_CONSTRAINT) violated Implementing multirow validation rules such as this one properly is not possible using check constraints on regular tables. CONSTRAINT_NAME -------------------SYS_C0019949 MY_CONSTRAINT CONSTRAINT_TYPE SEARCH_CONDITION --------------.-----------------------------C "T_KEY" IS NOT NULL C ROW_COUNT <= 3 Now any attempt to create more than 3 rows per group in table T2 will generate an error at commit time. 1. With materialized views they are declared using a few lines of code and are virtually bullet proof when applied correctly. Implementing them using triggers can be difficult if not impossible. commit * ERROR at line 1: ORA-12008: error in materialized view refresh path ORA-02290: check constraint (SCOTT.---------10 1 100 20 1 300 30 1 200 40 2 250 50 2 150 alter table mv -. When the materialized view is in ON COMMIT mode these constraints effectively constrain the materialized view's base tables. We will learn more about this powerful multirow validation approach in a .---------. KEY T_KEY AMT ---------. select constraint_name.--------------.

search_condition from user_constraints where table_name = 'MV' .e.-----------------------------C "T_KEY" IS NOT NULL C ROW_COUNT <= 3 C row_count < 4 The Oracle manual page for ALTER MATERIALIZED VIEW however does not indicate that constraints can be added this way. not to be confused with a base table). In the following example note how Oracle automatically adds an index to implement the system generated primary key we saw in the preceding topic. i.uniqueness . select constraint_name. ic. create materialized view mv as select key. CONSTRAINT_NAME -------------------SYS_C0019949 MY_CONSTRAINT MY_SECOND_CONSTRAINT CONSTRAINT_TYPE SEARCH_CONDITION --------------. the table containing the results of the query. constraint_type. Indexes When a materialized view is created Oracle may add system generated indexes to its underlying table (i.column_name from user_indexes i inner join user_ind_columns ic . Cleanup drop materialized view mv . column index_name format a15 column column_name format a15 select index_name . drop materialized view log on t2 . Curiously enough an ALTER MATERIALIZED VIEW command would have worked too. val from t . Constraints. Until the documentation says this is legal it is best to use ALTER TABLE.future SQL Snippets tutorial so stay tuned! In the mean time Ask Tom "Declarative Integrity" has some good information on the subject. Gotcha When we created MY_CONSTRAINT above we use an ALTER TABLE command. ALTER MATERIALIZED VIEW mv add constraint my_second_constraint check ( row_count < 4 ) deferrable .

ie. ic. sequence ( t_key. COUNT(*) ROW_COUNT from t2 group by t_key . drop materialized view mv . column column_expression format a35 select index_name .) Adding Your Own Indexes We can add out own indexes to MV just as we would a regular table.column_expression from user_indexes i inner join user_ind_columns ic left outer join user_ind_expressions ie using ( index_name ) using ( index_name ) where ic. create materialized view mv refresh fast on commit as select t_key. i.using ( index_name ) where i. INDEX_NAME UNIQUENES COLUMN_NAME COLUMN_EXPRESSION --------------.table_name = 'MV' .column_name .--------. INDEX_NAME UNIQUENES COLUMN_NAME --------------. Do not attempt to use it in your own code. . amt ) including new values .--------. See Nulls and Equality: SQL Only for additional info. create materialized view log on t2 with primary key. In the following example we will add an index on the T_KEY column.table_name = 'MV' .--------------.--------------SYS_C0019959 UNIQUE KEY In the next example Oracle automatically generates a function based index.uniqueness . rowid.----------------------------------I_SNAP$_MV UNIQUE SYS_NC00003$ SYS_OP_MAP_NONNULL("T_KEY") (Note that SYS_OP_MAP_NONNULL is an undocumented Oracle function.

T_KEY ROW_COUNT ---------.create index MY_INDEX on mv ( T_KEY ) .column_name from user_indexes i inner join user_ind_columns ic using ( index_name ) where i. set autotrace on explain set linesize 95 select * from mv where t_key = 2 .uniqueness . select index_name . i. INDEX_NAME --------------I_SNAP$_MV MY_INDEX UNIQUENES --------UNIQUE NONUNIQUE COLUMN_NAME --------------SYS_NC00003$ T_KEY To confirm that Oracle uses our index in queries let's turn SQL*Plus's Autotrace feature on and execute a query. ic.access("T_KEY"=2) .table_name = 'MV' .---------2 2 Execution Plan ---------------------------------------------------------Plan hash value: 2793437614 -----------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | -----------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 1 | 26 | 2 (0)| 00:00:01 | | 1 | MAT_VIEW ACCESS BY INDEX ROWID| MV | 1 | 26 | 2 (0)| 00:00:01 | |* 2 | INDEX RANGE SCAN | MY_INDEX | 1 | | 1 (0)| 00:00:01 | -----------------------------------------------------------------------------------------Predicate Information (identified by operation id): --------------------------------------------------2 .

max( amt ) as amt_max FROM T2 group by t_key order by t_key .dynamic sampling used for this statement Note how the optimizer chose an INDEX RANGE SCAN from MY_INDEX in step 2. amt ) including new values . Such materialized views make queries like this select t_key. Cleanup drop materialized view mv . amt_max FROM MV order by t_key . create materialized view mv refresh fast on commit as select t_key.Note ----. T_KEY AMT_MAX ---------.---------1 300 2 250 faster than its equivalent query. ENABLE QUERY REWRITE Materialized views can be useful for pre-calculating and storing derived values such as AMT_MAX in the following snippet. MAX( AMT ) AMT_MAX from t2 group by t_key . sequence ( t_key. rowid.---------1 300 2 250 . T_KEY AMT_MAX ---------. create materialized view log on t2 with primary key. drop materialized view log on t2 . select t_key.

set autotrace on explain set linesize 95 select t_key. 'MV' ) . alter materialized view mv ENABLE QUERY REWRITE . T_KEY AMT_MAX ---------.gather_table_stats( user. execute dbms_stats. max( amt ) as amt_max FROM T2 group by t_key order by t_key .ORA-00439 below if you encounter an ORA-00439 error at this step. Finally we can confirm Oracle will use the materialized view in queries by turning SQL*Plus's Autotrace feature on. To see it in action we first need to make the materialized view available to Query Rewrite like this. Next we collect statistics on the materialized view to help Oracle optimize the query rewrite process. (See Gotcha .) Note that materialized views which do not include the ENABLE QUERY REWRITE clause will have Query Rewrite disabled by default.---------1 300 2 250 Execution Plan ---------------------------------------------------------Plan hash value: 446852971 ------------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | ------------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 2 | 14 | 4 (25)| 00:00:01 | | 1 | SORT ORDER BY | | 2 | 14 | 4 (25)| 00:00:01 | | 2 | MAT_VIEW REWRITE ACCESS FULL| MV | 2 | 14 | 3 (0)| 00:00:01 | ------------------------------------------------------------------------------------- .Wouldn't it be nice if Oracle could use the information in MV to resolve this last query too? If your database has a feature called Query Rewrite available and enabled this happens automatically.

count(amt) as amt_count from t2 group by t_key . a more expensive approach than simply selecting pre-calculated column values from MV is. Each time this query is executed it has to recalculate MAX(VAL) from the information in T2 for each group. T_KEY AMT_MAX ---------.Note how the optimizer chose to access MV for its pre-calculated MAX(AMT) values in line 2 even though the query itself made no mention of MV. count(*) as row_count .dynamic sampling used for this statement Note how the optimizer chose to access T2 this time. create materialized view mv2 refresh fast on commit ENABLE QUERY REWRITE as select t_key . If you attempt to use ENABLE QUERY REWITE in an Oracle database where the feature is not enabled you will receive an ORA-00439 error. .ORA-00439 The materialized view query rewrite feature is not available in Oracle XE and some other Oracle configurations. alter session set QUERY_REWRITE_ENABLED = FALSE . Without the Query Rewrite feature the execution plan would look like this. select t_key. Gotcha .---------1 300 2 250 Execution Plan ---------------------------------------------------------Plan hash value: 50962384 --------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 5 | 130 | 4 (25)| 00:00:01 | | 1 | SORT GROUP BY | | 5 | 130 | 4 (25)| 00:00:01 | | 2 | TABLE ACCESS FULL| T2 | 5 | 130 | 3 (0)| 00:00:01 | --------------------------------------------------------------------------Note ----. max( amt ) as amt_max FROM T2 group by t_key order by t_key .

'REWRITE' ) as mv_report from dual . an expression containing the USER pseudo column or the SYSTIMESTAMP function. create materialized view mv ENABLE QUERY REWRITE as select key. set autotrace off drop materialized view mv . val from t . e. USER from t .from t2 * ERROR at line 9: ORA-00439: feature not enabled: Materialized view rewrite Cleanup alter session set query_rewrite_enabled = true . as select key. set long 5000 select my_mv_capabilities( 'MV'. Query Rewrite Restrictions and Capabilities Restrictions Materialized views with the following characteristics cannot have query rewrite enabled: • the defining query references functions which are not DETERMINISTIC • an expression in the defining query is not repeatable. First lets look at a simple.g. single table materialized view with query rewrite disabled. MV_REPORT . drop materialized view log on t2 . Attempting to violate these restrictions results in an error. create materialized view mv DISABLE QUERY REWRITE as select key. In EXPLAIN_MVIEW we used a utility called MY_MV_CAPABILITIES to explore a materialized view's refresh capabilities. In the snippets below we will use this same utility to explore rewrite capabilities. val. val. USER from t * ERROR at line 3: ORA-30353: expression not supported for query rewrite Capabilities A few different materialized view query rewrite capabilities exist.

'REWRITE' ) as mv_report from dual . If the materialized view happened to referenced a remote table then some rewrite capabilities would be available. drop materialized view mv . MV_REPORT ------------------------------------------------------------------------------Capable of: REWRITE REWRITE_FULL_TEXT_MATCH REWRITE_PARTIAL_TEXT_MATCH REWRITE_GENERAL Now all rewrite capabilities are available. 'REWRITE' ) as mv_report from dual . select my_mv_capabilities( 'MV'.------------------------------------------------------------------------------Not Capable of: REWRITE REWRITE_FULL_TEXT_MATCH query rewrite is disabled on the materialized view REWRITE_PARTIAL_TEXT_MATCH query rewrite is disabled on the materialized view REWRITE_GENERAL query rewrite is disabled on the materialized view This materialized view obviously has no rewrite capabilities available to it. alter materialized view mv ENABLE QUERY REWRITE . MV_REPORT ------------------------------------------------------------------------------- . but not others.) Enabling query rewrite on the materialized view changes this. val from T@REMOTE . (Descriptions of each capability name are available at Table 8-7 CAPABILITY_NAME Column Details. create materialized view mv enable query rewrite as select key. select my_mv_capabilities( 'MV'.

---------10 1 100 20 1 300 30 1 200 40 2 250 50 2 150 create materialized view mv as select t. select * from t . t2. Join Queries So far in this tutorial we have only seen materialized views based on a single table. t. KEY T_KEY AMT ---------. KEY ---------1 2 3 4 VAL ----a b c select * from t2 .Capable of: REWRITE REWRITE_PARTIAL_TEXT_MATCH REWRITE_GENERAL Not Capable of: REWRITE_FULL_TEXT_MATCH T mv references a remote table or view in the FROM list Cleanup drop materialized view mv .---------. t2 where t.key = t2.amt t2_amt from t.key t_key . t2. .t_key . Here is a simple example.key t2_key . Materialized views can also be created on multi-table queries to store the pre-calculated results of expensive join operations.val t_val .

They are as follows. you must collect statistics on it using the DBMS_STATS package. The Prototype Applying these restrictions and recommendations to our test case above yields the following prototypical materialized view with joins.from Refreshing Materialized Views: Tips for Refreshing Materialized Views Without Aggregates "After you create the materialized view. Oracle Database needs the statistics generated by this package to optimize query rewrite.select t_key.) to be fast refreshable certain restrictions beyond the General Restrictions on Fast Refresh must be met. then having an index on each of the join column rowids in the detail table will enhance refresh performance greatly. drop materialized view mv . t_val. "If a materialized view contains joins but no aggregates.---------.from CREATE MATERIALIZED VIEW.from Restrictions on Fast Refresh on Materialized Views with Joins Only In addition to these restrictions there are some recommended practices for using join queries. T_KEY ---------1 1 1 2 2 T_VAL T2_KEY T2_AMT ----. Whenever I need to create this type of materialized view in an application I use the code below as a starting point to remind me of the requirements. etc." -. sequence . create materialized view mv .---------a 10 100 a 20 300 a 30 200 b 40 250 b 50 150 REFRESH FAST For a materialized view with only joins (no aggregates. because this type of materialized view tends to be much larger than materialized views containing aggregates. create materialized view log on t with rowid. t2_key. unions. subqueries." -. t2_amt from mv . create materialized view log on t2 with rowid. These additional restrictions are: • materialized view logs with rowids must exist for all of the defining query's base tables • the SELECT clause cannot contain object type columns • the defining query cannot have a GROUP BY clause or aggregates • rowid columns for each table instance in the FROM clause must appear in the SELECT clause. -. sequence .

create index mv_i1 on mv ( t_row_id ) . to confirm it can be refreshed in all required situations.---------a 10 100 a 20 300 a 30 200 b 40 250 . t2_amt from mv . 'MV' ) . create index mv_i2 on mv ( t2_row_id ) . T_KEY ---------1 1 1 2 T_VAL T2_KEY T2_AMT ----. t2 where t. set long 5000 select my_mv_capabilities( 'MV'.refresh fast on commit enable query rewrite as select t.---------. First. MV_REPORT ------------------------------------------------------------------------------Capable of: REFRESH_COMPLETE REFRESH_FAST REFRESH_FAST_AFTER_INSERT REFRESH_FAST_AFTER_ONETAB_DML REFRESH_FAST_AFTER_ANY_DML Now let's test drive our new MV.rowid t2_row_id from t.amt t2_amt t. .key t2_key t2. execute dbms_stats.key t_key t. t_val. t2_key.val t_val t2. select t_key. Whenever we create a fast refreshable view we should use our EXPLAIN_MVIEW utility. 'REFRESH' ) as mv_report from dual .t_key . MY_MV_CAPABILITIES.key = t2. . . .rowid t_row_id t2. .gather_table_stats( user. here are MV's initial contents.

300 ) .---------A 10 100 A 20 300 A 30 200 B 40 250 B 50 150 C 60 300 Both changes are reflected in MV. select t_key. t2_key.---------. 'REWRITE' ) as mv_report from dual .2 b 50 150 Now let's do some DML on both base tables and see the effect on MV. MV_REPORT ------------------------------------------------------------------------------Capable of: REWRITE REWRITE_FULL_TEXT_MATCH REWRITE_PARTIAL_TEXT_MATCH REWRITE_GENERAL Gotcha . insert into t2 values ( 60. create materialized view mv2 refresh fast as . commit. t_val. Query Rewrite Materialized views containing joins can be used by the query rewrite facility (see ENABLE QUERY REWRITE). as expected. update t set val = upper(val) . t2_amt from mv . T_KEY ---------1 1 1 2 2 3 T_VAL T2_KEY T2_AMT ----.ANSI Join Syntax When we attempt to create a materialized view with the ANSI join syntax equivalent of the defining query used above we are surprisingly rewarded with an ORA error. 3. select my_mv_capabilities( 'MV'.

T_KEY ) * ERROR at line 12: ORA-12015: cannot create a fast refresh materialized view from a complex query While this behaviour appears to be a bug at first glance. .key t2_key ..rowid t_row_id t2. An examination of the EXPLAIN_MVIEW results for this case points to some behind-the-scenes transformations with ANSI syntax which may be causing the limitation.KEY = T2. select t.amt t2_amt t. . .rowid t2_row_id from T INNER JOIN T2 ON ( T. t2.val t_val t2. 'REFRESH_FAST_AFTER_INSERT' ) as mv_report from dual .rowid t_row_id .T_KEY ) T INNER JOIN T2 ON ( T. t. select my_mv_capabilities ( 'create materialized view mv2 refresh fast as select t. Metalink note 420856. t2. t. .1 explains that it is really an undocumented limitation of fast refresh materialized views.T_KEY )' .key t_key t.rowid t2_row_id from T INNER JOIN T2 ON . t2.KEY = T2. ( T.key t2_key t2.amt t2_amt .KEY = T2. MV_REPORT ------------------------------------------------------------------------------Not Capable of: REFRESH_FAST_AFTER_INSERT inline view or subquery in FROM list not supported for this type MV REFRESH_FAST_AFTER_INSERT inline view or subquery in FROM list not supported for this type MV REFRESH_FAST_AFTER_INSERT view or subquery in from list .key t_key .val t_val .

) to be fast refreshable certain restrictions beyond the General Restrictions on Fast Refresh must be met.---------. commit. select * from t2 order by key . insert into t2 select * from t2_backup . delete t . etc. These . materialized views containing aggregate functions are also possible. unions. delete t2 . subqueries.---------1 600 2 400 REFRESH FAST For a materialized view with only aggregates (no joins. select * from mv order by t_key . Aggregate Queries In addition to materialized views based on join queries. drop materialized view log on t2 . SUM(AMT) AMT_SUM from t2 group by t_key . KEY T_KEY AMT ---------. Here is a simple example.Cleanup drop materialized view mv . insert into t select * from t_backup . T_KEY AMT_SUM ---------. drop materialized view log on t .---------10 1 100 20 1 300 30 1 200 40 2 250 50 2 150 create materialized view mv as select t_key t_key .

or MAX • the defining query's SELECT clause must contain all the columns listed in the GROUP BY clause In addition to these restrictions some additional columns may be required in the defining query to allow it to be fast refreshable in all cases. Recommendations . MIN. The table below summarized these requirements. STDDEV." o "Specify with ROWID and INCLUDING NEW VALUES. AVG. For our current test case the most significant restrictions are these. deletes.) Oracle recommends including the Optional Aggregates expressions to obtain the most efficient and accurate fast refresh of the materialized view. • all base tables must have materialized view logs that: o "Contain all columns from the table referenced in the materialized view. Materialized Views with Aggregates: Requirements for Refresh Fast After Any DML Aggregate COUNT(expr) MIN(expr) MAX(expr) SUM(expr) SUM(col) AVG(expr) Additional Aggregates Required COUNT(*) COUNT(*) COUNT(*) COUNT(*) COUNT(expr) COUNT(*) SUM(expr) SUM(expr*expr) SUM(expr*expr) "col" must have a NOT NULL constraint Optional Aggregates Note defining query must have no WHERE clause defining query must have no WHERE clause COUNT(*) COUNT(expr) COUNT(*) STDDEV(expr) COUNT(expr) SUM(expr) COUNT(*) VARIANCE(expr) COUNT(expr) SUM(expr) (For insert-only materialized views see Table 8-2 Requirements for Materialized Views with Aggregates. COUNT." • aggregates in the defining query must be either SUM. VARIANCE. and updates.additional restrictions are fully documented at Restrictions on Fast Refresh on Materialized Views with Aggregates." o "Specify the SEQUENCE clause if the table is expected to have a mix of inserts/direct-loads.

Whenever I need to create this type of materialized view in an application I use the code below as a starting point to remind me of the requirements.The recommendation about gathering statistics that we saw in the Join Queries topic also applies to materialized views with aggregates. Oracle Database needs the statistics generated by this package to optimize query rewrite. 'MV' ) . sum(amt) as amt_sum . count(amt) as amt_count from t2 group by t_key ." -. "After you create the materialized view. sequence ( t_key. to confirm it can be refreshed in all required situations. execute dbms_stats. set long 5000 select my_mv_capabilities( 'MV'. MY_MV_CAPABILITIES. MV_REPORT ------------------------------------------------------------------------------Capable of: . The Prototype Applying these restrictions and recommendations to our test case above yields the following prototypical materialized view with aggregates. Whenever we create a fast refreshable view we should use our EXPLAIN_MVIEW utility. create materialized view log on t2 with rowid. 'REFRESH' ) as mv_report from dual . To improve the performance of such queries we will therefore add indexes to our materialized view's GROUP BY columns. amt ) including new values . count(*) as row_count . drop materialized view mv . create materialized view mv refresh fast on commit enable query rewrite as select t_key .gather_table_stats( user. you must collect statistics on it using the DBMS_STATS package. create index mv_i1 on mv ( t_key ) .from CREATE MATERIALIZED VIEW. Additionally we also expect that our GROUP BY columns will often be specified in WHERE or JOIN clauses.

3. 'REWRITE' ) as mv_report from dual . Query Rewrite Materialized views containing aggregates can be used by the query rewrite facility (see ENABLE QUERY REWRITE). T_KEY AMT_SUM ROW_COUNT AMT_COUNT ---------.---------. alter materialized view mv enable query rewrite . insert into t2 values ( 60.---------1 600 3 3 2 0 2 2 3 300 1 1 Both changes are reflected in MV. as expected. commit. MV_REPORT ------------------------------------------------------------------------------- . First.REFRESH_COMPLETE REFRESH_FAST REFRESH_FAST_AFTER_INSERT REFRESH_FAST_AFTER_ONETAB_DML REFRESH_FAST_AFTER_ANY_DML Now let's test drive our new MV. select * from mv order by t_key . select * from mv order by t_key . select my_mv_capabilities( 'MV'.---------. T_KEY AMT_SUM ROW_COUNT AMT_COUNT ---------. 300 ) .---------1 600 3 3 2 400 2 2 Now let's do some DML on the base table and see the effect on MV.---------.---------. here are MV's initial contents. update t2 set amt = 0 where t_key = 2 .

sum(amt) as amt_sum -count(*) as row_count .---------1 600 2 0 3 300 Let's try an INSERT.Insert-Only Materialized Views On Commit We know that COUNT(*). select * from mv2 order by t_key . but what happens if we do not include these columns? Let's find out. must be included in our materialized views for them to be fast refreshable in all cases. 3.Capable of: REWRITE REWRITE_FULL_TEXT_MATCH REWRITE_PARTIAL_TEXT_MATCH REWRITE_GENERAL Gotcha . and sometimes COUNT(expr). -count(amt) as amt_count from t2 group by t_key .---------1 600 2 0 3 1200 . create materialized view mv2 refresh fast on commit enable query rewrite as select t_key . T_KEY AMT_SUM ---------. T_KEY AMT_SUM ---------. select * from mv2 order by t_key . 900 ) . insert into t2 values ( 70. commit .

select * from mv2 order by t_key . 3 rows deleted.Looks good. until I re-read the page a third time and followed up on this seemingly inconsequential little comment.---------1 600 2 0 3 1200 Oops. The view was fast refreshed after the transaction committed. After all. T_KEY AMT_SUM ---------. all the rows for T_KEY = 1 were deleted from T2 but the group still appears in MV2. Commit complete. except this time we won't get any warnings or errors if a commit fails to trigger a fast refresh. 'c' ) select * from mv2 order by t_key .REFRESH( 'MV2'. The manual page for CREATE MATERIALIZED VIEW did not mention insert-only materialized views so I had no clue to their existence. delete from t2 where t_key = 1 . So we've confirmed we have another insert-only materialized view. execute DBMS_MVIEW. when one creates a materialized view specifying that it should REFRESH FAST ON COMMIT it seems reasonable to assume it will always refresh fast on commit. commit. In topic REFRESH FAST Categories we saw how an insert-only ON DEMAND materialized view similar to this one raised an error when we attempted to fast refresh it manually after a DELETE transaction. Let's see how our ON COMMIT version behaves after a DELETE. Let's try synchronizing MV2 manually using DBMS_MVIEW. T_KEY AMT_SUM ---------. When I first learned materialized views I stumbled across this behaviour by accident and found it puzzling.---------2 0 3 1200 That's a little better. The materialized view did not refresh on commit and no errors were generated.REFRESH. .

select my_mv_capabilities( 'MV2'. This is how to recognize an insert-only materialized view. MV_REPORT ------------------------------------------------------------------------------Capable of: REFRESH_COMPLETE REFRESH_FAST REFRESH_FAST_AFTER_INSERT Not Capable of: REFRESH_FAST_AFTER_ONETAB_DML AMT_SUM SUM(expr) without COUNT(expr) REFRESH_FAST_AFTER_ONETAB_DML COUNT(*) is not present in the select list REFRESH_FAST_AFTER_ANY_DML see the reason why REFRESH_FAST_AFTER_ONETAB_DML is disabled The report tells us MV2 is fast refreshable after insert. drop materialized view mv2 . refer to Oracle Database Advanced Replication and Oracle Database Data Warehousing Guide. So the lesson here is do not assume materialized views created with REFRESH FAST ON COMMIT will always refresh fast on commit.from CREATE MATERIALIZED VIEW This eventually lead me to learn about insert-only refreshing and how indispensable the DBMS_MVIEW.EXPLAIN_MVIEW to see whether or not it is an "insert-only" materialized view. insert into t2 select * from t2_backup . commit. Always check it with DBMS_MVEW." -.EXPLAIN_MVIEW utility is when working with fast refreshable materialized views. 'REFRESH' ) as mv_report from dual . For instructions on actually implementing the refresh.EXPLAIN_MVIEW has to say about MV2. but not after other types of DML. Cleanup drop materialized view mv . as we saw earlier. Let's see what DBMS_MVIEW."(The REFRESH clause) only sets the default refresh options. drop materialized view log on t2 . . delete t2 .

For example. amt ) including new values . t_key. T_KEY AMT_MAX T2_KEY_OF_AMT_MAX ---------. create materialized view log on t2 with rowid .) As always the first step is to create a materialized view log on T2. max(key) keep ( dense_rank last order by amt ) as t2_key_of_amt_max from t2 group by t_key . sequence ( key.EXPLAIN_MVIEW) tells us about our query.---------.----------------1 300 20 2 250 40 (T2_KEY_OF_AMT_MAX identifies the KEY value associated with the highest AMT value in each group.Nested Materialized Views Sometimes a single materialized view will not meet our requirements. T_KEY AMT KEY ---------. given this base table select t_key.---------1 100 10 1 200 30 1 300 20 2 150 50 2 250 40 say we wanted a fast refreshable materialized view defined with the following query. set long 5000 select .---------. Now let's see what the MY_MV_CAPABILITIES utility (created in topic DBMS_MVIEW. key . amt. amt. key from t2 order by t_key. select t_key t_key . max(amt) amt_max .

STDDEV. COUNT.amt ) in ( select t_key.my_mv_capabilities( 'select t_key t_key . AVG. max(amt) amt_max . max(amt) from t2 group by t_key . max(key) keep ( dense_rank LAST order by amt ) as t2_key_of_amt_max from t2 group by t_key' . select my_mv_capabilities( 'select t_key t_key . Let's try writing the query using a subquery instead of LAST. max(key) as t2_key_of_amt_max from t2 where ( t_key. VARIANCE. it turns out our query is not fast refreshable because the LAST aggregate function which we used to implement T2_KEY_OF_AMT_MAX is not one of the fast refreshable aggregates SUM. MIN and MAX (see Restrictions on Fast Refresh on Materialized Views with Aggregates). max(amt) amt_max . MV_REPORT ------------------------------------------------------------------------------Capable of: REFRESH_COMPLETE Not Capable of: REFRESH_FAST REFRESH_FAST_AFTER_INSERT aggregate function nested within an expression REFRESH_FAST_AFTER_ONETAB_DML see the reason why REFRESH_FAST_AFTER_INSERT is disabled REFRESH_FAST_AFTER_ONETAB_DML mv uses the MIN or MAX aggregate functions REFRESH_FAST_AFTER_ANY_DML see the reason why REFRESH_FAST_AFTER_ONETAB_DML is disabled Though not entirely obvious from the report. 'REFRESH' ) as mv_report from dual . t2.

MV_REPORT ------------------------------------------------------------------------------Capable of: REFRESH_COMPLETE . last_value( key ) over ( partition by t_key order by amt range between unbounded preceding and unbounded following ) as t2_key_of_amt_max from t2' .) group by t_key' . max( amt ) over ( partition by t_key ) as amt_max . 'REFRESH' ) as mv_report from dual . Perhaps an analytic approach will work? select my_mv_capabilities( 'select distinct t_key t_key . MV_REPORT ------------------------------------------------------------------------------Capable of: REFRESH_COMPLETE Not Capable of: REFRESH_FAST REFRESH_FAST_AFTER_INSERT subquery in mv REFRESH_FAST_AFTER_ONETAB_DML see the reason why REFRESH_FAST_AFTER_INSERT is disabled REFRESH_FAST_AFTER_ONETAB_DML mv uses the MIN or MAX aggregate functions REFRESH_FAST_AFTER_ANY_DML see the reason why REFRESH_FAST_AFTER_ONETAB_DML is disabled It looks like a subquery will not work either. 'REFRESH' ) as mv_report from dual .

Note that all base objects in a nested materialized view. For nested materialized views they are these. We need to rethink our approach. Since deriving the desired result set is conceptually a three step process 1. find the highest KEY value per AMT 3. which we will call MV1 and MV2. join the results of steps (1) and (2) together on the AMT column perhaps three separate materialized views would work? The materialized views for steps 1 and 2. which we will call MV3. regardless of whether they are tables or materialized views. However the materialized view for step 3. Note the term "Nested Materialized View" does not refer to MV1 and MV2. • If REFRESH FAST is specified then all materialized views in any chain related to the materialized view must also specify REFRESH FAST. Fortunately Oracle allows for a materialized view like MV3 and automatically manages the refresh order when all three views are refreshable on commit. Materialized views like MV3 are called "Nested Materialized Views". whether they are tables or materialized views. are treated as tables. will need to be based on MV1 and MV2 and will need to refresh after they do. which is a bit of a relief actually since the technique is rather crass. find the highest AMT value 2. even though they could be thought of as being "nested" within MV3. • The defining query must contain joins or aggregates. must each have materialized view logs. can be based on table T2 and can be refreshed independently of each other. before creating a type of materialized view we have not tried before we must be aware of its restrictions. . Restrictions and Recommendations As always. • All base objects.Not Capable of: REFRESH_FAST REFRESH_FAST_AFTER_INSERT DISTINCT clause in select list in mv REFRESH_FAST_AFTER_INSERT DISTINCT clause in select list in mv REFRESH_FAST_AFTER_INSERT window function in mv REFRESH_FAST_AFTER_ONETAB_DML see the reason why REFRESH_FAST_AFTER_INSERT is disabled REFRESH_FAST_AFTER_ANY_DML see the reason why REFRESH_FAST_AFTER_ONETAB_DML is disabled This last approach did not work either. • The base materialized views must contain joins or aggregates.

amt_count.We are now ready to craft our three step solution. row_count ) including new values . count(amt) amt_count . sequence . max(key) max_key_per_amt . create materialized view log on mv1 with rowid . create materialized view MV1 refresh fast on commit as select t_key . MV_REPORT ------------------------------------------------------------------------------Capable of: REFRESH_COMPLETE REFRESH_FAST REFRESH_FAST_AFTER_INSERT REFRESH_FAST_AFTER_ANY_DML create materialized view MV2 refresh fast on commit as select t_key . count(*) row_count from t2 group by t_key . select my_mv_capabilities( 'MV1'. amt_max. count(*) row_count from t2 group by t_key. create materialized view log on mv2 with rowid . sequence ( t_key. 'REFRESH' ) as mv_report from dual . amt . amt . max(amt) amt_max .

max_key_per_amt as t2_key_of_amt_max .rowid mv2_rowid from mv1.t_key and mv1. MV_REPORT ------------------------------------------------------------------------------Capable of: REFRESH_COMPLETE REFRESH_FAST REFRESH_FAST_AFTER_INSERT REFRESH_FAST_AFTER_ANY_DML create materialized view MV3 refresh fast on commit as select mv1. max_key_per_amt. 'REFRESH' ) as mv_report from dual .amt_max . row_count ) including new values .rowid mv1_rowid .( t_key. mv2. mv1. mv2 where mv1. mv2.amt_max = mv2. select my_mv_capabilities( 'MV3'.amt . 'REFRESH' ) as mv_report from dual . select my_mv_capabilities( 'MV2'.t_key .t_key = mv2. MV_REPORT ------------------------------------------------------------------------------Capable of: REFRESH_COMPLETE REFRESH_FAST REFRESH_FAST_AFTER_INSERT REFRESH_FAST_AFTER_ONETAB_DML REFRESH_FAST_AFTER_ANY_DML . mv1.

amt. amt_max. amt. key .---------1 100 10 1 300 20 1 300 30 2 150 50 2 250 40 3 650 60 Now we check MV3 to see if it contains the correct info. select t_key. 3. It matches the results returned by the first query we tried which used the LAST function. 3.----------------1 300 20 2 250 40 Good. amt_max. update t2 set amt = 300 where key = 30 . First we perform a few mixed DML transactions. update t2 set amt = 650 where key = 60 . select t_key. the nested one. Let's confirm that MV3. Now let's put all three materialized views through their paces. commit. T_KEY AMT_MAX T2_KEY_OF_AMT_MAX ---------. insert into t2 values ( 60. select t_key. commit. 550 ).We finally have a fast refreshable materialized view solution. contains the correct results. insert into t2 values ( 70.----------------1 300 30 2 250 40 3 650 60 It does. Mission accomplished. key from t2 order by t_key.---------. t2_key_of_amt_max from mv3 order by t_key .---------. t2_key_of_amt_max from mv3 order by t_key . T_KEY AMT_MAX T2_KEY_OF_AMT_MAX ---------. Cleanup .---------. delete from t2 where key = 70 . 450 ). T_KEY AMT KEY ---------.

amt number ) . Setup Run the code on this page in SQL*Plus to create the sample tables. ). insert into t2 select * from t2_backup . 100 300 200 250 150 ) ) ) ) ) . drop materialized view log on t2 . drop materialized view mv3 . 2. . 2. 30. ). . used by the examples in this section. create table t ( key number . 3. drop materialized view mv2 . 50. 1. 4. commit. 1. delete t2 . 2. 'a' 'b' 'c' null ). 20. t_key number . . create table t2_backup as select * from t2 . 1.drop materialized view mv1 . 40. . etc. Cleanup . insert insert insert insert commit. create table t_backup as select * from t . ). val varchar2(5) ) . primary key not null references t not null values values values values values ( ( ( ( ( 10. insert into insert into insert into insert into insert into commit. t2 t2 t2 t2 t2 into into into into t t t t values values values values primary key ( ( ( ( 1. data. create table t2 ( key number .

WARNING!! --. drop table t . and VARIABLE commands) exit your SQL*Plus session after running these cleanup commands.g.unless you specifically created it for this tutorial and no longer wish to -.MV_CAPABILITIES_TABLE is an Oracle table. -------------------------------------------------------------------------------. etc. do not drop it from your schema -.use it ------------------------------------------------------------------------------drop table mv_capabilities_table . exit . COLUMN. To clear session state changes (e. drop table t2 . created in earlier parts of this section. drop table t_backup . drop function my_mv_capabilities . drop table t2_backup . procedures. those made by SET.Run the code on this page to drop the sample tables.

null. nvl( group_2.any] . num_val[ 'Total 3 . with a single command. null.'a2'] . create a report containing ad-hoc totals like this. null. num_val[ 'Total 2 .A + a2'. 'n/a' ) as group_1 . num_val[ 'Total 4 .MODEL Clause This section presents tutorials on the MODEL clause of the SELECT command.'C'). It also adds procedural features to SQL previously available only through PL/SQL calls. null ] = sum(num_val)[any. For example. the MODEL clause is a powerful feature that gives you the ability to change any cell in the query's result set using data from any other cell (similar to the way a spreadsheet works). null ] = . num_val from t model dimension by ( cast(key as varchar2(20)) as key . null.any] . 'n/a' ) as group_2 ) measures( num_val ) rules ( num_val[ 'Total 1 . with MODEL you can take a simple table like this KEY -----1 2 3 4 5 6 7 8 9 10 GROUP_1 ---------A A A B B B C C GROUP_2 ---------a1 a2 a3 a1 a2 a3 a1 a2 a1 a2 DATE_VAL NUM_VAL ---------.A + C'.'A'. group_2 .a1 + a3'.any] + sum(num_val)[any. group_1 .------2005-01-01 100 2005-06-12 200 300 2006-02-01 2006-06-12 300 2005-01-01 100 2006-06-12 100 2005-02-01 2005-02-01 200 800 and.group_1 in ('A'.'n/a'. null ] = sum(num_val)[any.n/a'. Introduced in Oracle 10g. nvl( group_1.group_1 <> 'A'. set null "" select case when key like 'Total%' then key else null end as total . null ] = sum(num_val)[any.

2 ) as string from t where num_val is not null model return updated rows partition by ( group_1 ) dimension by ( row_number() over (partition by group_1 order by num_val) as position ) measures ( cast( num_val as varchar2(65) ) as string ) -.) . or impossible to do with a non-MODEL SELECT command. TOTAL GROUP_1 -------------------.0) = 0 ) ( string[0] = string[0] || '. GROUP_1 ---------A B C (null) STRING ---------------------------------------100. total nulls first . inefficient. set null "(null)" column string format a40 select group_1.any.' || string[iteration_number+1] ) order by group_1 .200.A + C Total 2 .1. Here is an example.group_2 in ('a1'. group_2 .Note 1 rules upsert iterate( 6 ) until ( presentv(string[iteration_number+2].------a1 100 a2 200 a3 300 a1 a2 300 a3 100 a1 100 a2 a1 200 a2 800 700 1700 1000 800 You can also use MODEL'S procedural features to produce results that are difficult.'a3')] ) order by group_1 .n/a Total 4 .300 100 200.A + a2 Total 3 . substr( string.sum(num_val)[any.---------A A A B B B C C n/a n/a Total 1 .800 (This last technique is explained fully in another section of SQL Snippets at Rows to String: MODEL Method 1.a1 + a3 GROUP_2 NUM_VAL ---------.300 100.

Though powerful, the MODEL clause is also somewhat complex and this can be intimidating when you read about it for the first time. The tutorials to follow will therefore present very simple MODEL examples to help you quickly become comfortable with its many features. Before continuing it is important to know that everything in the MODEL clause is evaluated after all other clauses in the query, except for SELECT DISTINCT and ORDER BY. Knowing this will help you better understand the examples in this section's tutorials.

DIMENSION BY
In this tutorial we learn about the DIMENSION BY component of the MODEL clause. DIMENSION BY specifies which columns in a SELECT statement are dimension columns, which for our purposes can be thought of as any column that serves to identify each row in the result of a SELECT statement. By default, the dimension columns in a MODEL clause must produce a unique key for the result set. See the Oracle® Database Data Warehousing Guide 10g Release 2 (10.2) - Glossary for a formal definition. Before we begin please note that, on its own, DIMENSION BY has little visible effect on the output of the SELECT statement. Most of the examples below would produce the same result as one with no MODEL clause at all. This is because we are not trying to manipulate the results just yet. We are simply seeing how to specify our dimension columns, which is a precursor for learning to manipulate results in subsequent pages. Consider the following table.
select key, key_2, group_1, group_2, num_val from t order by key ; KEY -----1 2 3 4 5 6 7 8 9 10 KEY_2 ----T-1 T-2 T-3 T-4 T-5 T-6 T-7 T-8 T-9 T-10 GROUP_1 ---------A A A B B B C C GROUP_2 NUM_VAL ---------- ------a1 100 a2 200 a3 300 a1 a2 300 a3 100 a1 100 a2 a1 200 a2 800

We see that KEY, KEY_2, and (GROUP_1, GROUP_2) all uniquely identify each row in the table. They are therefore dimension column candidates. To let Oracle know which column(s) we plan to use as dimensions we compose a MODEL clause like this. (Ignore the MEASURES and RULES clauses for now. We will explore those later.)

select key, num_val from t model DIMENSION BY ( KEY ) measures ( num_val ) rules () order by key ; KEY NUM_VAL ------ ------1 100 2 200 3 300 4 5 300 6 100 7 100 8 9 200 10 800

Multiple Dimensions If needed, you can define more than one dimension column, as this example shows.
select group_1, group_2, num_val from t model DIMENSION BY ( GROUP_1, GROUP_2 ) measures ( num_val ) rules () order by group_1, group_2 ; GROUP_1 ---------A A A B B B C C GROUP_2 NUM_VAL ---------- ------a1 100 a2 200 a3 300 a1 a2 300 a3 100 a1 100 a2 a1 200 a2 800

You can even include columns in the DIMENSION BY clause which are not required to uniquely identify each result row.
select key, date_val, num_val from t model DIMENSION BY ( KEY, DATE_VAL ) -- date_val not required to uniquely identify row measures ( num_val ) rules () order by key ; KEY -----1 2 3 4 5 6 7 8 9 10 DATE_VAL NUM_VAL ---------- ------2005-01-01 100 2005-06-12 200 300 2006-02-01 2006-06-12 300 2005-01-01 100 2006-06-12 100 2005-02-01 2005-02-01 200 800

Aliasing You cannot use SELECT clause aliases in DIMENSION BY. Here are some examples of aliases that will cause errors.
select KEY AS KEY_3, num_val from t model dimension by ( KEY_3 ) measures ( num_val ) rules () ; dimension by ( KEY_3 ) * ERROR at line 7: ORA-00904: "KEY_3": invalid identifier

select KEY * 10 AS KEY_3, num_val from t model

select KEY_3.------1 100 2 200 3 300 4 5 300 6 100 7 100 8 9 200 10 800 select KEY_3. KEY_3 NUM_VAL ---------. num_val . dimension by ( KEY_3 ) * ERROR at line 7: ORA-00904: "KEY_3": invalid identifier You can however alias such expressions directly in DIMENSION BY. num_val from t model dimension by ( KEY_3 ) measures ( num_val ) rules () . dimension by ( KEY_3 ) measures ( num_val ) rules () dimension by ( KEY_3 ) * ERROR at line 7: ORA-00904: "KEY_3": invalid identifier select ROWNUM AS KEY_3.. num_val from t model DIMENSION BY ( KEY AS KEY_3 ) measures ( num_val ) rules () order by key_3 .

if your DIMENSION BY columns do not give you a unique key for your result set you will get an error. KEY_3 NUM_VAL ---------. KEY_3 NUM_VAL ---------.------1 100 2 200 3 300 4 5 300 6 100 7 100 8 9 200 10 800 Uniqueness By default. num_val from .------10 100 20 200 30 300 40 50 300 60 100 70 100 80 90 200 100 800 select KEY_3.from t model DIMENSION BY ( KEY * 10 AS KEY_3 ) measures ( num_val ) rules () order by key_3 . num_val from t model DIMENSION BY ( ROWNUM AS KEY_3 ) measures ( num_val ) rules () order by key_3 . select group_2.

. See the Oracle® Database Data Warehousing Guide 10g Release 2 (10. GROUP_2 NUM_VAL ---------. t * ERROR at line 5: ORA-32638: Non unique addressing in MODEL dimensions This rule can be relaxed somewhat by specifying UNIQUE SINGLE REFERENCE. MEASURES In this tutorial we learn about the MEASURES component of the MODEL clause. num_val from t model UNIQUE SINGLE REFERENCE dimension by ( group_2 ) measures ( num_val ) rules () order by group_2 . MEASURES specifies which columns in a SELECT are measure columns.group_2 is not unique Note that UNIQUE SINGLE REFERENCE affects the types of RULES you can define.group_2 is not unique measures ( num_val ) rules () order by group_2 . This is explained further in Expressions and Cell References.Glossary for a formal definition. which for our purposes can be thought of as any column containing a measurable quantity like a price or a length. select group_2.------a1 100 a1 100 a1 200 a1 a2 800 a2 200 a2 300 a2 a3 300 a3 100 -.2) .t model DIMENSION BY ( GROUP_2 ) -.

------1 100 2 200 3 300 4 5 300 6 100 . group_2. KEY NUM_VAL -----.Before we begin please note that. Most of the examples below would produce the same result as one with no MODEL clause at all. We will see how to actually manipulate our output when we explore the RULES clause in subsequent tutorials. then all other columns are available for use as measure columns.------2005-01-01 100 2005-06-12 200 300 2006-02-01 2006-06-12 300 2005-01-01 100 2006-06-12 100 2005-02-01 2005-02-01 200 800 If we decide to use KEY as our sole dimension column. select key. We are simply seeing how to specify our measure columns. select key. date_val. MEASURES has little visible effect on the output of the SELECT statement. To let Oracle know we want to use the NUM_VAL column as our measure we can compose a MODEL clause like this. Before we see MEASURES in action first consider the following table. This is because we are not trying to manipulate the results just yet. group_1. on its own. num_val from t order by key . KEY -----1 2 3 4 5 6 7 8 9 10 GROUP_1 ---------A A A B B B C C GROUP_2 ---------a1 a2 a3 a1 a2 a3 a1 a2 a1 a2 DATE_VAL NUM_VAL ---------. which is a precursor to manipulating the results. num_val from t model dimension by ( key ) MEASURES ( NUM_VAL ) rules () order by key .

7 8 9 10 100 200 800 If we want to include more measure columns we do it like this. num_val. num_val from t model dimension by ( key ) MEASURES ( DATE_VAL. date_val_2. date_val.------------ . SYSDATE AS DATE_VAL_2 .------2005-01-01 100 2005-06-12 200 300 2006-02-01 2006-06-12 300 2005-01-01 100 2006-06-12 100 2005-02-01 2005-02-01 200 800 You can define measures using constants and expressions instead of simple column names. like this. num_val_2. note from t model dimension by ( key ) MEASURES ( num_val . KEY -----1 2 3 4 5 6 7 8 9 10 DATE_VAL NUM_VAL ---------. select key.---------.------. 'A BRIEF NOTE' AS NOTE ) rules( ) order by key . NUM_VAL * 10 AS NUM_VAL_2 . select key. NUM_VAL ) rules () order by key .---------. KEY NUM_VAL NUM_VAL_2 DATE_VAL_2 NOTE -----.

1 2 3 4 5 6 7 8 9 10 100 200 300 300 100 100 200 800 1000 2007-02-28 A BRIEF 2000 2007-02-28 A BRIEF 3000 2007-02-28 A BRIEF 2007-02-28 A BRIEF 3000 2007-02-28 A BRIEF 1000 2007-02-28 A BRIEF 1000 2007-02-28 A BRIEF 2007-02-28 A BRIEF 2000 2007-02-28 A BRIEF 8000 2007-02-28 A BRIEF NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE .

For all the remaining aggregate functions. count_all_rows .---------. This means that. Ignoring Nulls According to the SQL Reference Manual section on Aggregate Functions: All aggregate functions except COUNT(*) and GROUPING ignore nulls. but returns either a number or zero.---------. Techniques for generating results that ignore nulls and results that include nulls are highlighted. max_val .-----------------Group-1 (null) (null) 2 0 0 . min_val . or contains only rows with nulls as arguments to the aggregate function.---------.Nulls Nulls and Aggregate Functions This tutorial demonstrates how aggregate functions deal with null values. given a table with values like this GROUP_KEY ---------Group-1 Group-1 Group-2 Group-2 Group-2 Group-2 Group-2 Group-3 Group-3 Group-3 VAL ---------(null) (null) a a z z (null) A A Z aggregate functions like MAX . if the data set contains no rows. select group_key . MIN . MAX( VAL ) MIN( VAL ) COUNT( * ) COUNT( VAL ) COUNT( DISTINCT VAL ) from t1 group by group_key order by group_key . then the function returns null. like these. count_distinct_val GROUP_KEY MAX_VAL MIN_VAL COUNT_ALL_ROWS COUNT_VAL COUNT_DISTINCT_VAL ---------. and COUNT will return values that for the most part ignore nulls. COUNT never returns null. You can use the NVL function in the argument to an aggregate function to substitute a value for a null. count_val .-------------.

and COUNT(DISTINCT . MEDIAN. which we can use as follows to give us MAX and MIN results that include nulls. using the same test data as above GROUP_KEY ---------Group-1 Group-1 Group-2 Group-2 Group-2 Group-2 Group-2 Group-3 Group-3 Group-3 VAL ---------(null) (null) a a z z (null) A A Z we may wish to produce results like these. MAX( VAL ) KEEP ( DENSE_RANK LAST ORDER BY VAL ) max_val . Two of them are the FIRST and LAST functions. Fortunately for us there are aggregate functions in addition to COUNT(*) and GROUPING which do not ignore nulls. GROUP_KEY ---------Group-1 Group-2 Group-3 MAX_VAL ---------(null) (null) Z MIN_VAL COUNT_DISTINCT_VAL_1 ---------.) however. For example. MIN( VAL ) KEEP ( DENSE_RANK FIRST ORDER BY VAL ) min_val from t1 group by group_key order by group_key . we sometimes need results that take nulls into account.Group-2 Group-3 z Z a A 5 3 4 3 2 2 Note how MAX_VAL contains the same results for Group-2 and Group-3.. MIN.-------------------(null) 1 a 3 A 2 For the MAX and MIN cases it helps to take the statement "all aggregate functions except COUNT(*) and GROUPING ignore nulls" with a grain of salt. Note also that only COUNT_ALL_ROWS returned a count that included null values. even though Group-2 contains null VAL values and Group-3 does not. and SUM including nulls in the calculation is of little practical use. The other two versions of COUNT() ignored null values. GROUP_KEY MAX_VAL MIN_VAL . For aggregate functions like MAX. select group_key. Including Nulls For mathematical aggregate functions like AVG..

If the VAL column contained values whose DUMP output is truncated then the results can be incorrect. select group_key.-------------. COUNT( DISTINCT VAL ) + MAX( NVL2(VAL.-------------------.1) ) count_distinct_val_2 from t1 group by group_key order by group_key . . given test data like this (analytic value rankings are included for clarity) GROUP_KEY ---------Group-1 Group-1 Group-2 Group-2 Group-2 Group-2 Group-2 Group-3 Group-3 Group-3 VAL VAL_DENSE_RANK VAL_RANK ---------. DENSE_RANK and RANK include nulls by default. as with FIRST and LAST.---------(null) 1 1 (null) 1 1 a a z z (null) A A Z 1 1 2 2 3 1 1 2 1 1 3 3 5 1 1 3 The following results show how the aggregate versions of DENSE_RANK and RANK do not ignore nulls. COUNT( DISTINCT DUMP(VAL) ) count_distinct_val_1 .-------------------Group-1 1 1 Group-2 3 3 Group-3 2 2 Be careful with the DUMP approach though since DUMP's output is truncated at 4000 characters. GROUP_KEY COUNT_DISTINCT_VAL_1 COUNT_DISTINCT_VAL_2 ---------. Fortunately. For example. select group_key .---------Group-1 Group-2 Group-3 ---------(null) (null) Z ---------(null) a A For the COUNT( DISTINCT VAL ) case two possible approaches for including nulls are demonstrated below. DENSE_RANK and RANK Two more aggregate functions where including nulls in the calculation may be necessary are the DENSE_RANK and RANK functions.0.

as you will recall. max( NVL( VAL. A simple application of this logic can lead to trouble however. 'z' ) ) count_distinct_val from t1 group by group_key order by group_key . MAX( VAL ) ) max_val .---------------------Group-1 1 1 Group-2 3 5 Group-3 3 4 Gotchas Some people reading these two sentences from the manual All aggregate functions except COUNT(*) and GROUPING ignore nulls. 'z' ) ) max_val .DENSE_RANK( NULL ) WITHIN GROUP ( ORDER BY VAL ) null_dense_rank_within_group . GROUP_KEY ---------Group-1 Group-2 Group-3 MAX_VAL ---------(null) (null) Z MIN_VAL COUNT_DISTINCT_VAL_1 ---------. select group_key . min( NVL( VAL. say we choose to substitute all null values with a 'z'. '~' ) ). Taking the NVL idea a little further programmers sometimes employ more complex solutions such as this one. DECODE( MAX( NVL( VAL. '~'.-------------------(null) 1 a 3 A 2 A simple application of NVL clearly will not do then. like this. For example. .-----------------z 1 a 2 A 2 Note how none of the columns above contain the desired results which. 'z' ) ) min_val . GROUP_KEY NULL_DENSE_RANK_WITHIN_GROUP NULL_RANK_WITHIN_GROUP ---------. count( distinct NVL( VAL.---------------------------. GROUP_KEY ---------Group-1 Group-2 Group-3 MAX_VAL ---------z z Z MIN_VAL COUNT_DISTINCT_VAL ---------. select group_key . NULL. RANK( NULL ) WITHIN GROUP ( ORDER BY VAL ) null_rank_within_group from t1 group by group_key order by group_key . are these. may infer that aggregate functions can be made to treat null values the same way they treat nonnull values by simply using NVL to substitute nulls with some non-null value. You can use the NVL function in the argument to an aggregate function to substitute a value for a null.

'~' ) ) count_distinct_val from t1 group by group_key order by group_key . 'Group-4'. For example. max( val ) ) max_val . min( val ) ) min_val . GROUP_KEY ---------Group-4 Group-5 MAX_VAL ---------(null) ~~~ MIN_VAL COUNT_DISTINCT_VAL ---------. 'Group-5'. null. null ).-------------------~ 2 ~~~ 2 but using the NVL approach gives us these. '~~~' ). '~' ) ). 'Group-4'. given this data insert into t1 values ( 10. select group_key . NULL. '~'. '~'. decode( max( nvl(val. 'Group-5'. ). count( distinct nvl( val. decode( min( nvl(val. the results should be GROUP_KEY ---------Group-4 Group-5 MAX_VAL ---------(null) (null) MIN_VAL COUNT_DISTINCT_VAL_1 ---------. null. MIN( VAL ) ) min_val . '~' ) ) count_distinct_val from t1 where group_key in ( 'Group-4'.-----------------(null) 1 (null) 2 To avoid these gotchas simply use the non-NVL alternatives presented under "Including Nulls" above.DECODE( MIN( NVL( VAL. incorrect results. '~' ) ). COUNT( distinct NVL( VAL.-----------------(null) 1 a 3 A 2 Without some mechanism to ensure '~' and strings that sort higher than '~' never appear in VAL however. '~'. insert into t1 values ( 10. GROUP_KEY ---------Group-1 Group-2 Group-3 MAX_VAL ---------(null) (null) Z MIN_VAL COUNT_DISTINCT_VAL ---------. . null insert into t1 values ( 10. insert into t1 values ( 10. '~' ) ). these solutions will fail if such values are ever inserted into the table. 'Group-5' ) group by group_key order by group_key . '~' ).

OR with IS NULL While a bit cumbersome. SQL + PL/SQL These techniques work in both SQL and PL/SQL. If this is the behavior you need.---------. a null is not considered to be equal to or unequal to any value (including another null). However.---------1 A A Row 4 is not returned because. select * from t where ( C2 = C3 OR ( C2 IS NULL AND C3 IS NULL ) ). if you need a query that returns row 1 and row 4 then try one of the solutions in the subtopics to follow. select * from t where c2 = c3 . this basic solution is the easiest to understand and implement.Nulls and Equality In SQL you should always consider the effect of null values when comparing two values for equality (or any type of comparison for that matter). then read no further. select * from t. C1 --1 4 C2 ---------A (null) C3 ---------A (null) begin for r in ( select * from t ) loop . Consider a table where two of its columns can contain null values. in SQL. C1 C2 C3 --. C1 --1 2 3 4 C2 ---------A A (null) (null) C3 ---------A B A (null) If we attempt a SELECT statement like the following we will only get row 1.

C2 IS NULL AND R.' ).c1 || ' contains matching values. null ). commit. If we look at the table definition for T desc t Name Null? Type ---------------------------------------------.if ( R. insert into t values( 5. or whatever value you choose to use. 'x' ) . 'x' ) = nvl( c3. select * from t where nvl( c2.C2 = R. C1 --1 4 C2 ---------A (null) C3 ---------A (null) One problem with this solution is that the replacement value "x". end loop. 'x' ) = nvl( c3.-------------------------------------C1 NUMBER C2 VARCHAR2(10) . 'x' ) . like this. NVL The following NVL approach is a popular one. / Row 1 contains matching values.C3 IS NULL ) ) then dbms_output. end. Row 4 contains matching values. end if. select * from t where nvl( c2.C3 OR ( R. . This would cause a SELECT statement that has been working properly until that day to all of a sudden start returning the wrong answer. C1 --1 4 5 C2 ---------A (null) x C3 ---------A (null) (null) The trick to making this solution bullet proof is to choose a replacement value that can never appear in either of the columns being compared.put_line( 'Row ' || r. 'x'. might be inserted into columns C2 or C3 some day.

create function SAME( p_1 in varchar2.put_line( 'Row ' || r. '12345678901' ) = nvl( c3. / select * from t where SAME( C2. C1 --1 4 C2 ---------A (null) C3 ---------A (null) Custom Function If you do these comparisons frequently you may wish to create a custom database function like this one. select * from t where nvl( c2. R.C3 ) = 'Y' then dbms_output.c1 || ' contains matching values. p_2 in varchar2 ) return varchar2 is begin return ( case when p_1 is null and p_2 is null then 'Y' when p_1 = p_2 then 'Y' else 'N' end ).' ). C3 ) = 'Y' . '12345678901' ) . end if. C1 --1 4 C2 ---------A (null) C3 ---------A (null) begin for r in ( select * from t ) loop if SAME( R. . end loop.C2.C3 VARCHAR2(10) we see that values in C2 and C3 can be at most 10 characters long. Any replacement value larger than 10 characters is therefore guaranteed to never appear in either column (assuming the sizes of C2 or C3 are never expanded). end. end. .

To return rows where two columns contain the same value we can therefore use a command like the following. Row 4 contains matching values. Unlike the "=" operator. Here is an example. DECODE One approach uses the DECODE function. select 'Oops! This row should not be returned. C3. one for VARCHAR2 values./ Row 1 contains matching values. select * from t where DUMP(C2) = DUMP(C3) . RESULT -------------------------------------Oops! This row should not be returned. etc. 'Y'. C1 --1 4 C2 ---------A (null) C3 ---------A (null) DUMP Another approach uses the DUMP function. unfortunately only work in SQL commands. C1 --1 4 C2 ---------A (null) C3 ---------A (null) This approach has a couple of limitations however. 'N' ) = 'Y' . DECODE treats two nulls as equivalent. the output of the DUMP function is truncated at 4000 characters. 4000 ) ) . not PL/SQL commands. If the values being compared produce truncated DUMP output then the comparison can produce false positives. 4000) ) = DUMP( LPAD( 'B'.' as result from dual where DUMP( LPAD( 'A'. while more compact than the solutions presented in SQL + PL/SQL . One. . one for DATE values. SQL Only The following techniques. select * from t where DECODE( C2. With this approach however one function is required for comparing NUMBER values.

dump( 'A' ) as char_val from t where c1 = 1 . Comparing compatible datatypes such as VARCHAR2 and CHAR will fail to match any rows. no rows selected Examining the output of DUMP shows why this occurs. VARCHAR2_VAL -----------------------------CHAR_VAL -----------------------------Typ=1 Len=1: 65 Typ=96 Len=1: 65 SYS_OP_MAP_NONNULL Another approach that some have proposed uses the undocumented function SYS_OP_MAP_NONNULL. select * from t where SYS_OP_MAP_NONNULL( C2 ) = SYS_OP_MAP_NONNULL( C3 ) . and the literal 'A' is datatype 96. begin for r in ( select * from t ) loop . C2 and C3 must be the exact same datatype for the comparison to work. C1 --1 4 C2 ---------A (null) C3 ---------A (null) As with the other solutions on this page. VARCHAR2. but it is not. select 'Oops! This row should be returned.' as result from t where dump( c2 ) = dump( 'A' ) . SYS_OP_MAP_NONNULL does not work in PL/SQL.Two. The "typ=" part of the DUMP output for both terms differs because column C2 is datatype 1. CHAR. column varchar2_val format a30 column char_val format a30 fold_before select dump( c2 ) as varchar2_val .

column 10: PLS-00201: identifier 'SYS_OP_MAP_NONNULL' must be declared ORA-06550: line 6. . . / if ( SYS_OP_MAP_NONNULL( R. . ). . . select * * ERROR at line 1: ORA-01706: user function result value was too large While undocumented features such as this one are compelling. Be sure to read Using SQL Snippets ™ before executing any of these setup steps. ). 'A' 'A' null null . end loop. 3. ). create table t ( c1 number . They also make support and maintenance harder for others who need to work with your code and are not familiar with the feature. end.if ( SYS_OP_MAP_NONNULL( R. 2. their behavior or availability can change at any time making them a risky thing to include in your code.c1 || ' contains matching values. end if. Setup Run the code on this page in SQL*Plus to create the sample tables. c2 varchar2(10) . etc.put_line( 'Row ' || r.C3 ) ) then dbms_output. data.' ). 4. 'A' 'B' 'A' null ). used by the examples in this section. insert insert insert insert into into into into t t t t values( values( values( values( 1.C3 ) ) * ERROR at line 6: ORA-06550: line 6. column 5: PL/SQL: Statement ignored It also has a length limitation.C2 ) = SYS_OP_MAP_NONNULL( R. 4000 ) ) = SYS_OP_MAP_NONNULL( LPAD( 'B'. select * from dual where SYS_OP_MAP_NONNULL( LPAD( 'A'. c3 varchar2(10) ). 4000 ) ) .C2 ) = SYS_OP_MAP_NONNULL( R.

those made by SET. COLUMN. set null '(null)' set numformat 99 set serveroutput on Cleanup Run the code on this page to drop the sample tables. drop function same .g. Be sure to read Using SQL Snippets ™ before executing any of these setup steps. exit . etc. created in earlier parts of this section. To clear session state changes (e. procedures. and VARIABLE commands) exit your SQL*Plus session after running these cleanup commands.commit. drop table t .

DAY_OF_WEEK VAL ----------. t. if your data looks like this: select * from t .val from days_of_the_week d left outer join t using ( day_of_week ) order by day_of_week . select day_of_week.---------0 1 100 2 3 300 4 400 5 500 6 If you expect to write lots of queries that use the same series of integers and they are based on real world phenomena then creating a table like DAYS_OF_THE_WEEK can be the best solution. .Integer Series Generators Sometimes. For example. DAY_OF_WEEK VAL ----------.---------0 1 100 2 3 300 4 400 5 500 6 It would be useful to have a table with the numbers 0 to 6 in it so you could write an outer join query like this.---------1 100 3 300 4 400 5 500 and you want a report that looks like this DAY_OF_WEEK VAL ----------. having a way to create a series of integers greatly simplifies certain queries.

. In these cases it may be impractical or impossible to create a dedicated table that meets your needs. Integer Table Method This tutorial demonstrates how to generate a series of integers using a generic integer table. The tutorials in this section demonstrate a few of them. commit.Occassionally however. generic techniques for generating integers. for adhoc reports. end. or for a system you do not have CREATE TABLE privileges on. You can create such a table like this. / PL/SQL procedure successfully completed. you may need a different set of integers just for one specific query. Fortunately there are flexible. To load the table a simple loop like the following will do the trick.. The feature graph below will help you decide which method is best for you. 10 loop insert into integers values ( i ). Since this table only has a single indexed column we specified organization index to make this an Index-Organized table and save storage space. no custom objects N required Works in versions Y prior to 10g Performance comparison charts for all these methods are available at the end of the section on the Performance Comparison . Feature Intege MOD r Table EL Y N ROWNUM Type CONNECT CUB Pipelined + a Big Construct BY LEVEL E Function Table or Y Y Y Y Y Y N Y N Y Pure SQL solution. begin for i in -5 .Small Numbers and Performance Comparison . Other techniques are discussed in the topics listed in the menu to the left. create table integers ( integer_value integer primary key ) organization index . Table created. One of the most straightforward ways to generate a series of integers is by adding a generic integer table to your application. end loop.Large Numbers pages.

day_of_week ) where i.val from integers i left outer join t on ( i. (You can learn more about MODEL at SQL Features Tutorials: MODEL .integer_value between 0 and 6 order by i. In practice you would choose limits that anticipate the smallest and largest integers you will ever need. t.integer_value . INTEGER_VALUE -------------5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 Later.integer_value = t. DAY_OF_WEEK VAL ----------.---------0 1 100 2 3 300 4 400 5 500 6 MODEL Method This tutorial demonstrates how to generate a series of integers using the MODEL clause of the SELECT command.integer_value as day_of_week . select * from integers . select i. when you need a specific series of integers you can use the INTEGERS table like this.We used -5 and 10 as the limits of our series in this example.

Clause.) This technique only works with Oracle versions starting at 10g. Other techniques are discussed in the tutorials listed in the menu to the left. With this technique you can generate a series of integers starting at "1" using a query like this.
select integer_value from dual where 1=2 model dimension by ( 0 as key ) measures ( 0 as integer_value ) rules upsert ( integer_value[ for key from 1 to 10 increment 1 ] = cv(key) ) ; INTEGER_VALUE ------------1 2 3 4 5 6 7 8 9 10

Chaning the INCREMENT value lets us control the difference between successive values in the series.
select integer_value from dual where 1=2 model dimension by ( 0 as key ) measures ( 0 as integer_value ) rules upsert ( integer_value[ for key from 2 to 10 INCREMENT 2 ] = cv(key) ) ; INTEGER_VALUE ------------2 4 6 8 10

We can use bind variables to make the solution more generic.
variable v_first_key variable v_last_key variable v_increment execute :V_FIRST_KEY execute :V_LAST_KEY execute :V_INCREMENT number number number := 1 := 5 := 2

select key, integer_value from dual where 1=2 model dimension by ( 0 as key ) measures ( 0 as integer_value ) rules upsert ( integer_value[ for key from :V_FIRST_KEY to :V_LAST_KEY increment 1 ] = nvl2( integer_value[cv()-1], integer_value[cv()-1] + :V_INCREMENT, cv(key) ) ) ; KEY INTEGER_VALUE ---------- ------------1 1 2 3 3 5 4 7 5 9

When v_last_key is NULL or less than v_first_key no rows are returned.
execute :v_first_key := 1 PL/SQL procedure successfully completed. execute :v_last_key := null

PL/SQL procedure successfully completed. / no rows selected execute :v_last_key := 0 PL/SQL procedure successfully completed. / no rows selected execute :v_last_key := -5 PL/SQL procedure successfully completed. / no rows selected

Day of the Week Case Study

We can apply this technique to the day of the week scenario presented at the start of this chapter as follows.
select day_of_week , t.val from ( select day_of_week from dual where 1=2 model dimension by ( 0 as key ) measures ( 0 as day_of_week ) rules upsert ( day_of_week[ for key from 0 to 6 increment 1 ] = cv(key) ) ) i left outer join t using ( day_of_week ) order by day_of_week ; DAY_OF_WEEK VAL ----------- ---------0 1 100 2 3 300 4 400 5 500 6

Gotchas

Descending Series
If you need a descending series of integers this attempt will not work.
select integer_value from dual where 1=2 model dimension by ( 0 as key ) measures ( 0 as integer_value ) rules upsert ( integer_value[ for key FROM 3 TO 1 increment 1 ] = cv(key) ) ; no rows selected

Instead, do it this way
select integer_value from dual

where 1=2 model dimension by ( 0 as key ) measures ( 0 as integer_value ) rules upsert ( integer_value[ for key from 3 to 1 DECREMENT 1 ] = cv(key) ) ORDER BY INTEGER_VALUE DESC ; INTEGER_VALUE ------------3 2 1

or this way.
select integer_value from dual where 1=2 model dimension by ( 0 as key ) measures ( 0 as integer_value ) rules upsert ( integer_value[ for key from 1 TO 3 INCREMENT 1 ] = cv(key) ) ORDER BY INTEGER_VALUE DESC ; INTEGER_VALUE ------------3 2 1

WHERE 1=2
It is important to note that everything in the MODEL clause is evaluated after all other clauses in the query, except for SELECT DISTINCT and ORDER BY. Using the WHERE 1=2 clause ensures the query starts with an empty result set when MODEL rules are first applied to the rows returned by the SELECT ... FROM ... WHERE portion of the query. While it would be possible to omit the WHERE 1=2 clause using an approach like this
select integer_value from dual model dimension by ( 1 as key ) measures ( 1 as integer_value ) rules upsert ( integer_value[ for key from 1 to 10 increment 1 ] = cv(key) ) ; INTEGER_VALUE ------------1 2 3

4 5 6 7 8 9 10

this query causes the result set to always contain at least one row both before and after the MODEL rules are applied. This is not a problem for queries that always return one or more rows like this one,
select key, integer_value from dual model dimension by ( 4 as key ) measures ( 4 as integer_value ) rules upsert ( integer_value[ for key from 4 to 8 increment 1 ] = cv(key) ) ; KEY INTEGER_VALUE ---------- ------------4 4 5 5 6 6 7 7 8 8

but if the code is later parameterized and the TO bound is ever null or less than the FROM bound then the query will incorrectly return 1 row instead of the required zero rows for these cases.
variable v_first_key variable v_last_key number number

execute :v_first_key := 3 execute :v_last_key := 0 select key, integer_value from dual model dimension by ( :v_first_key as key ) measures ( :v_first_key as integer_value ) rules upsert ( integer_value[ for key from :v_first_key to :v_last_key increment 1 ] = cv(key) ) ; KEY INTEGER_VALUE ---------- ------------3 3

RETURN UPDATED ROWS

An alternative to using WHERE 1=2 would be to instead include a RETURN UPDATED ROWS clause, like this
select integer_value from dual model RETURN UPDATED ROWS dimension by ( 1 as key ) measures ( 1 as integer_value ) rules upsert ( integer_value[ for key from 1 TO 3 increment 1 ] = cv(key) ) ; INTEGER_VALUE ------------1 2 3 select integer_value from dual model RETURN UPDATED ROWS dimension by ( 1 as key ) measures ( 1 as integer_value ) rules upsert ( integer_value[ for key from 3 TO 0 increment 1 ] = cv(key) ) ; no rows selected

but using WHERE 1=2 to ensure the query always starts with an empty set seems like a cleaner way to work than starting with one row and then relying on RETURN UPDATED ROWS to return that row in some cases but not others.

INCREMENT and Bind Variables
Unlike the FROM and TO bounds, we cannot use a variable in the INCREMENT value (as tested in 10g).
variable v_first_key variable v_last_key variable v_increment execute :v_first_key execute :v_last_key execute :v_increment number number number := 1 := 9 := 2

select key, integer_value from dual where 1=2 model dimension by ( 0 as key ) measures ( 0 as integer_value ) rules upsert

1 as day_of_week .e. select day_of_week . The Solution Once you have identified a table with a sufficient number of rows simply select ROWNUM from it to generate the required integer series.val from ( select rownum . select rownum from all_objects where rownum <= 10 . ( integer_value[ for key from :v_first_key to :v_last_key INCREMENT :v_increment ] ERROR at line 8: ORA-32626: illegal bounds or increment in MODEL FOR loop * ROWNUM + a Big Table Method This tutorial demonstrates how to generate a series of integers using the ROWNUM pseudocolumn and any available table with as many rows in it as the number of integers required. t. like this. The data dictionary view ALL_OBJECTS is a popular choice for this method.( integer_value[ for key from :v_first_key to :v_last_key INCREMENT :v_increment ] = cv(key) ) . if you need a series of 10 integers then you need to find a table or view that will always have at least 10 rows in it. ROWNUM ---------1 2 3 4 5 6 7 8 9 10 We can apply this technique to our day of the week scenario as follows. Prerequisites Before using this solution you need to find a table with at least as many rows in it as the number of integers you need to generate. I. Other techniques are discussed in the tutorials listed in the menu to the left.

Other techniques are discussed in the tutorials listed in the menu to the left. Applying the technique to a table with two rows. yields some insight into how such queries work. LEVEL ---------1 2 3 4 5 6 7 8 9 10 Queries Without PRIOR The query above is a special case of a more general type of query. With this technique you can generate a series of integers starting at "1" using a query like this.from all_objects where rownum <= 7 ) i left outer join t using ( day_of_week ) order by day_of_week . "a" and "b". break on level duplicates skip 1 column path format a10 . select level from dual connect by level <= 10 . DAY_OF_WEEK VAL ----------. those that do not use the PRIOR operator.---------0 1 100 2 3 300 4 400 5 500 6 CONNECT BY LEVEL Method This tutorial demonstrates how to generate a series of integers using a novel application of the CONNECT BY clause first posted by Mikito Harakiri at Ask Tom "how to display selective record twice in the query?".

The effect also proves useful in situations where more than one integer series is required from a single query. If the number of rows is set with a bind variable whose value can be 0. negative.select from connect by order by LEVEL ---------1 1 2 2 2 2 3 3 3 3 3 3 3 3 level. or null however. key t4 level <= 3 level. See Multiple Integer Series: CONNECT BY LEVEL Method for more details and an example. This effect may be useful where an exponentially increasing number of output rows is required. . the technique may not work as expected. '/' ) as path. This is discussed further in the "Gotchas" section below. Variables The original syntax for this technique works fine when the number of rows is hardcoded to a value greater than or equal to 1. It always generates at least one row in these cases. clear breaks variable v_total_rows number execute :v_total_rows := 0 select level from dual connect by level <= :v_total_rows . path . Note there is some debate about whether queries without PRIOR in the CONNECT BY clause are legal or not. PATH ---------/a /b /a/a /a/b /b/a /b/b /a/a/a /a/a/b /a/b/a /a/b/b /b/a/a /b/a/b /b/b/a /b/b/b KEY --a b a b a b a b a b a b a b Without a CONNECT BY condition that uses PRIOR it appears Oracle returns all possible hierarchy permutations. sys_connect_by_path( key.

/ no rows selected execute :v_total_rows := null .LEVEL ---------1 execute :v_total_rows := -5 PL/SQL procedure successfully completed. no rows selected execute :v_total_rows := -5 PL/SQL procedure successfully completed. execute :v_total_rows := null PL/SQL procedure successfully completed. A simple WHERE clause fixes this behaviour. / LEVEL ---------1 1 row selected. / LEVEL ---------1 1 row selected. execute :v_total_rows := 0 PL/SQL procedure successfully completed. select from WHERE connect level dual :V_TOTAL_ROWS >= 1 by level <= :v_total_rows .

val from ( select level . Day of the Week Case Study In the next snippet we apply the technique to the day of the week scenario we examined in prior tutorials. / no rows selected execute :v_total_rows := 3 PL/SQL procedure successfully completed.---------0 1 100 2 3 300 4 400 5 500 6 Gotchas To Use PRIOR or Not to Use PRIOR. select day_of_week . / LEVEL ---------1 2 3 3 rows selected. t. DAY_OF_WEEK VAL ----------.1 as day_of_week from dual connect by level <= 7 ) i left outer join t using( day_of_week ) order by day_of_week .PL/SQL procedure successfully completed. That is the Question .

do not produce the desired 10 rows of output (as tested in Oracle 10g). one expression in condition must be qualified with the PRIOR operator to refer to the parent row. select level from dual connect by level <= 10 and PRIOR DBMS_RANDOM. Note that the following queries. seemingly equivalent to the CONNECT BY LEVEL <= 10 solution. ERROR: ORA-01436: CONNECT BY loop in user data select level from dual connect by level <= 10 AND PRIOR 1 = 1 . select level from dual connect by level <= 10 AND PRIOR DUMMY = DUMMY .VALUE IS NOT NULL .Laurent Schneider argues in his blog post Bible of Oracle that a clause like CONNECT BY LEVEL <= 10 is an illegal construct since it has no expressions qualified with the PRIOR operator. The fact the CONNECT BY clause works without error in Oracle 10g and some 9i versions somewhat supports this view.Oracle® Database SQL Reference 10g Release 2 (10. but the PL/SQL call it contains makes it perform worse (from Re: Creating N Copies of a Row using "CONNECT BY CONNECT_BY_ROOT" . as dictated by this statement in the SQL Reference Manual "in a hierarchical query. ERROR: ORA-01436: CONNECT BY loop in user data The following variation may be more legal than the original solution since it includes a PRIOR condition and does not produce a CONNECT BY loop.Volder).2) Others argue that this statement is a documentation bug. LEVEL ---------1 2 3 4 5 6 ." -.

like this select level from dual connect by level < 10 . Acknowledgements Mikito Harakiri. have been reported in Oracle versions earlier than 10. if you try the CONNECT BY LEVEL technique and get a single row when expecting muliple rows.Weird Results (bug?) Ask Tom "how to display selective record twice in the query?". Until there is a definitive answer.7 8 9 10 The jury is still out on using CONNECT BY LEVEL to generate integers. Tom Kyte.Weird results Ask Tom "Can there be an infinite DUAL?" . select * from (select level from dual connect by level < 10) . • • Ask Tom "how to display selective record twice in the query?" Ask Tom "Can there be an infinite DUAL?" . or variations of it. Ed In Oracle 9i. be aware there is a risk the technique may not work in future versions.2. • • • • Ask Tom "Can there be an infinite DUAL?" . may help (I have not tested this). Issues Issues with this technique. I have not tested these myself but here are some posts that describe problems. as in this snippet. LEVEL ---------1 putting the query in an inline view. LEVEL ---------1 2 3 4 5 6 7 8 9 9 rows selected. Laurent Schneider and other posters at these threads.minor simplification CONNECT BY Generator Rules | Ask Mr.

ROWNUM ---------1 2 3 4 5 6 7 8 To return 16 rows (2^4): ROWNUM ---------1 2 3 4 select rownum 5 from 6 ( 7 select 1 8 from dual 9 group by cube( 1. 3. Here are some examples that generate a series of integers using CUBE.CUBE Method This tutorial demonstrates how to generate a series of integers using the CUBE clause of the SELECT statement. 2. ROWNUM ---------1 2 3 4 To return 8 rows (2^3): select rownum from ( select 1 from dual group by cube( 1. 4 ) 10 ) . 2 ) ) . 3 ) ) . 11 12 13 14 15 16 To return 9 rows: select rownum from ( select 1 ROWNUM ---------1 2 . 2. Other techniques are discussed in the tutorials listed in the menu to the left. To return 4 rows (2^2): select rownum from ( select 1 from dual group by cube( 1.

otherwise you will not get the correct number of rows. 4 ) ) where rownum <= 9 . as in this example which attempts to generate 7 integers but only succeeds in generating 4.will only generate 2^2 rows . select rownum as integer_value from ( select 1 from t2 group by cube ( 1. t.Explained. 3 4 5 6 7 8 9 We can apply this technique to our day of the week scenario with this query.val from days_of_the_week left outer join t using ( day_of_week ) order by day_of_week . 2.1 as day_of_week from ( select 1 from dual group by cube( 1.---------0 1 100 2 3 300 4 400 5 500 6 For more details about how this method works see CUBE . with days_of_the_week as ( select rownum . 3. 3 ) ) where rownum <= 7 ) select day_of_week . 2. Gotchas Number of Arguments in CUBE Ensure the numeric literal in the WHERE clause is less than or equal to 2^(number of CUBE arguments). DAY_OF_WEEK VAL ----------. 2 ) -.from dual group by cube( 1.

. C1 C2 C3 -----.Explained To understand how the integer series generator described in the CUBE Method tutorial works we will start with a simple query. transform it into a query that uses CUBE the traditional way. or 1 in this query. ROWNUM ---------1 1 1 1 CUBE Method .-----. C2 .. (Read the Oracle manual page on the CUBE grouping operation first if you are not already familiar with this feature. not 5.-----. 3. c2. INTEGER_VALUE ------------1 2 3 4 Inline View Attempting to use rownum without the inline view will cause errors or incorrect results. . . cube( 1.---------x y 42 C1 C2 SUM_C3 -----. 2. select rownum * ERROR at line 1: ORA-00979: not a GROUP BY expression select rownum from t2 group by rownum. and then turn it into an integer series generator for the values 1 to 4.---------x y 42 . 6. select c1. 7. 2 ) . 2 ) .) set null "(null)" select * from t2 . select rownum from t2 group by cube( 1. sum( c3 ) as sum_c3 from t2 GROUP BY C1.can only be <= 4.) where rownum <= 7 -.

C1 -----(null) (null) x x C1 -----(null) (null) x x C2 SUM_C3 -----. c2. You could use arguments like 1.4. etc.3.'b' and still get the same number of rows.see Note 1 1 . 1 1 select ROWNUM AS INTEGER_VALUE from ( select 1 from t2 group by cube ( 1. 2 ) ) where ROWNUM <= 3 . 2 ) ). c2 ). The important part is how many literals you include. c2 ).6. 1 ---------1 1 1 1 1 select 1 ---------from t2 1 group by CUBE ( 1. INTEGER_VALUE ------------1 2 3 4 INTEGER_VALUE ------------1 2 3 Note 1: In this technique it does not matter what literals you use in the arguments to CUBE.----------(null) 1 y 1 (null) 1 y 1 select c1. 2 ) -.select c1.7 because it is . four literals will give you sixteen rows (2^4). select rownum as integer_value from ( select 1 from t2 group by cube ( 1. SELECT 1 from t2 group by cube ( c1. c2. sum( c3 ) as sum_c3 from t2 group by CUBE( c1.2. three literals will give you eight rows (2^3).---------(null) 42 y 42 (null) 42 y 42 C2 ANY_LITERAL -----. c2 ) . I like to use arguments like 1. 1 or 'a'. Two literals will give you four rows (2^2). 1 AS ANY_LITERAL from t2 group by cube( c1.5.

9.4. like 10 or 20.8.5.1.1.1. Type Constructor Expression Method This tutorial demonstrates how to generate a series or set of integers using Type Constructor Expressions for collection types. you can use a simple query like this one.7.1. COLUMN_VALUE -----------1 1 4 4 .1.1.10 ) ) . Other techniques are discussed in the tutorials listed in the menu to the left. desc integer_table_type integer_table_type TABLE OF NUMBER(38) The Solution If you need a manageable number of integers.1.easier to tell there are 7 arguments (which produce 2^7 rows) with this approach than with an argument list like 1. select column_value from table( integer_table_type( 1.Note 1. We will use one called INTEGER_TABLE_TYPE created in the Setup topic for this section.4. select column_value from table( integer_table_type( 1.2.3. COLUMN_VALUE -----------1 2 3 4 5 6 7 8 9 10 This method is unique from the others in this section in that it lends itself well to creating sets of non-sequential integers as well as sequential series.6. If you do not have privileges to create a type like this see Setup . Prerequisites This solution requires a nested table type or varry type.8.10 ) ) .4.4.

4 8 10

Applying the technique to our day of the week scenario yields this query.
select i.column_value as day_of_week , t.val from table( integer_table_type( 0,1,2,3,4,5,6 ) ) i left outer join t on ( i.column_value = t.day_of_week ) order by i.column_value ; DAY_OF_WEEK VAL ----------- ---------0 1 100 2 3 300 4 400 5 500 6

If you require more integers than you care to list in a type constructor expression see the Type Constructor + Cartesian Product tutorial for a variation of this technique. Gotchas If we specify more than 999 arguments in a type constructor it will generate a ORA-00939: too many arguments for function error (as tested in Oracle 10g).

Type Constructor + Cartesian Product Method
This tutorial demonstrates how to generate a series of integers using Type Constructor Expressions for collection types and Cartesian Products. Other techniques are discussed in the tutorials listed in the menu to the left. Prerequisites This solution requires a nested table type or varry type. We will use one called INTEGER_TABLE_TYPE created in the Setup topic for this section. If you do not have privileges to create a type like this see Setup - Note 1.
desc integer_table_type integer_table_type TABLE OF NUMBER(38)

The Solution

If you require a large number of integers then listing them all in a type constructor expression, like the solutions in the Type Constructor Expression Method tutorial, may be difficult or impossible. In this case you can use a Cartesian product with your type constructor expressions to generate a large number of rows with a small amount of code. Here are some examples. This query returns 9 rows (3x3).
ROWNUM ---------1 2 3 4 5 6 7 8 9

select rownum from table( integer_table_type( 1,2,3 ) ) i1, table( integer_table_type( 1,2,3 ) ) i2 ;

This query returns 12 rows (3x4).
ROWNUM ---------1 2 3 4 5 6 7 8 9 10 11 12

select rownum from table( integer_table_type( 1,2,3 ) ) i1, table( integer_table_type( 1,2,3,4 ) ) i2 ;

A query like this can return up to 10,000 rows (10^4), though we won't prove this by diplaying them all here. Listing 15 of them should suffice.
ROWNUM ---------1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

with i as ( select * from table ( integer_table_type( 1,2,3,4,5,6,7,8,9,10 ) ) ) select rownum from i,i,i,i where rownum <= 15 ;

How Cartesian Products Work When you do not specify a join between tables Oracle combines each row of one table with each row in the other to produce every possible row combination. This produces a result set with
(number of rows in Table 1) x (number of rows in Table 2)

rows in it. The following query illustrates this.
select rownum , i1.column_value i1_column_value, i2.column_value i2_column_value from table( integer_table_type( 1,2 ) ) i1, table( integer_table_type( 10,20,30 ) ) i2 ; ROWNUM I1_COLUMN_VALUE I2_COLUMN_VALUE ---------- --------------- --------------1 1 10 2 1 20 3 1 30 4 2 10 5 2 20 6 2 30

Pipelined Function Method
This tutorial demonstrates how to generate a series of integers using a Pipelined Function. Other techniques are discussed in the tutorials listed in the menu to the left. Prerequisites This solution requires a nested table type or varry type. We will use one called INTEGER_TABLE_TYPE created in the Setup topic for this section. If you do not have privileges to create a type like this see Setup - Note 1.
desc integer_table_type integer_table_type TABLE OF NUMBER(38)

You will also need the following custom database function.
create function integer_series ( p_lower_bound in number, p_upper_bound in number ) return integer_table_type pipelined as begin

for i in p_lower_bound .. p_upper_bound

column_value . end loop. select i. COLUMN_VALUE ------------5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 We can apply this technique to our day of the week scenario with this query. t.day_of_week ) order by i. return. end.7) ) . DAY_OF_WEEK VAL ----------.---------0 1 100 2 3 300 4 400 5 500 6 .column_value = t.column_value as day_of_week . here is how you use the INTEGER_SERIES function.loop pipe row(i).6) ) i left outer join t on ( i. select * from table( integer_series(-5. / The Solution Now that we have our prerequisites in place.val from table( integer_series(0.

072 2.144 262.Small Numbers The following tables show performance metrics for one run each of the eight integer series generation techniques described in the preceeding tutorials.071 2.-----------------------------Elapsed Time (1/100 sec) 3 3 3 2 5 262 3 4 session pga memory max 262. See the log file from these tests for more details.684 2.Performance Comparison . Integer ROWNUM CONNECT BY Type Type Constructor Pipelined METRIC_NAME Table MODEL + Big Table LEVEL CUBE Constructor + Cartesian Product Function ---------------------------------------.684 2.144 262. Latch Gets The following table shows total latch gets for each method.072 196.536 131.144 262.680 redo size 2.199 2. • Integer Table Method • MODEL Method • ROWNUM + a Big Table Method • CONNECT BY LEVEL Method • CUBE Method • Type Constructor Expression Method • Type Constructor + Cartesian Product Method • Pipelined Function Method Each run generated a series of integers from 1 to 100.071 2.144 327.640 2.744 2.144 262.072 131.----------------------.684 2.684 2.536 131.-----------.072 327.144 262.081 2.-----------.144 262. Statistics The following table shows database statistics where values for one method differ by more than 100 from another method.076 session uga memory 0 0 0 0 0 65.464 65.684 2.680 session pga memory 196.-----------.464 65.464 See Statistics Descriptions for a description of each metric.-----------.684 sorts (rows) 2. .608 65.608 65.071 2.071 2.

-----------.-----------------------------cache buffers chains 206 163 221 163 163 1.Integer ROWNUM CONNECT BY Type Type Constructor Pipelined METRIC_NAME Table MODEL + Big Table LEVEL CUBE Constructor + Cartesian Product Function ---------------------------------------.-----------.----------------------.-----------.-----------.208 181 249 row cache objects 136 96 129 87 84 135 114 171 library cache 92 77 86 80 77 113 89 179 shared pool 70 25 26 24 25 85 29 45 session idle bit 56 55 56 55 55 56 55 57 library cache pin 50 43 48 43 43 57 45 89 library cache lock 26 20 28 20 20 40 25 72 enqueues 23 16 17 16 16 26 16 20 enqueue hash chains 22 16 16 16 16 26 16 18 shared pool simulator 12 9 7 9 9 16 10 17 object queue header operation 8 12 12 12 12 305 12 15 redo allocation 8 8 8 8 8 18 8 8 cache buffers lru chain 7 6 6 6 6 396 6 7 SQL memory manager workarea list latch 6 10 6 6 6 73 6 6 session allocation 6 2 4 3 2 2 2 6 sort extent pool 4 4 4 4 4 4 4 4 session switching 4 4 4 4 4 4 4 4 kks stats 4 2 2 2 2 4 2 2 simulator hash latch 4 0 10 0 0 134 0 1 simulator lru latch 4 0 10 0 0 130 0 1 PL/SQL warning settings 3 3 3 3 3 3 3 3 compile environment latch 2 1 2 1 1 1 1 3 object stats modification 1 1 2 1 1 1 1 2 library cache lock allocation 1 1 1 1 1 1 1 2 .

-----------.242 632 986 .-----------.o list latch 0 1 0 0 OS process 0 3 0 0 messages 0 1 0 40 channel operations parent latch 0 1 0 18 channel handle pool latch 0 1 0 0 OS process allocation 0 1 0 0 process allocation 0 1 0 0 process group creation 0 1 0 0 checkpoint queue latch 0 0 0 269 redo writing 0 0 0 13 active checkpoint queue latch 0 0 0 13 loader state object freelist 0 0 0 12 virtual circuit buffers 0 0 0 9 virtual circuit queues 0 0 0 7 parallel query alloc buffer 0 0 0 4 user lock 0 0 0 4 session timer 0 0 0 3 library cache load lock 0 0 0 2 virtual circuits 0 0 0 2 active service list 0 0 0 2 library cache pin allocation 0 0 0 1 resmgr:actses active list 0 0 0 1 XDB unused session pool 0 0 0 1 KMG MMAN ready and startup request latch 0 0 0 1 resmgr:free threads list 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 1 0 0 0 0 0 -----------.-----------------------------sum 757 575 709 575 559 3.dml lock allocation 1 1 1 1 FOB s.----------------------.-----------.

784.680 session uga memory max 261.144 327. See the log file from these tests for more details. Integer ROWNUM CONNECT BY Type Constructor Pipelined METRIC_NAME Table MODEL Table LEVEL + Cartesian Product Function ---------------------------------------.144 327.-----------Elapsed Time (1/100 sec) 59 349 68 67 57 544 session pga memory max 262.144 2.000.128 262.888 6 39 72 + Big .252 261.984 261.Techniques that use a small number of latches scale better than techniques that use a large number of latches.536 131.927 45 78 111 consistent gets 6. Performance Comparison .144 4.840 6 6.964 4. Results will even differ from one set of test runs to the next on the same machine.072 0 262. Warning: Results on your own systems with your own data will differ from these results.-----------.----------------------.879 45 6.-----------.964 2.616 262.016. • Integer Table Method • MODEL Method • ROWNUM + a Big Table Method • CONNECT BY LEVEL Method • Type Constructor + Cartesian Product Method • Pipelined Function Method Each run generated a series of integers from 1 to 100.964 261. Run your own tests and average the results from multiple runs before making performance decisions. Note that the Type Constructor Expression Method technique was excluded from this comparison because it can only be used to generate up to 999 different values.533. The CUBE Method technique was excluded from this test because it failed to complete in under 10 minutes.964 session pga memory 196.031.608 65.680 session logical reads 6. Statistics The following table shows database statistics where values for one method differ by more than 100 from another method.------------------.Large Numbers The following tables show performance metrics for one run each of six integer series generation techniques described in the preceeding tutorials.

375 13.464 37 34 34 34 65.-----------cache buffers chains 13.888 6 39 no work .464 6.376 13.927 163 196 258 session idle bit 13.consistent gets from cache 6.946 836 13.375 13.consistent read gets 6. Latch Gets The following table shows total latch gets for each method.172 object queue header operation 79 365 12 12 15 18 checkpoint queue latch 55 237 0 0 11 43 library cache pin 50 49 48 43 54 13.822 5 6.----------------------.377 simulator lru latch 424 172 461 0 0 1 simulator hash latch 424 172 461 0 0 1 row cache objects 139 96 129 87 132 171 cache buffers lru chain 106 522 6 6 6 7 library cache 92 83 85 79 119 20.376 13.840 72 6.-----------.-----------.417 shared pool 47 20 18 17 45 49 library cache lock 30 24 30 22 34 79 enqueues 21 96 17 16 16 100 + Big .658 29 29 29 0 6 0 0 311 309 309 0 See Statistics Descriptions for a description of each metric.375 13. Integer ROWNUM CONNECT BY Type Constructor Pipelined METRIC_NAME Table MODEL Table LEVEL + Cartesian Product Function ---------------------------------------.860 0 0 buffer is not pinned count 0 0 22 DB time 27 37 22 CPU used when call started 27 35 22 CPU used by this session 27 35 21 session uga memory 0 0 65.------------------.

enqueue hash chains 16 16 16 messages 4 0 8 shared pool simulator 6 9 16 redo allocation 8 8 8 SQL memory manager workarea list latch 6 6 6 channel operations parent latch 0 0 6 session allocation 4 2 2 session switching 4 4 4 sort extent pool 4 4 4 kks stats 2 2 2 PL/SQL warning settings 3 3 3 redo writing 0 0 1 active checkpoint queue latch 0 0 1 compile environment latch 2 1 1 object stats modification 1 2 1 library cache lock allocation 1 1 1 dml lock allocation 1 1 1 session timer 0 0 1 KMG MMAN ready and startup request latch 0 0 1 object queue header heap 0 0 0 JS queue state obj latch 0 0 0 active service list 0 0 0 qmn task queue latch 0 0 0 In memory undo latch 0 0 0 OS process allocation 1 0 0 resmgr:actses active list 0 0 0 resmgr:schema config 0 0 0 kwqbsn:qsga 0 0 0 99 48 17 12 140 29 6 4 4 2 3 8 1 3 1 2 1 2 1 0 36 10 0 2 2 1 1 0 20 10 10 9 6 6 6 4 4 4 3 2 2 2 1 1 1 1 1 1 0 0 0 0 0 0 0 0 97 28 9 12 144 22 2 4 4 2 3 7 3 1 1 1 1 1 1 0 36 10 4 2 1 1 1 1 .

136 Techniques that use a small number of latches scale better than techniques that use a large number of latches.Shared B-Tree 0 0 library cache load lock 0 0 library cache pin allocation 0 0 mostly latch-free SCN 0 0 undo global data 0 0 lgwr LWN SCN 0 0 Consistent RBA 0 0 FOB s.089 48.o list latch 0 0 0 2 1 0 0 0 0 0 0 2 1 1 1 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 -----------.879 14. .633 13.-----------sum 28. Results will even differ from one set of test runs to the next on the same machine.883 16.-----------.------------------.----------------------.447 28. Run your own tests and average the results from multiple runs before making performance decisions. Warning: Results on your own systems with your own data will differ from these results.

2" but the row where KEY='e' needs the series "1.2. . Join Method Many of the single series solutions presented earlier can be easily adapted to generate multiple series with a simple join.------------a (null) (null) b c d d e e e 0 (null) 1 2 2 3 3 3 1 1 2 1 2 3 where a row like the one where KEY='d' needs the integer series "1. For example. The subtopics in this section demonstrate various ways to accomplish this. given a data table like this KEY QTY --. ). create table integers ( integer_value integer primary key ) organization index .3". Here is an example of how this is done using the Integer Table Method technique. ). insert into insert into insert into insert into insert into commit.---------a (null) b 0 c 1 d 2 e 3 we sometimes need queries that generate results like these KEY QTY INTEGER_VALUE --. integers integers integers integers integers values values values values values ( ( ( ( ( 1 2 3 4 5 ). Sometimes many different integer series are required in the same query.Multiple Integer Series The preceding topics showed how to generate a single series of integers. ). ).---------.

3. . qty. select key.------------a (null) (null) b c d d e e e 0 (null) 1 2 2 3 3 3 1 1 2 1 2 3 With the Type Constructor Expression Method technique it would look like this. integer_value from t3 left outer join integers on ( integers.4.integer_value <= t3.---------.column_value <= t3.set null "(null)" break on key duplicates skip 1 select key. KEY QTY INTEGER_VALUE --.qty ) order by key.qty ) order by key. KEY QTY INTEGER_VALUE --. integer_value .5) ) integers on ( integers. integer_value .------------a (null) (null) b c d d e e e 0 (null) 1 2 2 3 3 3 1 1 2 1 2 3 In both queries note that we first generate more integers than required and then filter out the excess values via a join condition. qty.2. column_value as integer_value from t3 left outer join table( integer_table_type(1.---------.

---------. integer_value . Multiple integer series can be created using a query like this one (the PATH column is included to illustrate how the query works) set null "(null)" break on key duplicates skip 1 column path format a10 . key2. set null "(null)" break on key duplicates skip 1 select key. KEY KEY2 QTY INTEGER_VALUE --. cast( null as integer ) as integer_value ) rules ( integer_value[ FOR KEY2 FROM 1 TO QTY[1] INCREMENT 1 ] = cv(key2) ) order by key.MODEL Method With MODEL queries there is no need to use the join technique described in Join Method.---------. integer_value from t3 model PARTITION BY ( KEY ) dimension by ( 1 as key2 ) measures ( qty.------------a 1 (null) (null) b c d d e e e 1 1 1 2 (null) 1 2 (null) 3 (null) 0 (null) 1 2 3 1 1 2 1 2 3 CONNECT BY LEVEL Method With the CONNECT BY LEVEL approach there is also no need to use the Join Method. qty. We can generate multiple series by applying a FOR loop to each row in the base table with the aid of a PARTITION BY clause.

qty order by key.---------c 1 1 /c d d e e e 2 2 3 3 3 1 /d 2 /d/d 1 /e 2 /e/e 3 /e/e/e or this approach (only possible on version 10g or greater) select key. level as integer_value.------------.qty order by key.---------. sys_connect_by_path( key.---------. 'b' ). sys_connect_by_path( key. KEY QTY INTEGER_VALUE PATH --. qty. KEY QTY INTEGER_VALUE PATH --. like the rows with KEY in ( 'a'. integer_value .select key. '/' ) as path from t3 where qty >= 1 connect by KEY = PRIOR KEY and prior dbms_random. '/' ) as path from t3 where qty >= 1 connect by KEY = CONNECT_BY_ROOT KEY and level <= t3. integer_value . qty.------------.value is not null and level <= t3. level as integer_value.---------c 1 1 /c d d e e e 2 2 3 3 3 1 /d 2 /d/d 1 /e 2 /e/e 3 /e/e/e Note these approaches will not work for rows where no integer series is required. Gotchas .

trc file: ORA-07445: exception encountered: core dump [ACCESS_VIOLATION] [__VInfreq__msqopnws+2740] [PC:0x30E2580] [ADDR:0x2C] [UNABLE_TO_READ] [] . integer_value . On my system the following variation of the CONNECT_BY_ROOT query raised some rather severe ORA errors casting further doubt on the technique's reliability (do not run this query on your own systems). ERROR at line 1: ORA-03113: end-of-file on communication channel ERROR: ORA-03114: not connected to ORACLE From the . "In a hierarchical query.qty order by key. "You cannot specify (CONNECT_BY_ROOT) in the START WITH condition or the CONNECT BY condition.The CONNECT_BY_ROOT technique may not work without error in all cases and it may not work in Oracle versions beyond 10g. This is because it violates two restrictions documented at Hierarchical Query Operators: 1." 2. level as integer_value from t3 start with qty >= 1 connect by KEY = CONNECT_BY_ROOT KEY and level <= t3. one expression in the CONNECT BY condition must be qualified by the PRIOR operator. select key. qty." The fact that the query above contradicts the documentation yet works without error in 10g suggests a bug in either the documentation or the SQL engine.

Too many context switches may degrade performance dramatically. SQL> insert into t_all_objects select * from t_all_objects. 4 begin 5 open c1. Begin Open c1. Bulk binding lets us to transfer rows between the sql engine and the plsql engine as collections. Table created. --process rows. Bulk collect is the bulk binding syntax for select statements. open it. 2 3 rec1 c1%rowtype. COUNT(*) ---------213248 SQL> declare cursor c1 is select object_name from t_all_objects. Exit when c1%notfound. fetch from it row by row in a loop and process the row they fetch. They declare a cursor. In order to reduce the number of these context switches we can use a feature named bulk binding. 6 loop . Declare Cursor c1 is select column_list from table_name>. insert. Here is a simple test case to compare the performance of fetching row by row and using bulk collect to fetch all rows into a collection. Loop. Bulk binding is available for select. Fetch c1 into r1. 3332 rows created.. Rec1 c1%rowtype.Bulk Collect Executing sql statements in plsql programs causes a context switch between the plsql engine and the sql engine.. SQL> r 1* insert into t_all_objects select * from t_all_objects 6664 rows created. End loop. End. ---replicated a couple of times SQL> select count(*) from t_all_objects. delete and update statements. One of the things i usuallly come accross is that developers usually tend to use cursor for loops to process data. SQL> create table t_all_objects as select * from all_objects.

null. Elapsed: 00:00:04. Otherwise process memory gets bigger and bigger as you fetch the rows. 4 rec1 c1_type. end. 9 10 11 end. / PL/SQL procedure successfully completed. 3 type c1_type is table of c1%rowtype. 7 loop 8 fetch c1 bulk collect into rec1 limit 200. 3 type c1_type is table of c1%rowtype.07 . 12 / PL/SQL procedure successfully completed. 12 exit when c1%notfound..rec1. The above method (which fetched all the rows) may not be applicable to all cases. 17 / PL/SQL procedure successfully completed. exit when c1%notfound.7 8 9 10 11 12 13 14 fetch c1 into rec1. 7 8 fetch c1 bulk collect into rec1. SQL> declare 2 cursor c1 is select object_name from t_all_objects. 5 begin 6 open c1. 5 begin 6 open c1.count loop 10 null. 11 end loop. Elapsed: 00:00:05. 4 rec1 c1_type. end loop.32 As can be clearly seen. 9 for i in 1. 14 15 16 end. 13 end loop.75 SQL> declare 2 cursor c1 is select object_name from t_all_objects. When there are many rows to process. bulk collecting the rows shows a huge performance improvement over fetching row by row. process those rows and fetch again. we can limit the number of rows to bulk collect. Elapsed: 00:00:44.

BEGIN FOR r IN c_orders LOOP v_amount_usd := currency_convert (r.---------. Running both versions of the code through SQL trace and TKPROF will yield reports from which you may derive a wealth of information. The discussion of the TKPROF reports will help you see how to interpret TKPROF output in order to assess the impact of bulk binds on your application.-------0. amount_local /* no bulk bind */ FROM open_orders.40 0 0 0 0.amount_local.-------. you can test your code both with and without bulk binds.45 0 0 1 rows -------0 1 0 -------1 .-------. If you have the luxury of time.00 0 0 0 -------. The same goes for a program that issues five or six UPDATE statements.Deciding When to Use Bulk Binds PL/SQL code that uses bulk binds will be slightly more complicated and somewhat more prone to programmer bugs than code without bulk binds.---------.04 0 0 1 10. COMMIT. so you need to ask yourself if the improved runtime performance will justify the expense. call count ------. v_amount_usd NUMBER.-----total 2 cpu elapsed disk query current -------. END. A Simple Program With and Without Bulk Binds In this section we will look at a simple program written both with and without bulk binds.05 0.000 rows from a cursor or performs that many similar UPDATE statements will most likely benefit from bulk binds. currency_code. We'll look at TKPROF reports that demonstrate the impact bulk binds can have. A PL/SQL program that reads a dozen rows from a cursor will probably see no noticeable benefit from bulk binds. a program that reads 1.order_id.-------10. END LOOP. Consider the following excerpts from a TKPROF report: ************************************************************************ DECLARE CURSOR c_orders IS SELECT order_id. the cost of adding a few lines of code is so slight that I would lean toward using bulk binds when in doubt.00 0.55 11.-----Parse 1 Execute 1 Fetch 0 ------. UPDATE open_orders /* no bulk bind */ SET amount_usd = v_amount_usd WHERE order_id = r.currency_code). However. r.-------. No universal rule exists to dictate when bulk binds are worthwhile and when they are not.60 11. However.-------.

---------. v_currency_codes t_char_array.32 1 60576 31022 0. EXIT WHEN v_row_count = c_orders%ROWCOUNT.10 0 30393 0 -------. using 7.-----Parse 1 Execute 30286 Fetch 0 ------.00 0 0 0 1. currency_code. LOOP FETCH c_orders BULK COLLECT INTO v_order_ids. There were 30.-------0.-------.---------.19 CPU seconds.00 0. please recognize it is for illustrative purposes only.00 0 0 0 -------.-------.00 0.-------0.00 0 0 0 0. this is a very simple program that does not use bulk binds. amount_local /* bulk bind */ FROM open_orders. amount_local /* no bulk bind */ FROM open_orders call count ------. BEGIN OPEN c_orders.---------.-------.-------.-------1. requiring 30.33 1 60576 31022 rows -------0 30286 0 -------30286 As you can see.-------. currency_code.-------.-------. v_amounts_local LIMIT 100. v_amounts_local t_num_array.-----total 30287 cpu elapsed disk query current -------. Now consider the following excerpts from another TKPROF report: ************************************************************************ DECLARE CURSOR c_orders IS SELECT order_id. . (The code borders on being silly.-------.08 CPU seconds.08 1. v_amounts_usd t_num_array.-----total 30289 cpu elapsed disk query current -------. v_order_ids t_num_array.286 times.00 0. TYPE t_num_array IS TABLE OF NUMBER INDEX BY BINARY_INTEGER.************************************************************************ SELECT order_id.00 0 0 0 7.19 7.19 7.) The PL/SQL engine used 10.08 1.10 0 30393 0 rows -------0 0 30286 -------30286 ************************************************************************ UPDATE open_orders /* no bulk bind */ SET amount_usd = :b2 WHERE order_id = :b1 call count ------.-------7. TYPE t_char_array IS TABLE OF VARCHAR2(10) INDEX BY BINARY_INTEGER.287 fetch calls against the cursor.---------. v_row_count NUMBER := 0.00 0.393 logical reads and 1.-----Parse 1 Execute 1 Fetch 30287 ------. v_currency_codes.55 CPU seconds to run this code (this figure does not include CPU time used by the SQL engine). The UPDATE statement was executed 30.

CLOSE c_orders. bringing logical reads down from 30.-------.59 0 4815 0 rows -------0 0 30286 -------30286 ************************************************************************ UPDATE open_orders /* bulk bind */ SET amount_usd = :b1 WHERE order_id = :b2 call count ------.---------.. but works with data 100 rows at a time instead of one row at a time.-------.66 0 0 0 rows -------0 1 0 -------1 ************************************************************************ SELECT order_id.00 0 0 0 -------. call count ------.-----Parse 1 Execute 303 Fetch 0 ------.-------0. The UPDATE statement was executed only 303 times instead of 30.60 seconds.count UPDATE open_orders /* bulk bind */ SET amount_usd = v_amounts_usd(i) WHERE order_id = v_order_ids(i).---------.38 0 30895 31021 0.00 0 0 0 0.-----total 305 cpu elapsed disk query current -------. END LOOP.-------0.00 0.38 0 30895 31021 rows -------0 30286 0 -------30286 This code uses bulk binds to do the same thing as the first code sample.-------0. reducing CPU time from 7.. We can see that CPU time used by the PL/SQL engine has reduced from 10.286. END. FORALL i IN 1.00 0 0 0 -------.63 0.-------0.-------. COMMIT.75 8.393 to 4.-----Parse 1 Execute 1 Fetch 0 ------. currency_code.count LOOP v_amounts_usd(i) := currency_convert (v_amounts_local(i).-------. amount_local /* bulk bind */ FROM open_orders call count ------.48 0.00 0 0 0 3.00 0.75 seconds.287.815 and CPU time down from 1.03 0 0 0 0.55 seconds to 0. FOR i IN 1.-------.-------. v_currency_codes(i)).---------.v_row_count := c_orders%ROWCOUNT.-----total 2 cpu elapsed disk query current -------.00 0.-----Parse 1 Execute 1 Fetch 303 ------.03 0.-------. .00 0.-----total 304 cpu elapsed disk query current -------.-------3.-------.08 seconds to 0.48 0.v_order_ids.75 8.60 0.00 0 0 0 0. There were only 303 fetch calls against the cursor instead of 30.-------.19 seconds to 3.48 seconds.---------.-------.-------.v_order_ids.59 0 4815 0 -------. END LOOP.00 0.62 0 0 0 0.---------.-------0.---------.-------.

The SQL trace facility imparts an overhead that is proportional to the number of parse.In this example it would appear that bulk binds were definitely worthwhile – CPU time was reduced by about 75%. Just remember that SQL trace can inflate the perceived benefit. This is still a significant savings. . The Oracle Call Interface has supported array processing for 15 years or more. While these TKPROF reports suggest that in this example bulk binds shaved about 50% off of the elapsed time. and the increased efficiency it brings is well understood. enabling your PL/SQL programs to use less CPU time and run faster. Although bulk binds are indeed beneficial here. Thus using bulk binds in your PL/SQL programs can certainly be worth the effort. Since bulk binds reduce the number of SQL calls. the benefit is not truly as rosy as it appears in these TKPROF reports. SQL trace adds much less overhead to code that uses bulk binds. and can offer significant performance improvements for certain types of programs. the savings were about 25% when SQL trace was not enabled. elapsed time by 50%. execute. and logical reads by 50%. It is nice to see this benefit available to PL/SQL programmers as well. Conclusion Bulk binds allow PL/SQL programs to interact more efficiently with the SQL engine built into Oracle. and fetch calls to the SQL engine. PL/SQL bulk binds are not hard to implement.

Sign up to vote on this title
UsefulNot useful