Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Practice 25 - Tuning the PGA P a g e |1

Practice 25

Tuning the PGA

Practice Target
In this practice, you will perform the following:
• Obtain information about PGA performance

• Experiment normal PGA operations

• Tune PGA size using the PGA Memory Advisory

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 25 - Tuning the PGA P a g e |2

Preparing the Practice Environment


In this section of the practice, you will perform steps to prepare your environment for this practice.

1. In Oracle VirtualBox manager, take a snapshot of srv1.

Caution: Do not proceed with performing the practice without taking snapshot first.

2. Start Putty and connect to srv1 as oracle. In the rest of this practice, this session will be
referred to as admin window.
Note: Unless stated otherwise, all the steps in this practice should be run in the admin window.

Note:
In the following steps, you will create multiple script files. If you want to save your time and create
all those scripts in one go, find the attached file pga_practice_scripts.txt file. Copy/paste its
contents into the Putty window.

3. Create the following SQL*Plus script file, named as display_wa_exec.sql

The script displays the private work area statistics at the system level.
cat > display_wa_exec.sql <<EOL
set linesize 180
col NAME for a40
col VALUE for 999,999,999

SELECT NAME, VALUE


FROM V\$SYSSTAT
WHERE (NAME LIKE 'workarea executions%') OR (NAME LIKE 'sorts%');

EOL

4. Create the following script file, named as display_pgstats.sql


The script displays the memory usage statistics on PGA.
cat > display_pgstats.sql <<EOL
col NAME for a40
col VALUE for a15

SELECT NAME,
DECODE(UNIT,'bytes',TO_CHAR(ROUND(VALUE/1024/1024,2)),VALUE) VALUE,
DECODE(UNIT,'bytes','MB',UNIT) UNIT
FROM V\$PGASTAT;

EOL

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 25 - Tuning the PGA P a g e |3

5. Create the following script file, named as display_wa_histo.sql


The script retrieves workarea histogram statistics.
cat > display_wa_histo.sql <<EOL
set pagesize 20

SELECT LOW_OPTIMAL_SIZE/1024 LOW_KB,(HIGH_OPTIMAL_SIZE+1)/1024 HIGH_KB,


OPTIMAL_EXECUTIONS, ONEPASS_EXECUTIONS, MULTIPASSES_EXECUTIONS
FROM V\$SQL_WORKAREA_HISTOGRAM
WHERE TOTAL_EXECUTIONS != 0;

EOL

6. Create the following script file, named as display_active_wa.sql


The script retrieves the active workarea statistics. Once the statement finishes its execution, it
disappears from the view.
cat > display_active_wa.sql <<EOL
col OPERATION for a20
col USERNAME for a10

SELECT A.SID, V.USERNAME,


OPERATION_TYPE OPERATION,
trunc(EXPECTED_SIZE/1024) ESIZE_KB,
trunc(ACTUAL_MEM_USED/1024) MEM,
trunc(MAX_MEM_USED/1024) MAX_MEM,
NUMBER_PASSES PASS, trunc(TEMPSEG_SIZE/1024) TSIZE_KB
FROM V\$SQL_WORKAREA_ACTIVE A, V\$SESSION V
WHERE A.SID = V.SID AND V.USERNAME <>'SYS'
ORDER BY 5;

EOL

7. Create the following script file, named as display_active_wa_agg.sql


The script retrieves the active work area statistics in aggregated format.
cat > display_active_wa_agg.sql <<EOL

SELECT OPERATION_TYPE OPERATION,


NUMBER_PASSES PASS,
SUM(trunc(EXPECTED_SIZE/1024)) ESIZE_KB,
SUM(trunc(ACTUAL_MEM_USED/1024)) MEM,
SUM(trunc(MAX_MEM_USED/1024)) MAX_MEM,
SUM(trunc(TEMPSEG_SIZE/1024)) TSIZE_KB
FROM V\$SQL_WORKAREA_ACTIVE
GROUP BY OPERATION_TYPE , NUMBER_PASSES;

EOL

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 25 - Tuning the PGA P a g e |4

8. Create the following script file, named as display_wa_stats.sql


The script retrieves the work area statistics. Statistics of active statements appear in the view
after they finish their execution.
cat > display_wa_stats.sql <<EOL
col USERNAME for a9
col SQL_TEXT for a25
col OPERATION_TYPE for a20

