五. Monitoring & Tuning
Let begin with major part which is knows as shared pool tuning and Oracle recommend that the default size of Shared Pool in Init.ora parameter should be ? of the Total SGA in a general scenario.
The other shared pool parameter controls how to the variable space area in the shared pool is parsed out. According to the suggestion give by Oracle if our total SGA size is 100 MB then we should set our Shared Pool size 25 MB and latter increase gradually as needed by the system.
Now a question comes in our mind what are the key area should be monitored by the DBA to get the knowledge that the shared pool size is small.
From V$SGASTAT and V$SQLAREA we will get this information.
how we can see that what is in the shared pool? Whether is being properly used or not. For that we can generate some common useful report.
First report will shown here the individual mapping of user with shared pool:
Create a temporary table to hold the information related to sql area for all users,that produces a summary report for each user.
/* Formatted on 2011/6/22 0:16:26 (QP5 v5.163.1008.3004) */
CREATE TABLE TEMP_SQL_REPORT
AS
(SELECT B.USERNAME,
A.SQL_TEXT,
A.EXECUTIONS,
A.LOADS,
A.USERS_EXECUTING,
A.SHARABLE_MEM,
A.PERSISTENT_MEM,
A.RUNTIME_MEM
FROM V$SQLAREA A, DBA_USERS B
WHERE A.PARSING_USER_ID = B.USER_ID)
查看report:
/* Formatted on 2011/6/22 0:19:04 (QP5 v5.163.1008.3004) */
SELECT Username,
SUM (SHARABLE_MEM),
SUM (PERSISTENT_MEM),
SUM (RUNTIME_MEM)
FROM temp_sql_report
GROUP BY username;

This output of this summary report shows here the sql area used by each user. Now try to analyse the summary report, if a particular hold a large amount of memory then that means the sql’s used by that user are bad every time it generate different execution plan for similar kind of queries, the coding produce a large number of nonreusable sql area.
Now we can generate an another where we will shown the actual sql statement executed by a user and how many time a sql statement execute by a user , what amount of memory that sql statement took to execute the statement.
/* Formatted on 2011/6/22 0:25:30 (QP5 v5.163.1008.3004) */
SELECT username users,
sql_text,
Executions,
loads,
users_executing,
sharable_mem,
persistent_mem
FROM temp_sql_report b
WHERE b.username LIKE UPPER ('%&user_name%')
ORDER BY 3 DESC, 1;
5.1 SQL
5.1.1 Literal SQL
A literal SQL statement is considered as one which use literals in the predicates rather then bind variable, where the value of the literal is likely to differ between various execution of the statement.
E.g 1 :
SELECT * FROM temp_x WHERE col_1='x';
is used by the application instead of
SELECT * FROM temp_x WHERE ename=:x;
Eg 2:
SELECT sysdate FROM dual;
does not use bind variables but would not be considered as a literal SQL statement for this article as it can be shared.
Eg 3:
SELECT version FROM app_version WHERE version>2.0;
If this same statement was used for checking the 'version' throughout the application then the literal value '2.0' is always the same so this statement can be considered sharable.
5.1.2 Hard Parse
If a new SQL statement is issued which does not exist in the shared pool then this has to be parsed fully. Eg: Oracle has to allocate memory for the statement from the shared pool, check the statement syntactically and semantically etc...
This is referred to as a hard parse, is very expensive in both terms of CPU used, and in the number of latch get s performed.
5.1.3 Soft Parse
If session issues a SQL statement, which is already in the shared pool AND it, can use an existing version of that statement then this is known as a 'soft parse'. As far as the application is concerned it has asked to parse the statement.
5.1.4 Identical Statements
If two SQL statements mean the same thing but are not identical character for character then from an Oracle viewpoint they are different statements. Consider the following issued by
SCOTT in a single session:
SELECT ENAME from TEMP_X;
SELECT ename from temp_x;
Although both of these statements are really the same they are not identical as an upper case 'T' is not the same as a lower case 't'.
5.1.5 Sharable SQL
If two sessions issue identical SQL statements it does NOT mean that the statement is sharable. Consider the following:
User USER_X has a table called TEMP_X and issues:
SELECT column_x from TEMP_X;
User USER_Y has his own table called TEMP_X and also issues:
SELECT column_y from TEMP_X;
Although the text of the statements are identical the TEMP_X tables are different objects. Hence these are different versions of the same basic statement. There are many things that determine if two identical SQL strings are truly the same statement (and hence can be shared) including:
All object names must resolve to the same actual objects. The optimiser goal of the sessions issuing the statement should be the same The types and lengths of any bind variables should be "similar". (We don’t discuss the details of this here but different types or lengths of bind variables can cause statements to be classed as different versions). The NLS (National Language Support) environment which applies to the statement must be the same
5.1.6 Versions of a statement
As described in 'Sharable SQL' if two statements are textually identical but cannot be shared then these are called 'versions' of the same statement.
If Oracle matches to a statement with many versions it has to check each version in turn to see if it is truly identical to the statement currently being parsed. Hence high version counts are best avoided by:
Standardising the maximum bind lengths specified by the client Avoid using identical SQL from lots of different schemas, which use private objects.
Eg:
SELECT xx FROM MYTABLE;
where eachuser has their own MYTABLE
Setting _SQLEXEC_PROGRESSION_COST to '0' in Oracle 8.1
5.1.7 Library Cache and Shared Pool latches
The shared pool latch is used to protect critical operations when allocating and freeing memory in the shared pool.
The library cache latches (and the library cache pin latch in Oracle 7.1) protect operations within the library cache itself. All of these latches are potential points of contention. The number of latch gets occurring is influenced directly by the amount activity in the shared pool, especially parse operations. Anything that can minimise the number of latch gets and indeed the amount of activity in the shared pool is helpful to both performance and scalability.
5.1.8 Literal SQL versus Shared SQL
To give a balanced picture this short section describes the benefits of both literal SQL and sharable SQL.
5.1.8.1 Literal SQL
The Cost Based Optimiser (CBO) works best when it has full statistics and when statements use literals in their predicates. Consider the following:
SELECT distinct cust_ref FROM orders WHERE total_cost < 10000.0;
Versus
SELECT distinct cust_ref FROM orders WHERE total_cost < :bindA;
For the first statement the CBO could use histogram statistics that have been gathered to decide if it would be fastest to do a full table scan of ORDERS or to use an index scan on TOTAL_COST (assuming there is one).
In the second statement CBO has no idea what percentage of rows fall below ":bindA" as it has no value for this bind variable to determine an execution plan . Eg: ":bindA" could be 0.0 or 99999999999999999.9
There could be orders of magnitude difference in the response time between the two execution paths so using the literal statement is preferable if you want CBO to work out the best execution plan for you. This is typical of Decision Support Systems where there may not be any 'standard' statements, which are issued repeatedly so the chance of sharing a statement is small. Also the amount of CPU spent on parsing is typically only a small percentage of that used to execute each statement so it is probably more important to give the optimiser as much information as possible than to minimise parse times.
