All technological notes.
Program Global Area (PGA)
DBA can use an initialization parameter to set a target maximum size of the instance PGA.
PGA_aggregate_limit: a limit on the aggregate PGA memory consumed by the instance. (hard limit)PGA_aggregate_target: the target aggregate PGA memory available to all server processes attached to the instance.instance PGA

show parameter pga_aggregate_limit
show parameter PGA_aggregate_target
select value from v$parameter where name = 'pga_aggregate_limit';
select value from v$parameter where name = 'PGA_aggregate_target';
ALTER SYSTEM SET pga_aggregate_limit = 2G SCOPE=both;
ALTER SYSTEM SET PGA_AGGREGATE_TARGET = 1024M SCOPE=both;
# SCOPE=both part ensures that the change takes effect immediately and is persistent across both the current session and future sessions.
PGA Contents
Private SQL Area
SQL Work Area
Session Memory

dedicated or shared server connections.| Memory Area | Dedicated Server | Shared Server |
|---|---|---|
| Nature of session memory | Private | Shared |
persistent area |
PGA | SGA |
run-time area for DML and DDL statements |
PGA | PGA |
Dedicated mode:
bind vairable and session are not shared
Session is private.persistent area in PGA.Run-time area always in PGA.
V$PGASTAT - PGA statisticsV$PGASTAT
select name, value from v$pgastat;

Private SQL Area - CursorPrivate SQL Area
When a server process executes SQL or PL/SQL code, the process uses the private SQL area to store bind variable values, query execution state information, and query execution work areas.
Private SQL Area vs shared SQL areaPrivate SQL Area |
shared SQL area |
|
|---|---|---|
| Memory Structure | PGA | SGA |
| Store Info | a parsed SQL statement + session-specific | stores execution plans |
private SQL areas in the same or different sessions can point to a single execution plan in the SGA.private SQL areas for each execution are not shared and may contain different values and data.run-time arearun-time area
One area in private SQL area
contains query execution state information.
the first step of an execute request = the creation of run-time area.
persistent areapersistent area
bind variable values.bind variable value
cursor
private SQL area.cursor
the terms are sometimes used interchangeably:
cursor = private SQL areas 
The client process is responsible for managing private SQL areas.
private SQL areas depends largely on the applicationIn general, applications should close all open cursors that will not be used again to free the persistent area and to minimize the memory required for application users.
initialization parameter: OPEN_CURSORS
SHOW PARAMETER OPEN_CURSORS;
SELECT value
FROM v$parameter
WHERE name = 'open_cursors';
ALTER SYSTEM SET OPEN_CURSORS = 500 SCOPE=both;

Private SQL area & CursorDECLARE
CURSOR c_employee (dept_id NUMBER) IS
SELECT employee_name
FROM employees
WHERE department_id = dept_id;
v_employee_name VARCHAR2(50);
v_department_id NUMBER := 10; -- Bind Variable
BEGIN
OPEN c_employee(v_department_id); -- Bind variable value supplied at runtime
LOOP
FETCH c_employee INTO v_employee_name; -- Interacts with private SQL area (run-time area)
EXIT WHEN c_employee%NOTFOUND;
-- Process the current row (v_employee_name)
DBMS_OUTPUT.PUT_LINE('Employee Name: ' || v_employee_name);
END LOOP;
CLOSE c_employee; -- Deallocates private SQL area (run-time area) and persistent area
END;
Execution Details:
Parse Phase:
CURSOR c_employee.Compile Phase:
dept_id.Bind Variable:
v_department_id is a bind variable initialized with the value 10.bind variable allows for dynamic customization of the SQL statement.Open Cursor:
OPEN c_employee(v_department_id); statement is executed.private SQL area (run-time area) is allocated at runtime to manage the SQL statement execution.bind variable value (10) is stored in the persistent area.Fetch Loop:
LOOP begins, and the FETCH c_employee INTO v_employee_name; statement interacts with the private SQL area (run-time area).runtime area to retrieve rows dynamically.Exit Loop:
EXIT WHEN c_employee%NOTFOUND; is met.Close Cursor:
CLOSE c_employee; statement is executed.private SQL area (run-time area) and the associated persistent area are deallocated, freeing resources.SQL Work AreasSQL Work Areas
sort area to sort a set of rows.hash area to build a hash table from its left inputbitmap merge area to merge data retrieved from scans of multiple bitmap indexes.When automatic PGA memory management is enabled, DB automatically tunes work area sizes
Performance:
work areas can significantly improve performance of an operator at the cost of higher memory consumption. 一般, 大 size 提高性能.work area is sufficient to accommodate the input data and auxiliary memory structures allocated by its associated SQL operator. 最优, 大小充分Parameter:
| Parameter | Desc |
|---|---|
SORT_AREA_SIZE |
size of the memory allocated for sorting operation |
HASH_AREA_SIZE |
size of the memory allocated for hashing operation |
# HASH_AREA_SIZE
SHOW PARAMETER HASH_AREA_SIZE;
SELECT value
FROM v$parameter
WHERE name = 'hash_area_size';
ALTER SYSTEM SET hash_area_size = <new_size> SCOPE=both;
SHOW PARAMETER SORT_AREA_SIZE;
SELECT value
FROM v$parameter
WHERE name = 'sort_area_size';
ALTER SYSTEM SET sort_area_size = <new_size> SCOPE=both;
SELECT *
FROM employees e JOIN departments d
ON e.department_id=d.department_id
ORDER BY last_name;
/* query plan */
--------------------------------------------------------------------------------
#| Id| Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
#| 0 | SELECT STATEMENT | | 106 | 9328 | 7 (29)| 00:00:01 |
#| 1 | SORT ORDER BY | | 106 | 9328 | 7 (29)| 00:00:01 |
#|*2 | HASH JOIN | | 106 | 9328 | 6 (17)| 00:00:01 |
#| 3 | TABLE ACCESS FULL| DEPARTMENTS | 27 | 540 | 2 (0)| 00:00:01 |
#| 4 | TABLE ACCESS FULL| EMPLOYEES | 107 | 7276 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------------
run-time area tracks the progress of the full table scans.hash area to match rows from the two tables.ORDER BY sort occurs in the sort area.