SELECT
Q.PARSING_SCHEMA_NAME USERNAME,
SUBSTR(Q.SQL_TEXT,1,20) SQL_TEXT,
W.OPERATION_TYPE,
TRUNC(W.ESTIMATED_OPTIMAL_SIZE/1024,2) EST_OPTIMAL_SIZE,
TRUNC(LAST_MEMORY_USED/1024,2) MEM_USED,
TRUNC(MAX_TEMPSEG_SIZE/1024,2) MAX_TEMP_SIZE
FROM V\$SQL_WORKAREA W, V\$SQL Q
WHERE W.ADDRESS = Q.ADDRESS
AND Q.PARSING_SCHEMA_NAME NOT IN ('SYS','ORACLE_OCM','GSMADMIN_INTERNAL')
ORDER BY LAST_MEMORY_USED DESC
FETCH FIRST 10 ROWS ONLY;

EOL

9. Create the following script file, named as display_sess_stats.sql


The script retrieves statistics on the work areas and the sort operations performed by session(s)
which have their action set to 'my action'.

cat > display_sess_stats.sql <<EOL


col NAME for a40
col VALUE for 999,999,999,999

SELECT N.NAME, T.VALUE


FROM V\$SESSTAT T, V\$SESSION S, V\$STATNAME N
WHERE T.STATISTIC# = N.STATISTIC# AND T.SID = S.SID
AND S.ACTION='my action'
AND ((N.NAME LIKE 'workarea executions%') OR (N.NAME LIKE 'sorts%') );

EOL

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 25 - Tuning the PGA P a g e |5

Obtain Information about PGA Performance


In this section of the practice, you will obtain information about the PGA and the operations that are
related to it from the V$ views so that you can judge its health condition.

10. In the hosting PC, open a command prompt window and start Swingbench.
D:
cd D:\swingbench\winbin
set PATH=D:\oracle\product\12.1.0\client_1\jdk\jre\bin;%PATH%
swingbench.bat

11. In Swingbench, open the warehouse.xml configuration file then Start Benchmark button.

12. In the Putty window, invoke SQL*Plus and login to ORADB as sysdba.
sqlplus / as sysdba

13. Verify that the automatic PGA memory management is configured.


show parameter PGA_AGGREGATE_TARGET

14. Display the instance-level statistics on the cursor work areas and sort operations.
In a healthy system, we should not see 'workarea executions – multipass' operations.
'sorts (disk)' should not have high value in an OLTP environment.

@ display_wa_exec.sql

15. Display the PGA memory usage statistics.


Observe the following in the output:
o 'aggregate PGA auto target' is the amount of memory that Oracle database can use to
adjust the tunable area of the PGA.
o 'total PGA allocated' is the total amount of memory allocated to PGA. Memory freed from
the PGA is given back to the OS. This should be less than or equal to the preceding figure.
o 'total PGA inuse' is the total amount of memory in use by the PGA. It should be less than
the preceding figure.
o 'cache hit percentage' represents the ratio at which the input data was processed without
extra passes. In an OLTP environment, it should be close to 100.
@ display_pgstats.sql

16. Display the work area execution statistics grouped by size ranges (buckets).
Most of the executions were in two size ranges.
Note: The statistics are gathered since instance startup.
@ display_wa_histo.sql

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 25 - Tuning the PGA P a g e |6

17. Display the active work area PGA statistics for each cursor.
Observe the following from the output:
o 'Number of Passes' is zero for all the sessions. Which is a sign of a healthy PGA condition.
o No session has wanted to save anything in the temporary tablespace.
o 'V$SQL_WORKAREA_ACTIVE' displays statistics about active work areas only. When the
statement of cursor finishes its execution, it disappears from the view.
@ display_active_wa.sql

18. Display the active work area PGA statistics aggregated by the operation type.
Most of the work area sizes are needed for sort operations.
@ display_active_wa_agg.sql

19. Display the work area PGA statistics for each cursor.
V$SQL_WORKAREA view contains statistics about the cursors that finished their executions.
Note: The script displays the top 10 cursors only.
@ display_wa_stats.sql

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 25 - Tuning the PGA P a g e |7

Experimenting PGA Operations


In this section of the practice, you will perform a small experiment on the PGA to demonstrate its
functionality. You will first examine how PGA is used in normal cases. Then, you will see how the
database behaves when the sort area size is smaller than the processed data.

20. Start another Putty session and connect to srv1 as oracle. Invoke SQL*Plus and login to ORADB
as soe. In the rest of this practice, this session will be referred to as the client window.

sqlplus soe/soe

21. In the client window, set an action to its session.


exec DBMS_APPLICATION_INFO.SET_ACTION ('my action')

22. In the client window, run the following query. Use a hint to distinguish the query from the other
statements submitted to the database.
The query retrieves and orders only 100 rows.
set linesize 180
SELECT /*+ SQLID: 1 */
ORDER_ID, ORDER_TOTAL
FROM ORDERS
WHERE ORDER_ID BETWEEN 10000 AND 10100
ORDER BY 2;

