私有sql区来自UGA;共享sql区来自SGA
首先先来了解一下PGA的结构:
图为11g中PGA的结构图
可以分为三部分:Private SQL Area; Session Memory; SQL Work Areas
其中私有共享区,是存储在UGA中的,只有在非共享服务器模式下,才会储存在PGA中
private SQL area:Each session issuing a SQL statement has a private SQL area in its UGA. Each user that submits the same statement has a private SQL area pointing to the same shared SQL area. Thus, many private SQL areas in separate PGAs can be associated with the same shared SQL area.
私有sql区:每个会话发布sql时,会拥有一个sql私有区,指向共享sql区(注意是同样的语句提交时,指向)
Do not confuse a private SQL area, which is in the UGA, with the shared SQL area, which stores execution plans in the SGA. Multiple private SQL areas in the same or different sessions can point to a single execution plan in the SGA. For example, 20 executions of SELECT * FROM employees in one session and 10 executions of the same query in a different session can share the same plan. The private SQL areas for each execution are not shared and may contain different values and data.
不要把UGA中的私有sql区和SGA中的共享sql区混淆,多个私有sql区在同一个或不同会话中可以指向在SGA中的同一个执行计划,每次执行导致的私有sql区不能共享
下图是11g SGA的结构图
Shared SQL area :The database uses the shared SQL area to process the first occurrence of a SQL statement. This area is accessible to all users and contains the statement parse tree and execution plan. Only one shared SQL area exists for a unique statement.
如何判断sql是否相同,oracle会执行以下步骤:
1.检查共享池,判断在共享sql区是否存在语法和语义一样的语句
如果完全一样的存在,那么数据库使用共享sql区,来执行后续的实例中的语句
如果不存在,数据库会在共享池分配一个新的共享sql区。同样的依法,但是不同语义的会使用相应的子查询。
以上情况,用户的私有sql区都会指向包含语句和执行计划的共享sql区。
2. 在会话端,分配私有sql区
总结一下:一条sql执行后,会到共享sql区检查,是否有完全一致的sql存在,如果不存在,则在共享sql区分配新的区域。之后在会话段生成私有sql区,当该sql提交时,私有sql区会指向共享sql区(PS,我觉得还是有点问题,但是不知道哪里错了,关键指向后干了些什么)