Keep clause
You may have seen an aggregate function like this in SQL queries:
max(value) keep (dense_rank first order by mydate)
or this analytic variant:
max(value) keep (dense_rank last order by mydate) over (partition by relation_nr)
Unfortunately, when you start
searching for the "keep" clause,
you won't find anything in the Oracle documentation (and hopefully
because of this blogpost, people will now have a reference). Of course
Oracle documents such functions. You only have to know that they are
called
FIRST and
LAST in the SQL Language Reference.
Even though these functions were already introduced in version 9, I've
seen lots of code that could have used these functions, but didn't. And
that's a pity because it's a wasted opportunity to write shorter and
faster code. The common use case I'm talking about is when you have a
detail table with a validity period. Typically with a column
startdate, and optionally an
enddate. For such a table, you often have to know the values of the currently valid row.
An example: suppose we have a table RELATIONS and for each relation we want to know his address at a certain point in time:
01 | SQL> create table relations |
02 | 2 ( id number not null primary key |
03 | 3 , name varchar2(30) not null |
09 | SQL> insert into relations |
10 | 2 select 1, 'Oracle Nederland' from dual union all |
11 | 3 select 2, 'Ciber Nederland' from dual |
16 | SQL> create table relation_addresses |
17 | 2 ( relation_id number not null |
18 | 3 , startdate date not null |
19 | 4 , address varchar2(30) not null |
20 | 5 , postal_code varchar2(6) not null |
21 | 6 , city varchar2(30) not null |
22 | 7 , constraint ra_pk primary key (relation_id,startdate) |
23 | 8 , constraint ra_r_fk foreign key (relation_id) references relations(id) |
29 | SQL> insert into relation_addresses |
30 | 2 select 1, date '1995-01-01' , 'Rijnzathe 6' , '3454PV' , 'De Meern' from dual union all |
31 | 3 select 1, date '2011-01-01' , 'Hertogswetering 163-167' , '3543AS' , 'Utrecht' from dual union all |
32 | 4 select 2, date '2000-01-01' , 'Frankrijkstraat 128' , '5622AH' , 'Eindhoven' from dual union all |
33 | 5 select 2, date '2006-01-01' , 'Meerkollaan 15' , '5613BS' , 'Eindhoven' from dual union all |
34 | 6 select 2, date '2010-01-01' , 'Burgemeester Burgerslaan 40b' , '5245NH' , 'Den Bosch' from dual union all |
35 | 7 select 2, date '2015-01-01' , 'Archimedesbaan 16' , '3439ME' , 'Nieuwegein' from dual |
41 | 2 dbms_stats.gather_table_stats( user , 'relations' ); |
42 | 3 dbms_stats.gather_table_stats( user , 'relation_addresses' ); |
46 | PL/SQL procedure successfully completed. |
Relation "Oracle Nederland" has two addresses, and its current address
being at the Hertogswetering. And fictively, relation "Ciber Nederland"
has four addresses. The current address is the Den Bosch one. And I've
also recorded a future address in Nieuwegein. Note that, in real life,
the latter three are all Ciber offices currently in use.
To get the active relation addresses on October 1st, 2012, I can use
this query:
01 | SQL> var REFERENCE_DATE varchar2(10) |
02 | SQL> exec :REFERENCE_DATE:= '2012-10-01' |
04 | PL/SQL procedure successfully completed. |
06 | SQL> select ra.relation_id |
07 | 2 , max (ra.startdate) startdate |
08 | 3 from relation_addresses ra |
09 | 4 where ra.startdate <= to_date(:REFERENCE_DATE, 'yyyy-mm-dd' ) |
10 | 5 group by ra.relation_id |
But what if I want to retrieve the current address belonging to these
rows? In fact, this is frequently being asked in Oracle forums. Prior to
Oracle8, you would have used a query like below:
01 | SQL> select ra.relation_id |
06 | 6 from relation_addresses ra |
07 | 7 where ra.startdate <= to_date(:REFERENCE_DATE, 'yyyy-mm-dd' ) |
09 | 9 ( select 'a relation_address with a more recent startdate' |
10 | 10 from relation_addresses ra2 |
11 | 11 where ra2.relation_id = ra.relation_id |
12 | 12 and ra2.startdate <= to_date(:REFERENCE_DATE, 'yyyy-mm-dd' ) |
13 | 13 and ra2.startdate > ra.startdate |
17 | RELATION_ID STARTDATE ADDRESS POSTAL CITY |
19 | 1 01-01-2011 00:00:00 Hertogswetering 163-167 3543AS Utrecht |
20 | 2 01-01-2010 00:00:00 Burgemeester Burgerslaan 40b 5245NH Den Bosch |
This uses a correlated subquery accessing the table (or index belonging
to) table RELATION_ADDRESSES twice. Which can be prevented from Oracle8
onwards by using an analytic function:
01 | SQL> select relation_id |
06 | 6 from ( select ra.relation_id |
11 | 11 , row_number() over (partition by ra.relation_id order by ra.startdate desc ) rn |
12 | 12 from relation_addresses ra |
13 | 13 where ra.startdate <= to_date(:REFERENCE_DATE, 'yyyy-mm-dd' ) |
18 | RELATION_ID STARTDATE ADDRESS POSTAL CITY |
20 | 1 01-01-2011 00:00:00 Hertogswetering 163-167 3543AS Utrecht |
21 | 2 01-01-2010 00:00:00 Burgemeester Burgerslaan 40b 5245NH Den Bosch |
Here you compute the row_number when you partition the result set per
relation_id ordered by startdate in descending order. Meaning the most
recent date starting before the reference date, gets row_number 1
assigned per relation_id. By using an inline view, we can filter on the
outcome of the analytic function, and only select the rows with
row_number 1. In forums, you'll see this solution often being adviced.
Compared to the correlated subquery, this query selects only once from
table RELATION_ADDRESSES. However, you can do even better by just adding
three "keep clause" functions to the original query:
01 | SQL> select ra.relation_id |
02 | 2 , max (ra.startdate) startdate |
03 | 3 , max (ra.address) keep (dense_rank last order by ra.startdate) address |
04 | 4 , max (ra.postal_code) keep (dense_rank last order by ra.startdate) postal_code |
05 | 5 , max (ra.city) keep (dense_rank last order by ra.startdate) city |
06 | 6 from relation_addresses ra |
07 | 7 where ra.startdate <= to_date(:REFERENCE_DATE, 'yyyy-mm-dd' ) |
08 | 8 group by ra.relation_id |
11 | RELATION_ID STARTDATE ADDRESS POSTAL CITY |
13 | 1 01-01-2011 00:00:00 Hertogswetering 163-167 3543AS Utrecht |
14 | 2 01-01-2010 00:00:00 Burgemeester Burgerslaan 40b 5245NH Den Bosch |
The three extra aggregate functions all do a "dense_rank last order by
startdate", meaning "sort the rows by startdate, and pick only those
rows which have the most recent startdate". If you have more rows with
the same startdate, the max function at the start tells Oracle to pick
the value with the maximum address/postal_code/city. However,
(relation_id,startdate) is unique, so ties are impossible and thus the
max function is a dummy. I also could have used min.
The query is shorter and -to me- clearer at first glance. However, the
main reason for my enthusiasm for the aggregate functions FIRST and LAST
is because it's just faster. To show this, let's execute those queries
against a table with 300,000 rows, 100,000 relations with 3 addresses
each:
01 | SQL> create table relations |
02 | 2 ( id number not null primary key |
03 | 3 , name varchar2(30) not null |
09 | SQL> create table relation_addresses |
10 | 2 ( relation_id number not null |
11 | 3 , startdate date not null |
12 | 4 , address varchar2(30) not null |
13 | 5 , postal_code varchar2(6) not null |
14 | 6 , city varchar2(30) not null |
15 | 7 , constraint ra_pk primary key (relation_id,startdate) |
16 | 8 , constraint ra_r_fk foreign key (relation_id) references relations(id) |
22 | SQL> insert into relations |
24 | 3 , dbms_random.string( 'a' ,30) |
26 | 5 connect by level <= 100000 |
31 | SQL> insert into relation_addresses |
32 | 2 select 1 + mod( level -1,100000) |
33 | 3 , date '2013-01-01' - numtodsinterval( level , 'hour' ) |
34 | 4 , dbms_random.string( 'a' ,30) |
35 | 5 , dbms_random.string( 'a' ,6) |
36 | 6 , dbms_random.string( 'a' ,30) |
38 | 8 connect by level <= 300000 |
44 | 2 dbms_stats.gather_table_stats |
48 | 6 , method_opt=> 'FOR ALL INDEXED COLUMNS SIZE 254' |
49 | 7 , estimate_percent=>100 |
51 | 9 dbms_stats.gather_table_stats |
53 | 11 , 'relation_addresses' |
55 | 13 , method_opt=> 'FOR ALL INDEXED COLUMNS SIZE 254' |
56 | 14 , estimate_percent=>100 |
61 | PL/SQL procedure successfully completed. |
Note that I created histograms with 254 buckets just to make the
optimizer see that it should full scan the table, despite the "startdate
<= :REFERENCE_DATE" predicate.
This next query should give a clue what's in the table:
02 | 2 from relation_addresses |
03 | 3 where relation_id in (1,2,99999,100000) |
04 | 4 order by relation_id |
08 | RELATION_ID STARTDATE ADDRESS POSTAL CITY |
10 | 1 09-03-1990 15:00:00 tKgXePxuAIdhFBNJLIRRjodrlJzGOl vPIAbL pNkbFHTJPrVuDIYLxsCfUfetBsKJIE |
11 | 1 05-08-2001 07:00:00 LybVzfpzoQzXjpCAdkSZrkYrwUtZtL cWJwFe IczTRyjITWCJIOErccfITVvsqRVyMF |
12 | 1 31-12-2012 23:00:00 lNEwsdYhbwdqRxHTSCTCykgICxiXKL oXzHQF YfyKFmiboCWfmNLjVLZoKmUDoMFaDu |
13 | 2 09-03-1990 14:00:00 svOylQPkbyfympSXRMeyudfFErFvlO MLFdpG LTtAKdrpUmCwFgqEmoKxnUtWecwgcV |
14 | 2 05-08-2001 06:00:00 BsRCUviBiLHaAEjyRVnIedRAWzuVSe DlBlZW ErQmCkDgNDTMOdZzceFYrMXnZmmjxg |
15 | 2 31-12-2012 22:00:00 wqdFdXoBdmmCooLtGfWOMKukIMrDlI geRRHz DaPpWHOOdWgbjLaRkxfFDUIPgVgvEt |
16 | 99999 12-10-1978 01:00:00 FsXOjUdNIgjjGjnWpJjTTscbcuqsxa PdhVtm qOskmLwRlngSEihmlpYhmNHhvtrpBc |
17 | 99999 09-03-1990 17:00:00 sqoKYNeDntZtAUSmSDMtIQZloTSVeD uGPszi GIDctptEomcGzYGYhUGhKHgDRZJCmY |
18 | 99999 05-08-2001 09:00:00 fhHGwuGPIHSOaKdjDvDcqTzsbHZzqR tpaLAP rVYCmijzqJmhlnZZLXkHpgFmLAEiTS |
19 | 100000 12-10-1978 00:00:00 WwxfHcVfkFfItgcXfjPnKTiATlHjao nSOjSn vZNRsRySNPlmQKgCJjcpiEOhQIxzoy |
20 | 100000 09-03-1990 16:00:00 cGcVPMsFyxCBrnsZtMYBnaAflXiNff NVKRIr SseFWkWyUDgaPpbxdmENdLjurGbJPK |
21 | 100000 05-08-2001 08:00:00 dRfCmqdmbhcmaMvyYBpewPsFBCVdlG BMQWLY YPaAGnKKUkfdnAeAyLYeUBfXwezsEo |
So there are a couple of rows that are filtered because they're in the
future, but for most rows, the latest row is the current one.
This is the plan of the first query with the correlated subquery:
01 | SQL> select * from table (dbms_xplan.display_cursor( null , null , 'iostats last' )) |
06 | SQL_ID d6p5uh67h65yb, child number 0 |
08 | select ra.relation_id , ra.startdate , ra.address , |
09 | ra.postal_code , ra.city from relation_addresses ra where |
10 | ra.startdate <= to_date(:REFERENCE_DATE, 'yyyy-mm-dd' ) and not exists |
11 | ( select 'a relation_address with a more recent startdate' |
12 | from relation_addresses ra2 where ra2.relation_id = |
13 | ra.relation_id and ra2.startdate <= |
14 | to_date(:REFERENCE_DATE, 'yyyy-mm-dd' ) and ra2.startdate > |
17 | Plan hash value: 3749094337 |
20 | | Id | Operation | Name | Starts | E- Rows | A- Rows | A- Time | Buffers | Reads | |
22 | | 0 | SELECT STATEMENT | | 1 | | 100K|00:00:00.66 | 15071 | 3681 | |
23 | |* 1 | HASH JOIN RIGHT ANTI | | 1 | 2978 | 100K|00:00:00.66 | 15071 | 3681 | |
24 | |* 2 | INDEX FAST FULL SCAN| RA_PK | 1 | 297K| 297K|00:00:00.05 | 1240 | 35 | |
25 | |* 3 | TABLE ACCESS FULL | RELATION_ADDRESSES | 1 | 297K| 297K|00:00:00.12 | 13831 | 3646 | |
28 | Predicate Information (identified by operation id): |
31 | 1 - access( "RA2" . "RELATION_ID" = "RA" . "RELATION_ID" ) |
32 | filter( "RA2" . "STARTDATE" > "RA" . "STARTDATE" ) |
33 | 2 - filter( "RA2" . "STARTDATE" <=TO_DATE(:REFERENCE_DATE, 'yyyy-mm-dd' )) |
34 | 3 - filter( "RA" . "STARTDATE" <=TO_DATE(:REFERENCE_DATE, 'yyyy-mm-dd' )) |
A HASH JOIN ANTI for the not exists, and a total of .66 seconds.
Next, the plan for the query with the analytic row_number function:
01 | SQL> select * from table (dbms_xplan.display_cursor( null , null , 'iostats last' )) |
06 | SQL_ID 1zd4wqtxkc2vz, child number 0 |
08 | select relation_id , startdate , address , postal_code |
09 | , city from ( select ra.relation_id , ra.startdate |
10 | , ra.address , ra.postal_code , |
11 | ra.city , row_number() over (partition by ra.relation_id |
12 | order by ra.startdate desc ) rn from relation_addresses ra |
13 | where ra.startdate <= to_date(:REFERENCE_DATE, 'yyyy-mm-dd' ) |
16 | Plan hash value: 2795878473 |
19 | | Id | Operation | Name | Starts | E- Rows | A- Rows | A- Time | Buffers | Reads | |
21 | | 0 | SELECT STATEMENT | | 1 | | 100K|00:00:00.97 | 7238 | 3646 | |
22 | |* 1 | VIEW | | 1 | 297K| 100K|00:00:00.97 | 7238 | 3646 | |
23 | |* 2 | WINDOW SORT PUSHED RANK| | 1 | 297K| 200K|00:00:00.93 | 7238 | 3646 | |
24 | |* 3 | TABLE ACCESS FULL | RELATION_ADDRESSES | 1 | 297K| 297K|00:00:00.09 | 7238 | 3646 | |
27 | Predicate Information (identified by operation id): |
31 | 2 - filter(ROW_NUMBER() OVER ( PARTITION BY "RA" . "RELATION_ID" ORDER BY |
32 | INTERNAL_FUNCTION( "RA" . "STARTDATE" ) DESC )<=1) |
33 | 3 - filter( "RA" . "STARTDATE" <=TO_DATE(:REFERENCE_DATE, 'yyyy-mm-dd' )) |
Note that this query takes longer than the correlated subquery above:
.97 seconds versus .66 seconds. The HASH JOIN ANTI took .49 seconds (.66
- .05 -.12) where computing the ROW_NUMBER took .84 seconds (.93 -
.09). So here, on my laptop, I have avoided .05 seconds for the INDEX
FAST FULL SCAN, but spend .35 (.84 - .49) seconds more for the
computation. Likely, when I/O is more expensive than on my laptop, the
time of the first query will go up and the times will be closer to each
other.
Now the keep clause variant:
01 | SQL> select * from table (dbms_xplan.display_cursor( null , null , 'iostats last' )) |
06 | SQL_ID dcw8tyyqtu2kk, child number 0 |
08 | select ra.relation_id , max (ra.startdate) startdate , |
09 | max (ra.address) keep (dense_rank last order by ra.startdate) address |
10 | , max (ra.postal_code) keep (dense_rank last order by ra.startdate) |
11 | postal_code , max (ra.city) keep (dense_rank last order by |
12 | ra.startdate) city from relation_addresses ra where ra.startdate <= |
13 | to_date(:REFERENCE_DATE, 'yyyy-mm-dd' ) group by ra.relation_id |
15 | Plan hash value: 2324030966 |
18 | | Id | Operation | Name | Starts | E- Rows | A- Rows | A- Time | Buffers | Reads | |
20 | | 0 | SELECT STATEMENT | | 1 | | 100K|00:00:00.55 | 7238 | 3646 | |
21 | | 1 | SORT GROUP BY | | 1 | 100K| 100K|00:00:00.55 | 7238 | 3646 | |
22 | |* 2 | TABLE ACCESS FULL | RELATION_ADDRESSES | 1 | 297K| 297K|00:00:00.09 | 7238 | 3646 | |
25 | Predicate Information (identified by operation id): |
28 | 2 - filter( "RA" . "STARTDATE" <=TO_DATE(:REFERENCE_DATE, 'yyyy-mm-dd' )) |
The shortest query, the shortest plan and the fastest execution. The
SORT GROUP BY immediately reduces the number of intermediate rows from
297K to 100K, whereas the WINDOW SORT PUSHED RANK had to compute the
row_number for all 297K rows.
Much Ado About Nothing?
I was reading this presentation PDF of Hugh Darwen recently, called
How To Handle Missing Information Without Using NULL.
Several great thinkers and founders of the relational theory consider
NULL as the thing that should not be. For example, one slide in the
above mentioned PDF is titled
SQL's Nulls Are A Disaster. And I found a paper with the amusing title
The Final Null In The Coffin.
I can understand the critique. The introduction of NULL leads to three
valued logic, which makes programs much more complex and harder to prove
correct. All database professionals likely have been bitten by NULLs
several times during their career, myself included. And a NULL can have
several interpretations. By using NULL, you are not making clear what is
meant. If the value for column hair_colour is NULL, does it mean the
person is bald? Or do you know the person has hair, but you just don't
know what colour? Or can the person be bald or have hair, but you just
don't know which one applies? Or is the person in the midst of a hair
colouring exercise and you only temporarily don't know the colour? If
you're creative, I'm sure you can come up with other interpretations as
well.
On the other hand, the theorists don't have to build database
applications for end users who like reasonable response times, and I do.
Avoiding nulls at all cost typically leads to a data model that has
more tables than needed, requiring more joins and therefore making
queries slower. So I have to make a trade off. In general I try to avoid
nullable columns as much as possible, for example by chosing subtype
implementations instead of supertype implementations, and by modelling
entity subtypes in the first place, but I will never let it noticeably
slow down my application. At my current job, I'm making a data model
right now. Having read all use cases, I know how the data will be used
and so I know where in the model there is room to avoid an extra
nullable column. One thing I'll never voluntarily do though, is make up
strange outlier values just to get rid of the null.
Any way, I was curious to see how Hugh Darwen handles missing
information without using nulls. In his paper, he has a concise example,
which I'll translate to Oracle syntax in this blogpost to see what
practically needs to happen to avoid nulls in his example. He starts
with this table:
07 | 1234 Anne Lawyer 100000 |
Which contains four NULL values. The meaning of those NULL values can't
be seen from this table, but this is what they are meant to be:
- Boris earns something, but we don't know how much
- Cindy does some job, but we don't know what it is
- Davinder doesn't have a job
- Davinder doesn't have a salary
So he applies a technique called vertical decomposition and on top of
those results horizontal decomposition, to arrive at the seven tables
below, where everything has a clear meaning.
Here we achieved a data model where every NULL has been banned out.
Now what if we'd like to simulate a query against the PERS_INFO table?
Darwen uses this expression to transform the seven tables back to the
PERS_INFO table:
01 | WITH (EXTEND JOB_UNK ADD ‘Job unknown’ AS Job_info) AS T1, |
02 | (EXTEND UNEMPLOYED ADD ‘Unemployed’ AS Job_info) AS T2, |
03 | (DOES_JOB RENAME (Job AS Job_info)) AS T3, |
04 | (EXTEND SALARY_UNK ADD ‘Salary unknown’ AS Sal_info) AS T4, |
05 | (EXTEND UNSALARIED ADD ‘Unsalaried’ AS Sal_info) AS T5, |
06 | (EXTEND EARNS ADD CHAR (Salary) AS Sal_info) AS T6, |
07 | (T6 { ALL BUT Salary }) AS T7, |
08 | ( UNION ( T1, T2, T3 )) AS T8, |
09 | ( UNION ( T4, T5, T7 )) AS T9, |
10 | ( JOIN ( CALLED, T8, T9 )) AS PERS_INFO : |
Translated to Oracle syntax, this becomes:
03 | 3 , 'Job unknown' as job_info |
08 | 8 , 'Unemployed' as job_info |
18 | 18 , 'Salary unknown' as sal_info |
23 | 23 , 'Unsalaried' as sal_info |
29 | 29 , to_char(salary, 'fm999G999' ) as sal_info |
69 | 69 inner join t8 j on (c.id = j.id) |
70 | 70 inner join t9 s on (c.id = s.id) |
76 | ID NAME JOB_INFO SAL_INFO |
78 | 1235 Boris Banker Salary unknown |
79 | 1237 Davinder Unemployed Unsalaried |
80 | 1234 Anne Lawyer 100,000 |
81 | 1236 Cindy Job unknown 70,000 |
Very elaborate, but the optimizer does a great job at simplifying the
query under the covers, as can be seen in this execution plan:
02 | 2 from table (dbms_xplan.display_cursor( null , null , 'allstats last' )) |
07 | SQL_ID bmrtdy0jad18p, child number 0 |
09 | with t1 as ( select id , 'Job unknown' as job_info from |
10 | job_unk ) , t2 as ( select id , 'Unemployed' as job_info |
11 | from unemployed ) , t3 as ( select id , job as job_info from |
12 | does_job ) , t4 as ( select id , 'Salary unknown' as sal_info |
13 | from salary_unk ) , t5 as ( select id , 'Unsalaried' as |
14 | sal_info from unsalaried ) , t6 as ( select id , salary |
15 | , to_char(salary, 'fm999G999' ) as sal_info from earns ) , t7 as ( |
16 | select id , sal_info from t6 ) , t8 as ( select id , |
17 | job_info from t1 union all select id , job_info |
18 | from t2 union all select id , job_info from t3 ) , t9 |
19 | as ( select id , sal_info from t4 union all select id |
20 | , sal_info from t5 union all select id , sal_info |
21 | from t7 ) , pers_info as ( select c.id , c. name , |
22 | j.job_info , s.sal_info from called c inner join t8 |
25 | Plan hash value: 583520090 |
28 | | Id | Operation | Name | Starts | E- Rows | A- Rows | A- Time | Buffers | OMem | 1Mem | Used-Mem | |
30 | | 0 | SELECT STATEMENT | | 1 | | 4 |00:00:00.01 | 14 | | | | |
31 | |* 1 | HASH JOIN | | 1 | 4 | 4 |00:00:00.01 | 14 | 1011K| 1011K| 550K (0)| |
32 | |* 2 | HASH JOIN | | 1 | 4 | 4 |00:00:00.01 | 8 | 1180K| 1180K| 548K (0)| |
33 | | 3 | TABLE ACCESS FULL | CALLED | 1 | 4 | 4 |00:00:00.01 | 2 | | | | |
34 | | 4 | VIEW | | 1 | 4 | 4 |00:00:00.01 | 6 | | | | |
35 | | 5 | UNION - ALL | | 1 | | 4 |00:00:00.01 | 6 | | | | |
36 | | 6 | TABLE ACCESS FULL | JOB_UNK | 1 | 1 | 1 |00:00:00.01 | 2 | | | | |
37 | | 7 | TABLE ACCESS FULL | UNEMPLOYED | 1 | 1 | 1 |00:00:00.01 | 2 | | | | |
38 | | 8 | TABLE ACCESS FULL | DOES_JOB | 1 | 2 | 2 |00:00:00.01 | 2 | | | | |
39 | | 9 | VIEW | | 1 | 4 | 4 |00:00:00.01 | 6 | | | | |
40 | | 10 | UNION - ALL | | 1 | | 4 |00:00:00.01 | 6 | | | | |
41 | | 11 | TABLE ACCESS FULL | SALARY_UNK | 1 | 1 | 1 |00:00:00.01 | 2 | | | | |
42 | | 12 | TABLE ACCESS FULL | UNSALARIED | 1 | 1 | 1 |00:00:00.01 | 2 | | | | |
43 | | 13 | TABLE ACCESS FULL | EARNS | 1 | 2 | 2 |00:00:00.01 | 2 | | | | |
46 | Predicate Information (identified by operation id): |
49 | 1 - access( "C" . "ID" = "S" . "ID" ) |
50 | 2 - access( "C" . "ID" = "J" . "ID" ) |
If I had to build the PERS_INFO table back again with a query myself, I'd use this shorter query with six left outer joins:
03 | 3 , coalesce (j.job,nvl2(ju.id, 'Job unknown' , null ),nvl2(ue.id, 'Unemployed' , null )) job_info |
04 | 4 , coalesce (to_char(e.salary, 'fm999G999' ),nvl2(su.id, 'Salary unknown' , null ),nvl2(us.id, 'Unsalaried' , null )) salary_info |
06 | 6 left outer join does_job j on (c.id = j.id) |
07 | 7 left outer join job_unk ju on (c.id = ju.id) |
08 | 8 left outer join unemployed ue on (c.id = ue.id) |
09 | 9 left outer join earns e on (c.id = e.id) |
10 | 10 left outer join salary_unk su on (c.id = su.id) |
11 | 11 left outer join unsalaried us on (c.id = us.id) |
14 | ID NAME JOB_INFO SALARY_INFO |
16 | 1234 Anne Lawyer 100,000 |
17 | 1236 Cindy Job unknown 70,000 |
18 | 1235 Boris Banker Salary unknown |
19 | 1237 Davinder Unemployed Unsalaried |
Although, as you can see below, the plan doesn't really improve:
02 | 2 from table (dbms_xplan.display_cursor( null , null , 'allstats last' )) |
07 | SQL_ID 6x45b27mvpb1m, child number 0 |
09 | select c.id , c. name , coalesce (j.job,nvl2(ju.id, 'Job |
10 | unknown' , null ),nvl2(ue.id, 'Unemployed' , null )) job_info , |
11 | coalesce (to_char(e.salary, 'fm999G999' ),nvl2(su.id, 'Salary |
12 | unknown' , null ),nvl2(us.id, 'Unsalaried' , null )) salary_info from called |
13 | c left outer join does_job j on (c.id = j.id) left outer |
14 | join job_unk ju on (c.id = ju.id) left outer join unemployed ue |
15 | on (c.id = ue.id) left outer join earns e on (c.id = e.id) |
16 | left outer join salary_unk su on (c.id = su.id) left outer join |
17 | unsalaried us on (c.id = us.id) |
19 | Plan hash value: 3398518218 |
22 | | Id | Operation | Name | Starts | E- Rows | A- Rows | A- Time | Buffers | OMem | 1Mem | Used-Mem | |
24 | | 0 | SELECT STATEMENT | | 1 | | 4 |00:00:00.01 | 15 | | | | |
25 | |* 1 | HASH JOIN OUTER | | 1 | 4 | 4 |00:00:00.01 | 15 | 955K| 955K| 528K (0)| |
26 | |* 2 | HASH JOIN OUTER | | 1 | 4 | 4 |00:00:00.01 | 12 | 1000K| 1000K| 523K (0)| |
27 | |* 3 | HASH JOIN OUTER | | 1 | 4 | 4 |00:00:00.01 | 10 | 1035K| 1035K| 536K (0)| |
28 | |* 4 | HASH JOIN OUTER | | 1 | 4 | 4 |00:00:00.01 | 8 | 1063K| 1063K| 536K (0)| |
29 | |* 5 | HASH JOIN OUTER | | 1 | 4 | 4 |00:00:00.01 | 6 | 1114K| 1114K| 537K (0)| |
30 | |* 6 | HASH JOIN OUTER | | 1 | 4 | 4 |00:00:00.01 | 4 | 1180K| 1180K| 538K (0)| |
31 | | 7 | TABLE ACCESS FULL | CALLED | 1 | 4 | 4 |00:00:00.01 | 2 | | | | |
32 | | 8 | TABLE ACCESS FULL | JOB_UNK | 1 | 1 | 1 |00:00:00.01 | 2 | | | | |
33 | | 9 | TABLE ACCESS FULL | UNEMPLOYED | 1 | 1 | 1 |00:00:00.01 | 2 | | | | |
34 | | 10 | TABLE ACCESS FULL | SALARY_UNK | 1 | 1 | 1 |00:00:00.01 | 2 | | | | |
35 | | 11 | TABLE ACCESS FULL | UNSALARIED | 1 | 1 | 1 |00:00:00.01 | 2 | | | | |
36 | | 12 | TABLE ACCESS FULL | DOES_JOB | 1 | 2 | 2 |00:00:00.01 | 2 | | | | |
37 | | 13 | TABLE ACCESS FULL | EARNS | 1 | 2 | 2 |00:00:00.01 | 3 | | | | |
40 | Predicate Information (identified by operation id): |
43 | 1 - access( "C" . "ID" = "E" . "ID" ) |
44 | 2 - access( "C" . "ID" = "J" . "ID" ) |
45 | 3 - access( "C" . "ID" = "US" . "ID" ) |
46 | 4 - access( "C" . "ID" = "SU" . "ID" ) |
47 | 5 - access( "C" . "ID" = "UE" . "ID" ) |
48 | 6 - access( "C" . "ID" = "JU" . "ID" ) |
But the two plans above are really complex, compared with a simple query against the PERS_INFO table with nullable columns:
07 | 1234 Anne Lawyer 100000 |
15 | 2 from table (dbms_xplan.display_cursor( null , null , 'allstats last' )) |
20 | SQL_ID 016x9f106gj27, child number 1 |
22 | select * from pers_info |
24 | Plan hash value: 1584579034 |
27 | | Id | Operation | Name | Starts | E- Rows | A- Rows | A- Time | Buffers | |
29 | | 0 | SELECT STATEMENT | | 1 | | 4 |00:00:00.01 | 7 | |
30 | | 1 | TABLE ACCESS FULL | PERS_INFO | 1 | 4 | 4 |00:00:00.01 | 7 | |
If queries like this are not very frequent in your database, you might
want to take this extra work for granted and avoid the NULL. But you
need to consider something else as well: the new schema requires much
more constraints. Using just the PERS_INFO table, a single primary key
constraint on the Id column is all you need. But for the new model,
Darwen describes 9, but really 15 constraints:
- No two CALLED rows have the same Id. (Primary key)
- Every row in CALLED has a matching row in either DOES_JOB, JOB_UNK, or UNEMPLOYED.
- No row in DOES_JOB has a matching row in JOB_UNK.
- No row in DOES_JOB has a matching row in UNEMPLOYED.
- No row in JOB_UNK has a matching row in UNEMPLOYED.
- Every row in DOES_JOB has a matching row in CALLED. (Foreign key)
- Every row in JOB_UNK has a matching row in CALLED. (Foreign key)
- Every row in UNEMPLOYED has a matching row in CALLED. (Foreign key)
- Constraints 2 through 8 repeated, mutatis mutandis, for CALLED with respect to EARNS, SALARY_UNK and UNSALARIED.
Implementing constraint 1 is easy:
1 | SQL> alter table called add primary key (id) |
And so are constraints 6, 7 and 8:
01 | SQL> alter table does_job add foreign key (id) references called (id) |
06 | SQL> alter table job_unk add foreign key (id) references called (id) |
11 | SQL> alter table unemployed add foreign key (id) references called (id) |
But constraint 2 says that the Id in table CALLED is a foreign
distributed key. And constraints 3, 4 and 5 say the Id's of tables
DOES_JOB, JOB_UNK and UNEMPLOYED are a distributed key. Oracle doesn't
have declarative support for distributed keys or for foreign distributed
keys. We could write database trigger code to implement this, which is
very hard to do correct or we could use the materialized view trick to
have the condition validated at the end of a transaction, instead of at
the end of the statement, which also
has its downsides.
And such deferred constraint checking is explicitly ruled out by The
Third Manifesto as well. Nevertheless, here is how it can be done.
The distributed key (constraints 3, 4 and 5):
01 | SQL> create materialized view log on does_job with rowid |
04 | Materialized view log created. |
06 | SQL> create materialized view log on job_unk with rowid |
09 | Materialized view log created. |
11 | SQL> create materialized view log on unemployed with rowid |
14 | Materialized view log created. |
16 | SQL> create materialized view distributed_key_vw |
17 | 2 refresh fast on commit |
35 | Materialized view created. |
37 | SQL> alter table distributed_key_vw |
38 | 2 add constraint distributed_key_check |
And to show that the distributed key implementation works:
01 | SQL> insert into job_unk values (1234) |
11 | ORA-12048: error encountered while refreshing materialized view "RWIJK" . "DISTRIBUTED_KEY_VW" |
12 | ORA-00001: unique constraint (RWIJK.DISTRIBUTED_KEY_CHECK) violated |
And the foreign distributed key ("Every row in CALLED has a matching row
in either DOES_JOB, JOB_UNK, or UNEMPLOYED.") can be implemented like
this:
01 | SQL> create materialized view log on does_job with rowid |
04 | Materialized view log created. |
06 | SQL> create materialized view log on job_unk with rowid |
09 | Materialized view log created. |
11 | SQL> create materialized view log on unemployed with rowid |
14 | Materialized view log created. |
16 | SQL> create materialized view foreign_distributed_key_vw |
17 | 2 refresh fast on commit |
19 | 4 select c.rowid c_rowid |
31 | 16 where c.id = dj.id (+) |
32 | 17 and c.id = ju.id (+) |
33 | 18 and c.id = ue.id (+) |
36 | Materialized view created. |
38 | SQL> alter table foreign_distributed_key_vw |
39 | 2 add constraint foreign_distributed_key_check |
40 | 3 check ( coalesce (dj_id,ju_id,ue_id) is not null ) |
And some proof that this implementation works:
01 | SQL> insert into called values (1238, 'Elise' ) |
11 | ORA-12008: error in materialized view refresh path |
12 | ORA-02290: check constraint (RWIJK.FOREIGN_DISTRIBUTED_KEY_CHECK) violated |
Would I go through the extra trouble of an implementation with 6 more
tables, 14 extra constraints and worse performance like above? It
depends. It depends on how often the data is queried, and on how often
it is updated concurrently. And on whether the distinction between the
possible multiple meanings of NULL is relevant in my case. And whether I
have sufficient extra time to implement it. Using Oracle, probably most
often, I won't.
A hierarchical query is typically executed using a plan that starts
with the operation CONNECT BY WITH FILTERING, which has two child
operations. The first child operation implements the START WITH clause
and the second child operation contains a step called CONNECT BY PUMP,
implementing the recursive part of the query. Here is an example of such
a plan using the well known hierarchical query on table EMP:
01 | SQL> select lpad( ' ' , 2 * level - 2, ' ' ) || ename as ename |
06 | 6 connect by mgr = prior empno |
07 | 7 start with mgr is null |
29 | SQL> select * from table (dbms_xplan.display_cursor( null , null , 'allstats last' )) |
34 | SQL_ID d2c7xqxbr112u, child number 0 |
36 | select lpad( ' ' , 2 * level - 2, ' ' ) || ename as ename , level , job , deptno from emp connect by |
37 | mgr = prior empno start with mgr is null |
39 | Plan hash value: 1869448388 |
42 | | Id | Operation | Name | Starts | E- Rows | A- Rows | A- Time | Buffers | Reads | OMem | 1Mem | Used-Mem | |
44 | |* 1 | CONNECT BY WITH FILTERING| | 1 | | 14 |00:00:00.02 | 15 | 6 | 2048 | 2048 | 2048 (0)| |
45 | |* 2 | TABLE ACCESS FULL | EMP | 1 | 1 | 1 |00:00:00.01 | 3 | 6 | | | | |
46 | |* 3 | HASH JOIN | | 4 | | 13 |00:00:00.01 | 12 | 0 | 1452K| 1452K| 853K (0)| |
47 | | 4 | CONNECT BY PUMP | | 4 | | 14 |00:00:00.01 | 0 | 0 | | | | |
48 | | 5 | TABLE ACCESS FULL | EMP | 4 | 2 | 56 |00:00:00.01 | 12 | 0 | | | | |
51 | Predicate Information (identified by operation id): |
54 | 1 - access( "MGR" = PRIOR NULL ) |
55 | 2 - filter( "MGR" IS NULL ) |
56 | 3 - access( "MGR" = PRIOR NULL ) |
You can see a great and more detailed explanation of connect by with filtering
here on Christian Antognini's blog.
When I was researching the new
recursive subquery factoring
clause one and a half year ago, and compared a standard hierarchical
query on EMP using recursive subquery factoring with a query using the
good old connect by, I stumbled upon a new optimizer algorithm for
implementing recursive queries:
01 | SQL> select lpad( ' ' , 2 * level - 2, ' ' ) || ename as ename |
06 | 6 connect by mgr = prior empno |
07 | 7 start with mgr is null |
29 | SQL> select * from table (dbms_xplan.display_cursor( null , null , 'allstats last' )) |
34 | SQL_ID d2c7xqxbr112u, child number 0 |
36 | select lpad( ' ' , 2 * level - 2, ' ' ) || ename as ename , level |
37 | , job , deptno from emp connect by mgr = prior empno |
40 | Plan hash value: 763482334 |
43 | | Id | Operation | Name | Starts | E- Rows | A- Rows | A- Time | Buffers | Reads | |
45 | | 0 | SELECT STATEMENT | | 1 | | 14 |00:00:00.02 | 6 | 6 | |
46 | |* 1 | CONNECT BY NO FILTERING WITH START- WITH | | 1 | | 14 |00:00:00.02 | 6 | 6 | |
47 | | 2 | TABLE ACCESS FULL | EMP | 1 | 14 | 14 |00:00:00.02 | 6 | 6 | |
50 | Predicate Information (identified by operation id): |
53 | 1 - access( "MGR" = PRIOR NULL ) |
You
might wonder what I did to make two exactly the same queries to use a
different execution plan, but I'll address that later. First, I'd like
to show there are two optimizer hints available, with which you can
control which algorithm the optimizer uses:
03 | 3 where name like '%CONNECT_BY_FILTERING%' |
08 | INVERSE TARGET_LEVEL PROPERTY VERSION VERSION_OUTLINE |
10 | CONNECT_BY_FILTERING QKSFM_ALL CONNECT_BY_FILTERING |
11 | NO_CONNECT_BY_FILTERING 2 16 10.2.0.2 10.2.0.2 |
13 | NO_CONNECT_BY_FILTERING QKSFM_ALL CONNECT_BY_FILTERING |
14 | CONNECT_BY_FILTERING 2 16 10.2.0.2 10.2.0.2 |
And
this was surprising to me. As the version column suggests, the
no_connect_by_filtering hint and the accompanying new algorithm were
already introduced in version 10.2.0.2! I checked with my old 10.2.0.4
database and it is indeed present and can be used there:
11 | SQL> select /*+ no_connect_by_filtering gather_plan_statistics */ |
12 | 2 lpad( ' ' , 2 * level - 2, ' ' ) || ename as ename |
17 | 7 connect by mgr = prior empno |
18 | 8 start with mgr is null |
40 | SQL> select * from table (dbms_xplan.display_cursor( null , null , 'allstats last' )) |
45 | SQL_ID 39kr5s8dxz7j0, child number 0 |
47 | select /*+ no_connect_by_filtering gather_plan_statistics */ lpad( ' ' , 2 * level - 2, ' |
48 | ' ) || ename as ename , level , job , deptno from emp connect by mgr = prior |
49 | empno start with mgr is null |
51 | Plan hash value: 763482334 |
54 | | Id | Operation | Name | Starts | E- Rows | A- Rows | A- Time | Buffers | |
56 | |* 1 | CONNECT BY NO FILTERING WITH START- WITH | | 1 | | 14 |00:00:00.01 | 3 | |
57 | | 2 | TABLE ACCESS FULL | EMP | 1 | 14 | 14 |00:00:00.01 | 3 | |
60 | Predicate Information (identified by operation id): |
63 | 1 - access( "MGR" = PRIOR NULL ) |
But
you need the no_connect_by_filtering hint in version 10.2.0.4 for this
query. If you do not provide the hint, this is the result:
01 | SQL> select /*+ gather_plan_statistics */ |
02 | 2 lpad( ' ' , 2 * level - 2, ' ' ) || ename as ename |
07 | 7 connect by mgr = prior empno |
08 | 8 start with mgr is null |
30 | SQL> select * from table (dbms_xplan.display_cursor( null , null , 'allstats last' )) |
35 | SQL_ID 6zhtnf720u0bm, child number 0 |
37 | select /*+ gather_plan_statistics */ lpad( ' ' , 2 * level - 2, ' ' ) || ename as ename , level |
38 | , job , deptno from emp connect by mgr = prior empno start with mgr is null |
40 | Plan hash value: 1869448388 |
43 | | Id | Operation | Name | Starts | E- Rows | A- Rows | A- Time | Buffers | OMem | 1Mem | Used-Mem | |
45 | |* 1 | CONNECT BY WITH FILTERING| | 1 | | 14 |00:00:00.01 | 15 | 2048 | 2048 | 2048 (0)| |
46 | |* 2 | TABLE ACCESS FULL | EMP | 1 | 1 | 1 |00:00:00.01 | 3 | | | | |
47 | |* 3 | HASH JOIN | | 4 | | 13 |00:00:00.01 | 12 | 1452K| 1452K| 843K (0)| |
48 | | 4 | CONNECT BY PUMP | | 4 | | 14 |00:00:00.01 | 0 | | | | |
49 | | 5 | TABLE ACCESS FULL | EMP | 4 | 2 | 56 |00:00:00.01 | 12 | | | | |
52 | Predicate Information (identified by operation id): |
55 | 1 - access( "MGR" = PRIOR NULL ) |
56 | 2 - filter( "MGR" IS NULL ) |
57 | 3 - access( "MGR" = PRIOR NULL ) |
Which
explains why I didn't see the CONNECT BY NO FILTERING WITH START-WITH
earlier. It seems that Oracle has adjusted the cost calculation of
connect by queries somewhere between 10.2.0.4 and 11.2.0.1. Just look at
the cost from both execution plans on 10.2.0.4 using a regular explain
plan statement and a "select * from table(dbms_xplan.display):
02 | | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | |
04 | | 0 | SELECT STATEMENT | | 2 | 50 | 3 (0)| 00:00:01 | |
05 | |* 1 | CONNECT BY WITH FILTERING| | | | | | |
06 | |* 2 | TABLE ACCESS FULL | EMP | 1 | 29 | 3 (0)| 00:00:01 | |
07 | |* 3 | HASH JOIN | | | | | | |
08 | | 4 | CONNECT BY PUMP | | | | | | |
09 | | 5 | TABLE ACCESS FULL | EMP | 2 | 50 | 3 (0)| 00:00:01 | |
2 | | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | |
4 | | 0 | SELECT STATEMENT | | 14 | 350 | 3 (0)| 00:00:01 | |
5 | |* 1 | CONNECT BY NO FILTERING WITH START- WITH | | | | | | |
6 | | 2 | TABLE ACCESS FULL | EMP | 14 | 350 | 3 (0)| 00:00:01 | |
The cost of 3 is due to the full table scan of EMP, and no additional cost is added for the hierarchical query.
These are the plans from 11.2.0.2:
02 | | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | |
04 | | 0 | SELECT STATEMENT | | 3 | 156 | 15 (20)| 00:00:01 | |
05 | |* 1 | CONNECT BY WITH FILTERING| | | | | | |
06 | |* 2 | TABLE ACCESS FULL | EMP | 1 | 25 | 4 (0)| 00:00:01 | |
07 | |* 3 | HASH JOIN | | 2 | 76 | 9 (12)| 00:00:01 | |
08 | | 4 | CONNECT BY PUMP | | | | | | |
09 | |* 5 | TABLE ACCESS FULL | EMP | 13 | 325 | 4 (0)| 00:00:01 | |
2 | | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | |
4 | | 0 | SELECT STATEMENT | | 14 | 728 | 5 (20)| 00:00:01 | |
5 | |* 1 | CONNECT BY NO FILTERING WITH START- WITH | | | | | | |
6 | | 2 | TABLE ACCESS FULL | EMP | 14 | 350 | 4 (0)| 00:00:01 | |
The
numbers from the 11.2.0.2 show more sophistication than just the cost
of the table scan. The optimizer can't know how many levels deep the
data is, but version 10.2.0.4 apparently picked 1, and left the total
cost unchanged from 3 to 3. I'm curious to know in which version in
between 10.2.0.4 and 11.2.0.2 this cost calculation changed. If anyone
who is reading this, has a version in between and likes to check, please
let me know in the comments. My guess would be that 11.2.0.1 contained
the cost change.
What does CONNECT BY NO FILTERING WITH START-WITH do?Let's explore this, using this table:
01 | SQL> create table t (id, parent_id, value, indicator) |
04 | 4 , case level when 1 then null else trunc(( level -1)/10) end |
05 | 5 , round(dbms_random.value * 1000) |
06 | 6 , case mod( level ,10) when 4 then 'N' else 'Y' end |
08 | 8 connect by level <= 100000 |
14 | 2 add constraint cbt_pk |
20 | SQL> create index i1 on t (parent_id,indicator) |
25 | SQL> exec dbms_stats.gather_table_stats( user , 't' , cascade => true ) |
The
data is tree shaped where each parent node has exactly 9 child nodes.
One tenth of the data, with an id that ends with the digit 3, has its
indicator column set to 'N'. This select query will make it clearer how
the data looks like:
03 | 3 where id < 24 or id > 99997 |
When
hearing the word "filter", I almost immediately associate it with a
WHERE clause. But a where clause in a connect by query, is not what is
meant by connect by filtering.
The documentation states:
Oracle processes hierarchical queries as follows:
A join, if present, is evaluated first, whether the join is specified in the FROM clause or with WHERE clause predicates.
The CONNECT BY condition is evaluated.
Any remaining WHERE clause predicates are evaluated.
So a where clause predicate is evaluated AFTER the connect by has done its job. You can see that happening here:
SQL> explain plan
2 for
3 select id
4 , parent_id
5 , sys_connect_by_path(id,'->') scbp
6 from t
7 where indicator = 'N'
8 connect by parent_id = prior id
9 start with parent_id is null
10 /
Explained.
SQL> select * from table(dbms_xplan.display)
2 /
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2502271019
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 11 | 319 | 164 (3)| 00:00:02 |
|* 1 | FILTER | | | | | |
|* 2 | CONNECT BY WITH FILTERING | | | | | |
|* 3 | TABLE ACCESS FULL | T | 1 | 11 | 80 (2)| 00:00:01 |
| 4 | NESTED LOOPS | | 10 | 240 | 82 (2)| 00:00:01 |
| 5 | CONNECT BY PUMP | | | | | |
| 6 | TABLE ACCESS BY INDEX ROWID| T | 10 | 110 | 2 (0)| 00:00:01 |
|* 7 | INDEX RANGE SCAN | I1 | 10 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("INDICATOR"='N')
2 - access("PARENT_ID"=PRIOR "ID")
3 - filter("PARENT_ID" IS NULL)
7 - access("PARENT_ID"="connect$_by$_pump$_002"."prior id ")
22 rows selected.
The
"indicator = 'N'" predicate is at step 1, which is executed after the
CONNECT BY WITH FILTERING at step 2. Note that although this query is
executed in 11.2.0.2, the optimizer has chosen the old CONNECT BY WITH
FILTERING.
Connect by filtering is done by using filters in your
CONNECT BY clause. Here is an example using the predicate "indicator =
'N'" inside the CONNECT BY clause:
03 | 3 , sys_connect_by_path(id, '->' ) scbp |
05 | 5 connect by parent_id = prior id |
07 | 7 start with parent_id is null |
15 | 333 33 ->0->3->33->333 |
16 | 3333 333 ->0->3->33->333->3333 |
17 | 33333 3333 ->0->3->33->333->3333->33333 |
21 | SQL> select * from table (dbms_xplan.display_cursor( null , null , 'allstats last' )) |
26 | SQL_ID dzkjzrrzgnvd5, child number 0 |
28 | select id , parent_id , sys_connect_by_path(id, '->' ) scbp |
29 | from t connect by parent_id = prior id and indicator = 'N' |
30 | start with parent_id is null |
32 | Plan hash value: 3164577763 |
35 | | Id | Operation | Name | Starts | E- Rows | A- Rows | A- Time | Buffers | OMem | 1Mem | Used-Mem | |
37 | | 0 | SELECT STATEMENT | | 1 | | 6 |00:00:00.01 | 294 | | | | |
38 | |* 1 | CONNECT BY WITH FILTERING | | 1 | | 6 |00:00:00.01 | 294 | 2048 | 2048 | 2048 (0)| |
39 | |* 2 | TABLE ACCESS FULL | T | 1 | 1 | 1 |00:00:00.01 | 277 | | | | |
40 | | 3 | NESTED LOOPS | | 6 | 5 | 5 |00:00:00.01 | 17 | | | | |
41 | | 4 | CONNECT BY PUMP | | 6 | | 6 |00:00:00.01 | 0 | | | | |
42 | | 5 | TABLE ACCESS BY INDEX ROWID| T | 6 | 5 | 5 |00:00:00.01 | 17 | | | | |
43 | |* 6 | INDEX RANGE SCAN | I1 | 6 | 5 | 5 |00:00:00.01 | 12 | | | | |
46 | Predicate Information (identified by operation id): |
49 | 1 - access( "PARENT_ID" = PRIOR NULL ) |
50 | 2 - filter( "PARENT_ID" IS NULL ) |
51 | 6 - access( "PARENT_ID" = "connect$_by$_pump$_002" . "prior id " AND "INDICATOR" = 'N' ) |
In
the A-rows column, you can see that the connect by filtering was
effective here. Only the necessary rows were being read. And this is the
key difference between the two connect by algorithms: with CONNECT BY
WITH FILTERING, you can filter within each recursion, whereas CONNECT BY
NO FILTERING WITH START-WITH has to read everything, does an in-memory
operation, and return the result. With this example, the latter is much
less efficient:
01 | SQL> select /*+ no_connect_by_filtering */ id |
03 | 3 , sys_connect_by_path(id, '->' ) scbp |
05 | 5 connect by parent_id = prior id |
07 | 7 start with parent_id is null |
15 | 333 33 ->0->3->33->333 |
16 | 3333 333 ->0->3->33->333->3333 |
17 | 33333 3333 ->0->3->33->333->3333->33333 |
21 | SQL> select * from table (dbms_xplan.display_cursor( null , null , 'allstats last' )) |
26 | SQL_ID 3fcr31tp83by9, child number 0 |
28 | select /*+ no_connect_by_filtering */ id , parent_id , |
29 | sys_connect_by_path(id, '->' ) scbp from t connect by parent_id = |
30 | prior id and indicator = 'N' start with parent_id is null |
32 | Plan hash value: 2303479083 |
35 | | Id | Operation | Name | Starts | E- Rows | A- Rows | A- Time | Buffers | |
37 | | 0 | SELECT STATEMENT | | 1 | | 6 |00:00:00.14 | 277 | |
38 | |* 1 | CONNECT BY NO FILTERING WITH START- WITH | | 1 | | 6 |00:00:00.14 | 277 | |
39 | | 2 | TABLE ACCESS FULL | T | 1 | 100K| 100K|00:00:00.01 | 277 | |
42 | Predicate Information (identified by operation id): |
45 | 1 - access( "PARENT_ID" = PRIOR NULL ) |
46 | filter( "PARENT_ID" IS NULL ) |
100K
rows were being read, and the A-time was 0.14 seconds instead of 0.01
seconds. I wondered where those 0.14 seconds went to, since the plan
shows it's NOT for the full table scan. Using Tom Kyte's runstats_pkg
reveals this:
04 | 4 select /*+ connect_by_filtering */ id |
06 | 6 , sys_connect_by_path(id, '->' ) scbp |
08 | 8 connect by parent_id = prior id |
10 | 10 start with parent_id is null |
14 | 14 select /*+ no_connect_by_filtering */ id |
16 | 16 , sys_connect_by_path(id, '->' ) scbp |
18 | 18 connect by parent_id = prior id |
19 | 19 and indicator = 'N' |
20 | 20 start with parent_id is null |
23 | 23 runstats_pkg.rs_start; |
24 | 24 for r in c1 loop null ; end loop; |
25 | 25 runstats_pkg.rs_middle; |
26 | 26 for r in c2 loop null ; end loop; |
27 | 27 runstats_pkg.rs_stop; |
32 | run 1 ran in 0% of the time |
35 | STAT...HSC Heap Segment Block 16 15 -1 |
36 | STAT...db block changes 48 47 -1 |
37 | STAT...consistent gets - exami 9 8 -1 |
38 | STAT...db block gets from cach 32 33 1 |
39 | STAT...db block gets 32 33 1 |
40 | STAT...redo subscn max counts 0 1 1 |
41 | STAT...redo ordering marks 0 1 1 |
42 | STAT...redo entries 16 15 -1 |
43 | STAT...calls to kcmgas 0 1 1 |
44 | STAT...calls to kcmgcs 29 28 -1 |
45 | STAT... free buffer requested 0 1 1 |
46 | STAT...Heap Segment Array Inse 16 15 -1 |
47 | STAT...consistent changes 32 31 -1 |
48 | STAT...heap block compress 9 8 -1 |
49 | STAT...parse time cpu 1 0 -1 |
50 | STAT...buffer is pinned count 1 0 -1 |
51 | STAT...session cursor cache co 1 0 -1 |
52 | STAT...sql area evicted 1 0 -1 |
53 | LATCH.undo global data 11 10 -1 |
54 | LATCH.SQL memory manager worka 3 5 2 |
56 | LATCH.OS process allocation 0 2 2 |
57 | LATCH.simulator hash latch 20 23 3 |
58 | LATCH.object queue header oper 4 1 -3 |
59 | STAT...workarea executions - o 10 6 -4 |
60 | STAT... table fetch by rowid 15 10 -5 |
61 | STAT... index scans kdiixs1 6 0 -6 |
62 | LATCH.row cache objects 280 274 -6 |
63 | STAT...sorts (memory) 8 2 -6 |
64 | STAT...CPU used by this sessio 2 11 9 |
65 | STAT...Elapsed Time 1 11 10 |
66 | STAT...recursive cpu usage 2 12 10 |
67 | STAT... no work - consistent re 300 284 -16 |
68 | STAT...buffer is not pinned co 36 20 -16 |
69 | STAT...session logical reads 354 337 -17 |
70 | STAT...consistent gets from ca 313 296 -17 |
71 | STAT...consistent gets from ca 322 304 -18 |
72 | LATCH.shared pool 186 168 -18 |
73 | STAT...consistent gets 322 304 -18 |
74 | LATCH.shared pool simulator 23 4 -19 |
75 | LATCH.cache buffers chains 785 740 -45 |
76 | STAT...undo change vector size 3,500 3,420 -80 |
77 | STAT...redo size 4,652 4,560 -92 |
78 | STAT...session uga memory 0 -65,488 -65,488 |
79 | STAT...session pga memory 0 -65,536 -65,536 |
80 | STAT...sorts ( rows ) 12 100,001 99,989 |
82 | Run1 latches total versus runs |
84 | 1,467 1,384 -83 106.00% |
86 | PL/SQL procedure successfully completed |
The
major difference is the number of rows sorted! The CONNECT BY NO
FILTERING WITH START-WITH sorts all 100K rows. This is a surprise,
because normally when you sort, you use memory from the PGA workarea,
which shows up in your memory statistics from your execution plan. But
the no filtering plan did not show those statistics (OMem, 1Mem,
Used-Mem). I have no explanation for this phenomenon yet.
Let's zoom in on the sorting:
05 | 5 where ms.statistic# = sn.statistic# |
06 | 6 and sn. name like '%sort%' |
19 | 3 , sys_connect_by_path(id, '->' ) scbp |
21 | 5 connect by parent_id = prior id |
23 | 7 start with parent_id is null |
31 | 333 33 ->0->3->33->333 |
32 | 3333 333 ->0->3->33->333->3333 |
33 | 33333 3333 ->0->3->33->333->3333->33333 |
41 | 5 where ms.statistic# = sn.statistic# |
42 | 6 and sn. name like '%sort%' |
53 | SQL> select /*+ no_connect_by_filtering */ id |
55 | 3 , sys_connect_by_path(id, '->' ) scbp |
57 | 5 connect by parent_id = prior id |
59 | 7 start with parent_id is null |
67 | 333 33 ->0->3->33->333 |
68 | 3333 333 ->0->3->33->333->3333 |
69 | 33333 3333 ->0->3->33->333->3333->33333 |
77 | 5 where ms.statistic# = sn.statistic# |
78 | 6 and sn. name like '%sort%' |
So
CONNECT BY WITH FILTERING did 8 sorts (2286 - 2278) and sorted 12 rows
(9425522 - 9425510), whereas CONNECT BY NO FILTERING WITH START-WITH did
2 (2288 - 2286) sorts and sorted 100,001 rows (9525523 - 9425522).
And
finally, I promised to explain why the first two queries of this
blogpost are identical, but show a different execution plan. The reason
is simple: the first one is executed on 10.2.0.4 and the second one on
11.2.0.2.
hi go through below link for more interview questions on sql and plsql....
http://swaretesting.blogspot.in/2010/08/sql-and-plsql-interview-questions-iii.html