Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

rekharaghuram
from rekharaghuram More from this publisher
05.11.2015 Views

CHAPTER 4 ■ MEMORY STRUCTURES 117 sort is done. The SORT_AREA_SIZE-SORT_AREA_RETAINED_SIZE is generally allocated out of your PGA, and the SORT_AREA_RETAINED_SIZE will be in your UGA. You can discover your current usage of PGA and UGA memory and monitor its size by querying special Oracle V$ views, also referred to as dynamic performance views. For example, let’s run a small test whereby in one session we’ll sort lots of data and, from a second session, we’ll monitor the UGA/PGA memory usage in that first session. To do this in a predicable manner, we’ll make a copy of the ALL_OBJECTS table, with about 45,000 rows in this case, without any indexes (so we know a sort has to happen): ops$tkyte@ORA10G> create table t as select * from all_objects; Table created. ops$tkyte@ORA10G> exec dbms_stats.gather_table_stats( user, 'T' ); PL/SQL procedure successfully completed. To remove any side effects from the initial hard parsing of queries, we’ll run the following script, but for now ignore its output. We’ll run the script again in a fresh session so as to see the effects on memory usage in a controlled environment. We’ll use the sort area sizes of 64KB, 1MB, and 1GB in turn: create table t as select * from all_objects; exec dbms_stats.gather_table_stats( user, 'T' ); alter session set workarea_size_policy=manual; alter session set sort_area_size = 65536; set termout off select * from t order by 1, 2, 3, 4; set termout on alter session set sort_area_size=1048576; set termout off select * from t order by 1, 2, 3, 4; set termout on alter session set sort_area_size=1073741820; set termout off select * from t order by 1, 2, 3, 4; set termout on ■Note When we process SQL in the database, we must first “parse” the SQL statement. There are two types of parses available. The first is a hard parse, which is what happens the first time a query is parsed by the database instance and includes query plan generation and optimization. The second is a soft parse, which can skip many of the steps a hard parse must do. We hard parse the previous queries so as to not measure the work performed by that operation in the following section. Now, I would suggest logging out of that SQL*Plus session and logging back in before continuing, in order to get a consistent environment, or one in which no work has been done yet.

118 CHAPTER 4 ■ MEMORY STRUCTURES To ensure we’re using manual memory management, we’ll set it specifically and specify our rather small sort area size of 64KB. Also, we’ll identify our session ID (SID) so we can monitor the memory usage for that session. ops$tkyte@ORA10G> alter session set workarea_size_policy=manual; Session altered. ops$tkyte@ORA10G> select sid from v$mystat where rownum = 1; SID ---------- 151 Now, we need to measure SID 151’s memory from a second separate session. If we used the same session, then our query to see how much memory we are using for sorting might itself influence the very numbers we are looking at. To measure the memory from this second session, we’ll use a small SQL*Plus script I developed for this. It is actually a pair of scripts. The one we want to watch that resets a small table and sets a SQL*Plus variable to the SID is called reset_stat.sql: drop table sess_stats; create table sess_stats ( name varchar2(64), value number, diff number ); variable sid number exec :sid := &1 ■Note Before using this script (or any script, for that matter), make sure you understand what the script does. This script is dropping and re-creating a table called SESS_STATS. If your schema already has such a table, you’ll probably want to use a different name! The other script is called watch_stat.sql, and for this case study, it uses the MERGE SQL statement so we can initially INSERT the statistic values for a session and then later come back and update them—without needing a separate INSERT/UPDATE script: merge into sess_stats using ( select a.name, b.value from v$statname a, v$sesstat b where a.statistic# = b.statistic# and b.sid = :sid and (a.name like '%ga %' or a.name like '%direct temp%') ) curr_stats

CHAPTER 4 ■ MEMORY STRUCTURES 117<br />

sort is done. The SORT_AREA_SIZE-SORT_AREA_RETAINED_SIZE is generally allocated out of your<br />

PGA, <strong>and</strong> the SORT_AREA_RETAINED_SIZE will be in your UGA. You can discover your current<br />

usage of PGA <strong>and</strong> UGA memory <strong>and</strong> monitor its size by querying special <strong>Oracle</strong> V$ views, also<br />

referred to as dynamic performance views.<br />

For example, let’s run a small test whereby in one session we’ll sort lots of data <strong>and</strong>, from a<br />

second session, we’ll monitor the UGA/PGA memory usage in that first session. To do this in a<br />

predicable manner, we’ll make a copy of the ALL_OBJECTS table, with about 45,000 rows in this<br />

case, without any indexes (so we know a sort has to happen):<br />

ops$tkyte@ORA10G> create table t as select * from all_objects;<br />

Table created.<br />

ops$tkyte@ORA10G> exec dbms_stats.gather_table_stats( user, 'T' );<br />

PL/SQL procedure successfully completed.<br />

To remove any side effects from the initial hard parsing of queries, we’ll run the following<br />

script, but for now ignore its output. We’ll run the script again in a fresh session so as to see<br />

the effects on memory usage in a controlled environment. We’ll use the sort area sizes of 64KB,<br />

1MB, <strong>and</strong> 1GB in turn:<br />

create table t as select * from all_objects;<br />

exec dbms_stats.gather_table_stats( user, 'T' );<br />

alter session set workarea_size_policy=manual;<br />

alter session set sort_area_size = 65536;<br />

set termout off<br />

select * from t order by 1, 2, 3, 4;<br />

set termout on<br />

alter session set sort_area_size=1048576;<br />

set termout off<br />

select * from t order by 1, 2, 3, 4;<br />

set termout on<br />

alter session set sort_area_size=1073741820;<br />

set termout off<br />

select * from t order by 1, 2, 3, 4;<br />

set termout on<br />

■Note When we process SQL in the database, we must first “parse” the SQL statement. There are two<br />

types of parses available. The first is a hard parse, which is what happens the first time a query is parsed<br />

by the database instance <strong>and</strong> includes query plan generation <strong>and</strong> optimization. The second is a soft parse,<br />

which can skip many of the steps a hard parse must do. We hard parse the previous queries so as to not<br />

measure the work performed by that operation in the following section.<br />

Now, I would suggest logging out of that SQL*Plus session <strong>and</strong> logging back in before continuing,<br />

in order to get a consistent environment, or one in which no work has been done yet.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!