23. In the admin window, display the work area statistics on the executed query.
All the workarea executions were optimal, no sorts in the disk.
@ display_sess_stats.sql

24. In the client window, run the following query. The query retrieves and orders 39000 rows.
SELECT /*+ SQLID: 2 */
ORDER_ID, ORDER_TOTAL
FROM ORDERS
WHERE ORDER_ID BETWEEN 10000 AND 400000
ORDER BY 2;

25. In the admin window, display the work area statistics on the executed query.
All the work area executions were optimal, no sorts in the disk.
/

26. Display the work area statistics.


You should see the query in the top of the work areas list. Oracle database estimated that a large
amount of the work area was needed because of the high number of retrieved rows.
@ display_wa_stats.sql

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 25 - Tuning the PGA P a g e |8

27. In the client window, reduce the sort area size of the session to a small size (160204 bytes for
example).
To make the changes of the work area sort size take effect, we must set the
WORKAREA_SIZE_POLICY to MANUAL.

ALTER SESSION SET WORKAREA_SIZE_POLICY=MANUAL;


ALTER SESSION SET SORT_AREA_SIZE=160204;

28. In the client window, run the following query. The query retrieves the same rows retrieved by
the last query. Only the hint is different.
SELECT /*+ SQLID: 3 */
ORDER_ID, ORDER_TOTAL
FROM ORDERS
WHERE ORDER_ID BETWEEN 10000 AND 400000
ORDER BY 2;

29. In the admin window, display the work area statistics on the executed query.
'sorts (disk)' is incremented.
@ display_sess_stats.sql

30. Display the work area statistics at the instance level.


Observe the following from the output:
o The database estimated a large amount of memory was needed, but allocated much smaller
amount because we reduced the sort area size for the session.
o Temporary space was needed and consumed. Part of the sorting process happened in the
disk. This happens because of the shortage in the available work area memory.
@ display_wa_stats.sql

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 25 - Tuning the PGA P a g e |9

Tuning PGA Size Using the PGA Memory Advisory


In this section of the practice, you will mimic shortage on PGA memory size and use the PGA Memory
Advisory to tune the PGA.

31. Stop the Swingbench sessions.

32. Change the value of the PGA_AGGREGATE_TARGET to 50 MB.

This parameter is a dynamic parameter.


ALTER SYSTEM SET PGA_AGGREGATE_TARGET=50M;

33. Start Swingbench sessions.

34. Wait for a couple of minutes.

35. Display the PGA and the work area statistics.


Observe the following in the output:
o PGA is efficient (not multipass, no sorting on disk).
o Allocated PGA size is way more than the PGA_AGGREGATE_TARGET value. Oracle database does
not strictly follow the value of the PGA_AGGREGATE_TARGET. If the value of
PGA_AGGREGATE_TARGET is less than the required PGA size, the database may allocate much
larger PGA size than the value assigned to the parameter.
@ display_wa_exec.sql
@ display_pgstats.sql

36. Run the following query to obtain details from the PGA memory advisory. Determine from the
output the optimal PGA size.

Note: AWR reports show PGA memory advisory data as well.

SELECT ROUND(PGA_TARGET_FOR_ESTIMATE/1024/1024) TARGET_MB,


ESTD_PGA_CACHE_HIT_PERCENTAGE CACHE_HIT_PERC,
ESTD_OVERALLOC_COUNT
FROM V$PGA_TARGET_ADVICE;

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 25 - Tuning the PGA P a g e | 10

Clean up
37. Stop the Swingbench sessions.

38. Exit from Putty sessions

39. Shutdown srv1 machine

40. Restore srv1 from the snapshot taken in the beginning of the practice

41. Delete the snapshot

42. (optional) Delete the generated AWR report files from the staging folder

43. Restart srv1

Oracle Database Performance Tuning, a course by Ahmed Baraka


Practice 25 - Tuning the PGA P a g e | 11

Summary

• The following rules can be used to judge the PGA health:

o In a healthy system, we should not see 'workarea executions – multipass' operations.

o 'sorts (disk)' should not have high value in an OLTP environment.

o In an OLTP environment, 'cache hit percentage' should be close to 100.

• V$SQL_WORKAREA_ACTIVE shows statistics about active work areas.

• V$SQL_WORKAREA shows statistics about work areas of SQL cursors that finished their executions.

• Oracle database may allocate more size for the PGA than the size set by the
PGA_AGGREGATE_TARGET, if need be.

• PGA Memory Advisory advises about the optimal PGA size

Oracle Database Performance Tuning, a course by Ahmed Baraka

You might also like