唯一允許您在不多次讀取表格的情況下執行此操作的構造是partitioned outer join。
這是您的數據集的例子:
SQL> create table available_templates (template_group,template_name,locale)
2 as
3 select 'RO', 'LTRU', 'fi_FI' from dual union all
4 select 'RO', 'LTRU', 'se_SE' from dual union all
5 select 'RO', 'LTRU', 'en_US' from dual union all
6 select 'BL', 'V1PRO', 'se_SE' from dual union all
7 select 'BL', 'V1PRO', 'en_US' from dual
8/
Table created.
SQL> create table system_locales (sys_locale)
2 as
3 select 'lv_LV' from dual union all
4 select 'fi_FI' from dual union all
5 select 'se_SE' from dual union all
6 select 'en_US' from dual
7/
Table created.
而且分區外部聯接:
SQL> select at.template_group
2 , at.template_name
3 , sl.sys_locale
4 , nvl
5 (at.locale
6 , max(decode(at.locale,'fi_FI',at.locale)) over (partition by at.template_group, at.template_name)
7 ) use_locale
8 from system_locales sl
9 left outer join available_templates at
10 partition by (at.template_group,at.template_name)
11 on (at.locale = sl.sys_locale)
12/
TEMPLATE_GROUP TEMPLATE_NAME SYS_LOCALE USE_LOCALE
-------------- ------------- ---------- ----------
BL V1PRO en_US en_US
BL V1PRO fi_FI
BL V1PRO lv_LV
BL V1PRO se_SE se_SE
RO LTRU en_US en_US
RO LTRU fi_FI fi_FI
RO LTRU lv_LV fi_FI
RO LTRU se_SE se_SE
8 rows selected.
這裏是證明表只掃描一次:
SQL> select * from table(dbms_xplan.display_cursor(null,null,'allstats last'))
2/
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------------------------------------------------------------
SQL_ID 57br33gc6n1sc, child number 0
-------------------------------------
select at.template_group , at.template_name , sl.sys_locale
, nvl (at.locale ,
max(decode(at.locale,'fi_FI',at.locale)) over (partition by
at.template_group, at.template_name) ) use_locale from
system_locales sl left outer join available_templates at
partition by (at.template_group,at.template_name) on
(at.locale = sl.sys_locale)
Plan hash value: 921719364
-----------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | OMem | 1Mem | Used-Mem |
-----------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 8 |00:00:00.01 | 6 | | | |
| 1 | WINDOW BUFFER | | 1 | 1 | 8 |00:00:00.01 | 6 | 2048 | 2048 | 2048 (0)|
| 2 | VIEW | | 1 | 1 | 8 |00:00:00.01 | 6 | | | |
| 3 | MERGE JOIN PARTITION OUTER| | 1 | 1 | 8 |00:00:00.01 | 6 | | | |
| 4 | SORT JOIN | | 3 | 4 | 9 |00:00:00.01 | 3 | 2048 | 2048 | 2048 (0)|
| 5 | TABLE ACCESS FULL | SYSTEM_LOCALES | 1 | 4 | 4 |00:00:00.01 | 3 | | | |
|* 6 | SORT PARTITION JOIN | | 9 | 5 | 5 |00:00:00.01 | 3 | 2048 | 2048 | 2048 (0)|
| 7 | TABLE ACCESS FULL | AVAILABLE_TEMPLATES | 1 | 5 | 5 |00:00:00.01 | 3 | | | |
-----------------------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
6 - access("AT"."LOCALE"="SL"."SYS_LOCALE")
filter("AT"."LOCALE"="SL"."SYS_LOCALE")
Note
-----
- dynamic sampling used for this statement (level=2)
35 rows selected.
Regards,
Rob。
對於給定的「TEMPLATE_GROUP」(或反之亦然),您是否可以有多個「TEMPLATE_NAME」值?或者這兩個值之間是否存在一對一的對應關係?另外,您是否有模板/模板組值的單獨表格(除AVAILABLE_TEMPLATES以外)? – 2013-03-04 13:46:07