假设我有下表:
create table Employee
(
ID VARCHAR2(4 BYTE) NOT NULL,
First_Name VARCHAR2(10 BYTE),
Last_Name VARCHAR2(10 BYTE),
Start_Date DATE,
End_Date DATE,
Salary Number(8,2),
City VARCHAR2(10 BYTE),
Description VARCHAR2(15 BYTE)
)
;
insert into employee (
ID, First_Name, Last_Name, Start_Date,
End_Date, Salary, City, Description)
select 21 + (level * 2) - 1,'Jason', 'Martin', to_date('19960725','YYYYMMDD'),
to_date ('20060725','YYYYMMDD'), 1234.56+level/3*5, 'Toronto', 'Programmer'
from dual
connect by level < 2501
;
当我尝试运行以下查询时:
with tt2 as (
select t.*,
case when rownum = 1 then 1 when rownum = 3 then 3 else null end rfn
-- RFN 是一个复杂情况的例子,它只为需要的行分配数字。请不要修复它
from (select id, first_name, salary from employee) t
)
select
(select id from tt2 where rfn = 3) w
from dual
它返回给我所有类似于 rfn = 3 的 ID(2500 行,共 26 行)
这是解释计划
"PLAN_TABLE_OUTPUT"
"Plan hash value: 2716438026"
" "
"--------------------------------------------------------------------------------------------------------"
"| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |"
"--------------------------------------------------------------------------------------------------------"
"| 0 | SELECT STATEMENT | | 2500 | | 12 (0)| 00:00:01 |"
"|* 1 | VIEW | | 2500 | 42500 | 4 (0)| 00:00:01 |"
"| 2 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6645_8CDDE2 | 2500 | 60000 | 4 (0)| 00:00:01 |"
"| 3 | TEMP TABLE TRANSFORMATION | | | | | |"
"| 4 | LOAD AS SELECT | SYS_TEMP_0FD9D6645_8CDDE2 | | | | |"
"| 5 | COUNT | | | | | |"
"| 6 | TABLE ACCESS FULL | EMPLOYEE | 2500 | 60000 | 8 (0)| 00:00:01 |"
"| 7 | VIEW | | 2500 | | 4 (0)| 00:00:01 |"
"| 8 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6645_8CDDE2 | 2500 | 60000 | 4 (0)| 00:00:01 |"
"--------------------------------------------------------------------------------------------------------"
" "
"Predicate Information (identified by operation id):"
"---------------------------------------------------"
" "
" 1 - filter(""RFN""=3)"
" "
"Note"
"-----"
" - dynamic sampling used for this statement (level=2)"
- 编辑 -
例如,我在 WITH 中有表:
"ID" "FIRST_NAME" "SALARY" "RFN" “22”“杰森”1236.23 1 “24”“杰森”1237.89 “26”“杰森”1239.56 3
我想选择 RFN = 1 的任何人的薪水,给我一个号码 1236.23
但我也想在同一个查询中选择 RFN = 3 的薪水,给我一个号码 1239.56。
等等。
但。对于此查询中的每一个,它都会生成 N 行的 FULL_TABLE_SCAN。
with tt2 as (
select t.*, case when rownum = 1 then 1 when rownum = 3 then 3 else null end rfn
from (select id, first_name, salary from employee) t
)
select
(select salary from tt2 where rfn = 1) w,
(select salary from tt2 where rfn = 3) w
from dual
解释表:
('Plan hash value: 1823688231');
(' ');
('--------------------------------------------------------------------------------------------------------');
('| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |');
('--------------------------------------------------------------------------------------------------------');
('| 0 | SELECT STATEMENT | | 2500 | | 12 (0)| 00:00:01 |');
('|* 1 | VIEW | | 2500 | 40000 | 4 (0)| 00:00:01 |');
('| 2 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6651_8CDDE2 | 2500 | 60000 | 4 (0)| 00:00:01 |');
('|* 3 | VIEW | | 2500 | 40000 | 4 (0)| 00:00:01 |');
('| 4 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6651_8CDDE2 | 2500 | 60000 | 4 (0)| 00:00:01 |');
('| 5 | TEMP TABLE TRANSFORMATION | | | | | |');
('| 6 | LOAD AS SELECT | SYS_TEMP_0FD9D6651_8CDDE2 | | | | |');
('| 7 | COUNT | | | | | |');
('| 8 | TABLE ACCESS FULL | EMPLOYEE | 2500 | 60000 | 8 (0)| 00:00:01 |');
('| 9 | VIEW | | 2500 | | 4 (0)| 00:00:01 |');
('| 10 | TABLE ACCESS FULL | SYS_TEMP_0FD9D6651_8CDDE2 | 2500 | 60000 | 4 (0)| 00:00:01 |');
('--------------------------------------------------------------------------------------------------------');
(' ');
('Predicate Information (identified by operation id):');
('---------------------------------------------------');
(' ');
(' 1 - filter("RFN"=1)');
(' 3 - filter("RFN"=3)');
(' ');
('Note');
('-----');
(' - dynamic sampling used for this statement (level=2)');
——为更清楚而编辑——
预期结果来自:
"ID" "FIRST_NAME" "SALARY" "RFN" “22”“杰森”1236.23 1 “24”“杰森”1237.89 “26”“杰森”1239.56 3 “26”“杰克”1249.56 6 “26”“维基”119.56 8
工资 工资 1 工资 2 姓名 姓名 2 1236.23 1249.56 119.56 维基杰克
来自〜以下声明:
with tt2 as (
select t.*, case when row_number() over (order by id) = 1 then 1 when row_number() over (order by id) = 3 then 3 else null end rfn
from (select id, first_name, salary from employee) t
)
select
(select salary from tt2 where rfn = 1) salary,
(select salary from tt2 where rfn = 6) salary1,
(select salary from tt2 where rfn = 8) salary2,
(select first_name from tt2 where rfn = 8) name,
(select first_name from tt2 where rfn = 6) name2
from dual
没有 2500 行的 5 次全表扫描