在Oracle的管理员手册上,说明可以为表、视图、物化视图、过程、函数、包、对象和序列建立同义词。虽然Oracle并没有明确说明,但是同样可以为同义词建立同义词。
有些时候建立指向同义词的同义词是有意义的。由于同义词本身功能之一就是实现物理结构和物理位置的透明性,因此用户在建立同义词的时候并不会太关系当前指向的对象是否是同义词还是其他对象。
Oracle处理指向同义词的同义词只是在解析对象的时候存在很小的代价,其他并没有太多的影响。
当然如果同义词嵌套的层数过多,也不是很好的设计方式,用户在确定要访问真正的对象是会很麻烦,Oracle在分析对象的时候同样也要花费时间:
SQL> create synonym s1 for t;
同义词已创建。
SQL> create synonym s2 for s1;
同义词已创建。
对于同义词S2来说,就是一个嵌套同义词,指向了同义词S1。如果这种嵌套的层数太多,也会影响分析的性能。比如,一个嵌套了10000层的同义词:
SQL> begin
2 for i in 2..10000 loop
3 execute immediate 'create synonym s' || (i+1) || ' for s' || i;
4 end loop;
5 end;
6 /
PL/SQL过程已成功完成。
SQL> set timing on
SQL> set autot on
SQL> select * from t;
未选定行
已用时间: 00: 00: 00.02
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT ptimizer=CHOOSE
1 0 TABLE ACCESS (FULL) OF 'T'
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
267 bytes sent via SQL*Net to client
460 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
SQL> select * from s1;
未选定行
已用时间: 00: 00: 00.02
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT ptimizer=CHOOSE
1 0 TABLE ACCESS (FULL) OF 'T'
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
267 bytes sent via SQL*Net to client
460 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
SQL> select * from s2;
未选定行
已用时间: 00: 00: 00.02
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT ptimizer=CHOOSE
1 0 TABLE ACCESS (FULL) OF 'T'
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
267 bytes sent via SQL*Net to client
460 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
SQL> select * from s100;
未选定行
已用时间: 00: 00: 00.03
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT ptimizer=CHOOSE
1 0 TABLE ACCESS (FULL) OF 'T'
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
267 bytes sent via SQL*Net to client
460 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
SQL> select * from s1000;
未选定行
已用时间: 00: 00: 00.11
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT ptimizer=CHOOSE
1 0 TABLE ACCESS (FULL) OF 'T'
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
267 bytes sent via SQL*Net to client
460 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
SQL> select * from s10000;
未选定行
已用时间: 00: 00: 01.20
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT ptimizer=CHOOSE
1 0 TABLE ACCESS (FULL) OF 'T'
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
267 bytes sent via SQL*Net to client
460 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
SQL> select * from s10000;
未选定行
已用时间: 00: 00: 00.01
Execution Plan
----------------------------------------------------------
0 SELECT STATEMENT ptimizer=CHOOSE
1 0 TABLE ACCESS (FULL) OF 'T'
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
267 bytes sent via SQL*Net to client
460 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
可以看到,同义词嵌套层数越多,Oracle就要花费越多的分析的时间来确定要访问的对象。当然这个分析的结果是保存在共享池内的,下次再次运行相同的查询,就不需要重新分析了。
oracle视频教程请关注:http://u.youku.com/user_video/id_UMzAzMjkxMjE2.html