So I set the autotrace on
ramin.tb001 had 10 rows.
In 1 version I use equality and in second less than
I want to know is the When condions reads 10 rows from ramin.tb001 and check condition or how?
So I set the autotrace on
ramin.tb001 had 10 rows.
In 1 version I use equality and in second less than
I want to know is the When condions reads 10 rows from ramin.tb001 and check condition or how?
I want to know is the When condions reads 10 rows from ramin.tb001 and check condition or how?
Your first insert statement applies the condition when id = 6
and the second applies the condition when id < 10
. In both cases the query reads the whole table and filters the result set by evaluating each row. How else could the query be processed?
Well, if there were a unique index on ramin.tb001.id
the optimiser would probably choose to use that instead. As only ID is selected in each case, the entire query can be satisfied from the index, which would be more efficient than scanning the table. This is true even if ramin.tb001
has just the one column, because ID would be sorted in the index, but maybe not in the table. Although given that the source data has such a trivial amount of data the difference in actual performance would be negligible.
I do not thing that Oracle account for your special case with only one WHEN
insert clause and implemented the optimizer path using index (test also shows always FULL TABLE SCAN
s).
The reason is that the general case deployes more insert clauses such as follows:
insert all
when id < 3 then into tb002 values(id)
when id >= 3 then into tb003 values(id)
select id from tb001;
What is Oracle doing is visible in the excecution plan
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | INSERT STATEMENT | | 10 | 130 | 3 (0)| 00:00:01 |
| 1 | MULTI-TABLE INSERT | | | | | |
| 2 | TABLE ACCESS FULL | TB001 | 10 | 130 | 3 (0)| 00:00:01 |
| 3 | INTO | TB002 | | | | |
| 4 | INTO | TB003 | | | | |
-----------------------------------------------------------------------------
You see the FULL SCAN
of the source table in the line 2, i.e. all rows are considered; for each row all the INTO
insert clauses are evalueted and if TRUE the row is inserted.
If you want to implement a ELSE
clause - you must use INSERT FIRST
insert first
when id < 3 then into tb002 values(id)
else into tb003 values(id)
select id from tb001
from the Oracle documentation
If you specify FIRST, then the database evaluates each WHEN clause in the order in which it appears in the statement. For the first WHEN clause that evaluates to true, the database executes the corresponding INTO clause and skips subsequent WHEN clauses for the given row.