SCD2s And Their Affect On The CBO Part II
I wrote a while ago in a post here that the CBO has no way of knowing how many rows it will return for a given fixed date when applied to the FROM_DATE / TO_DATE of an SCD2 table…well, as I said I would in the article, I caught up with Wolfgang Breitling at the UKOUG after he did a presentation on Histograms and was able to discuss this problem with him further, whereupon he suggested that using dynamic sampling at level 4 or above might allow the CBO to determine the selectivity for combinations of FROM_DATE / TO_DATE in the target SCD2 table…I’ve just tried that on my new XE database on Windows and I’m pleased to say that it does seem to work nicely…so thanks very much to Wolfgang for that advice.
From the log below you’ll see that with dynamic sampling level 4 the CBO is able to determine the correct number of rows to be returned (13 instead of 1).
jeff[28/5]@XE> @test_dynamic_sampling.sql
jeff[28/5]@XE> REM Script: test_dynamic_sampling.sql
jeff[28/5]@XE> REM Author: Jeff Moss
jeff[28/5]@XE> REM Purpose: To test out dynamic sampling level 4 after talking to
jeff[28/5]@XE> REM Wolfgang Breitling about our selectivity issue with
jeff[28/5]@XE> REM SCD2 FROM_DATE/TO_DATE tables.
jeff[28/5]@XE>
jeff[28/5]@XE> REM Setup some environment stuff
jeff[28/5]@XE> set define on
jeff[28/5]@XE> set linesize 132
jeff[28/5]@XE> set pagesize 1000
jeff[28/5]@XE> set wrap off
jeff[28/5]@XE> SET PAUSE OFF
jeff[28/5]@XE> set verify on
jeff[28/5]@XE> set echo on
jeff[28/5]@XE> column other_tag heading “Parallel” format a10
jeff[28/5]@XE> column operation heading “Operation” format a30
jeff[28/5]@XE> column order heading “Order” format a10
jeff[28/5]@XE> column cardinality heading “Cardinality” format 999,999,990
jeff[28/5]@XE> column num_buckets heading “NumBuckets” format 99
jeff[28/5]@XE> column histogram heading “Histogram” format a16
jeff[28/5]@XE>
jeff[28/5]@XE> REM Drop the test table…
jeff[28/5]@XE> DROP TABLE jeff_dynamic_sampling;
Table dropped.
Elapsed: 00:00:00.06
jeff[28/5]@XE>
jeff[28/5]@XE> REM Create the test table…
jeff[28/5]@XE> CREATE TABLE jeff_dynamic_sampling(pk_col NUMBER NOT NULL
2 ,non_key_attribute VARCHAR2(20) NOT NULL
3 ,from_date DATE NOT NULL
4 ,to_date DATE NULL
5 );
Table created.
Elapsed: 00:00:00.03
jeff[28/5]@XE>
jeff[28/5]@XE> REM Populate the test table with skewed data…
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(1,’XXX’,TO_DATE(’01-JAN-2005′,’DD-MON-YYYY’),TO_DATE(’31-JAN-2005′,’DD-MON-YYYY’));
1 row created.
Elapsed: 00:00:00.00
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(2,’XXX’,TO_DATE(’01-FEB-2005′,’DD-MON-YYYY’),TO_DATE(’28-FEB-2005′,’DD-MON-YYYY’));
1 row created.
Elapsed: 00:00:00.01
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(3,’XXX’,TO_DATE(’01-MAR-2005′,’DD-MON-YYYY’),TO_DATE(’31-MAR-2005′,’DD-MON-YYYY’));
1 row created.
Elapsed: 00:00:00.00
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(4,’XXX’,TO_DATE(’01-APR-2005′,’DD-MON-YYYY’),TO_DATE(’30-APR-2005′,’DD-MON-YYYY’));
1 row created.
Elapsed: 00:00:00.01
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(5,’XXX’,TO_DATE(’01-MAY-2005′,’DD-MON-YYYY’),TO_DATE(’31-MAY-2005′,’DD-MON-YYYY’));
1 row created.
Elapsed: 00:00:00.00
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(6,’XXX’,TO_DATE(’01-JUN-2005′,’DD-MON-YYYY’),TO_DATE(’30-JUN-2005′,’DD-MON-YYYY’));
1 row created.
Elapsed: 00:00:00.00
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(7,’XXX’,TO_DATE(’01-JUL-2005′,’DD-MON-YYYY’),TO_DATE(’31-JUL-2005′,’DD-MON-YYYY’));
1 row created.
Elapsed: 00:00:00.00
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(8,’XXX’,TO_DATE(’01-AUG-2005′,’DD-MON-YYYY’),TO_DATE(’31-AUG-2005′,’DD-MON-YYYY’));
1 row created.
Elapsed: 00:00:00.01
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(9,’XXX’,TO_DATE(’01-SEP-2005′,’DD-MON-YYYY’),TO_DATE(’30-SEP-2005′,’DD-MON-YYYY’));
1 row created.
Elapsed: 00:00:00.00
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(10,’XXX’,TO_DATE(’01-OCT-2005′,’DD-MON-YYYY’),TO_DATE(’31-OCT-2005′,’DD-MON-YYYY’));
1 row created.
Elapsed: 00:00:00.00
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(11,’XXX’,TO_DATE(’01-NOV-2005′,’DD-MON-YYYY’),TO_DATE(’30-NOV-2005′,’DD-MON-YYYY’));
1 row created.
Elapsed: 00:00:00.01
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(12,’XXX’,TO_DATE(’01-DEC-2005′,’DD-MON-YYYY’),TO_DATE(’31-DEC-2005′,’DD-MON-YYYY’));
1 row created.
Elapsed: 00:00:00.01
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(13,’XXX’,TO_DATE(’01-JAN-2005′,’DD-MON-YYYY’),NULL);
1 row created.
Elapsed: 00:00:00.00
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(14,’XXX’,TO_DATE(’01-JAN-2005′,’DD-MON-YYYY’),NULL);
1 row created.
Elapsed: 00:00:00.01
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(15,’XXX’,TO_DATE(’01-JAN-2005′,’DD-MON-YYYY’),NULL);
1 row created.
Elapsed: 00:00:00.01
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(16,’XXX’,TO_DATE(’01-JAN-2005′,’DD-MON-YYYY’),NULL);
1 row created.
Elapsed: 00:00:00.00
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(17,’XXX’,TO_DATE(’01-JAN-2005′,’DD-MON-YYYY’),NULL);
1 row created.
Elapsed: 00:00:00.00
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(18,’XXX’,TO_DATE(’01-JAN-2005′,’DD-MON-YYYY’),NULL);
1 row created.
Elapsed: 00:00:00.00
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(19,’XXX’,TO_DATE(’01-JAN-2005′,’DD-MON-YYYY’),NULL);
1 row created.
Elapsed: 00:00:00.01
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(20,’XXX’,TO_DATE(’01-JAN-2005′,’DD-MON-YYYY’),NULL);
1 row created.
Elapsed: 00:00:00.01
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(21,’XXX’,TO_DATE(’01-JAN-2005′,’DD-MON-YYYY’),NULL);
1 row created.
Elapsed: 00:00:00.01
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(22,’XXX’,TO_DATE(’01-JAN-2005′,’DD-MON-YYYY’),NULL);
1 row created.
Elapsed: 00:00:00.01
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(23,’XXX’,TO_DATE(’01-JAN-2005′,’DD-MON-YYYY’),NULL);
1 row created.
Elapsed: 00:00:00.01
jeff[28/5]@XE> INSERT INTO jeff_dynamic_sampling VALUES(24,’XXX’,TO_DATE(’01-JAN-2005′,’DD-MON-YYYY’),NULL);
1 row created.
Elapsed: 00:00:00.01
jeff[28/5]@XE> COMMIT;
Commit complete.
Elapsed: 00:00:00.00
jeff[28/5]@XE>
jeff[28/5]@XE> REM Gather stats on the table
jeff[28/5]@XE> BEGIN
2 dbms_stats.gather_table_stats(ownname => USER
3 ,tabname => ‘JEFF_DYNAMIC_SAMPLING’
4 ,method_opt => ‘FOR ALL COLUMNS SIZE 254’
5 ,estimate_percent => 100
6 );
7 END;
8 /
PL/SQL procedure successfully completed.
Elapsed: 00:00:00.07
jeff[28/5]@XE>
jeff[28/5]@XE> REM Now show the optimizer stats
jeff[28/5]@XE> SELECT column_name,low_value,high_value,num_buckets,histogram
2 FROM dba_tab_columns
3 WHERE table_name=’JEFF_DYNAMIC_SAMPLING’
4 ORDER BY column_id;
Column Low Value High Value Num Histogram
Buckets
—————– ——————– ——————– ——- ——————
PK_COL C102 C119 24 HEIGHT BALANCED
NON_KEY_ATTRIBUTE 585858 585858 1 FREQUENCY
FROM_DATE 78690101010101 78690C01010101 12 FREQUENCY
TO_DATE 7869011F010101 78690C1F010101 12 HEIGHT BALANCED
4 rows selected.
Elapsed: 00:00:00.06
jeff[28/5]@XE>
jeff[28/5]@XE> REM Turn autotrace on
jeff[28/5]@XE> SET AUTOTRACE ON
jeff[28/5]@XE>
jeff[28/5]@XE> REM First run a test statement using the default dynamic sampling level 2
jeff[28/5]@XE> SELECT COUNT(1)
2 FROM jeff_dynamic_sampling
3 WHERE TO_DATE(’15-JAN-2005′,’DD-MON-YYYY’) BETWEEN from_date AND NVL(to_date,TO_DATE(’31-DEC-9999′,’DD-MON-YYYY’));
COUNT(1)
———-
13
1 row selected.
Elapsed: 00:00:00.03
Execution Plan
———————————————————-
Plan hash value: 2444057389
——————————————————————————————–
Id Operation Name Rows Bytes Cost (%CPU) Time
——————————————————————————————–
0 SELECT STATEMENT 1 13 3 (0) 00:00:01
1 SORT AGGREGATE 1 13
You can thus make your personal email address safe and secured so viagra online australia that none can gain access to this increasingly popular treatment option. Incase nevertheless a good may be by themselves for they challenges, properly, all the internet inside constantly taking all the country a great a lot more city or possibly a assist could make one phone call clearly…as long as definitely one models some transfer, the religious aids which can be do you untouched having many of our online generic cialis personal imagination does information it so that you can be fully aware about the. tadalafil generic cialis Be careful when taking vitamin B6 or pyridoxine as a large dose can lead to neuropathy. Nicotine in tobacco is a strong vasoconstrictor, which contracts blood viagra side online vessels that further damages arteries & veins. ly:Courier New;font-size:78%;”>* 2 TABLE ACCESS FULL JEFF_DYNAMIC_SAMPLING 1 13 3 (0) 00:00:01
——————————————————————————————–
Predicate Information (identified by operation id):
—————————————————
2 – filter(“FROM_DATE”<=TO_DATE('2005-01-15 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
AND NVL(“TO_DATE”,TO_DATE(‘9999-12-31 00:00:00’, ‘yyyy-mm-dd
hh24:mi:ss’))>=TO_DATE(‘2005-01-15 00:00:00’, ‘yyyy-mm-dd hh24:mi:ss’))
Statistics
———————————————————-
1 recursive calls
0 db block gets
7 consistent gets
0 physical reads
0 redo size
411 bytes sent via SQL*Net to client
380 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
jeff[28/5]@XE>
jeff[28/5]@XE> PAUSE Press RETURN to continue…
Press RETURN to continue…
jeff[28/5]@XE>
jeff[28/5]@XE> REM Next test the same statement using the dynamic sampling level 4
jeff[28/5]@XE> SELECT /*+ dynamic_sampling(jds 4) */
2 COUNT(1)
3 FROM jeff_dynamic_sampling jds
4 WHERE TO_DATE(’15-JAN-2005′,’DD-MON-YYYY’) BETWEEN jds.from_date AND NVL(jds.to_date,TO_DATE(’31-DEC-9999′,’DD-MON-YYYY’));
COUNT(1)
———-
13
1 row selected.
Elapsed: 00:00:00.01
Execution Plan
———————————————————-
Plan hash value: 2444057389
——————————————————————————————–
Id Operation Name Rows Bytes Cost (%CPU) Time
——————————————————————————————–
0 SELECT STATEMENT 1 13 3 (0) 00:00:01
1 SORT AGGREGATE 1 13
* 2 TABLE ACCESS FULL JEFF_DYNAMIC_SAMPLING 13 169 3 (0) 00:00:01
——————————————————————————————–
Predicate Information (identified by operation id):
—————————————————
2 – filter(“JDS”.”FROM_DATE”<=TO_DATE('2005-01-15 00:00:00', 'yyyy-mm-dd
hh24:mi:ss’) AND NVL(“JDS”.”TO_DATE”,TO_DATE(‘9999-12-31 00:00:00’, ‘yyyy-mm-dd
hh24:mi:ss’))>=TO_DATE(‘2005-01-15 00:00:00’, ‘yyyy-mm-dd hh24:mi:ss’))
Note
—–
– dynamic sampling used for this statement
Statistics
———————————————————-
5 recursive calls
0 db block gets
15 consistent gets
0 physical reads
0 redo size
411 bytes sent via SQL*Net to client
380 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
jeff[28/5]@XE>
jeff[28/5]@XE> SET AUTOTRACE OFF
jeff[28/5]@XE> spool off &n
bsp;