Sie sind auf Seite 1von 5

SQR Tuning and Efficiency

Coding Standards

Develop all SQRS using Structured Programming


Structured Programming
A technique for organizing and coding computer programs in which a hierarch of
modules issued, each having a single entry and a single exit point, and in which control is
passed downward through the structure without unconditional branches to higher levels
of the structure.
Structured programming is often associated with a top-down approach to design. In this
way designers map out the large scale structure of a program in terms of smaller
operations, implement and test the smaller operations, and then tie them together into a
whole program.

Pseudo-Code
o Flowchart the program logic
o Research and ask a few questions:
How much data will be pulled? Will it be100 rows or 1000 rows?
How many rows will come from each table?
What kind of data will be pulled? Student Data? Setup Data?
What does the key structure look like for the tables being pulled?
Is this table the parent or the child?
How important is this table?
What fields will be needed from this table? Are they available somewhere
else?
o Write SQL and test in SQL Plus before coding SQR
Use Linear Programming easier to program and debug
Comment

Performance Considerations
Joins SQL or Procedure Calls
Limit Joins in a Select to 5 tables.
Join on the Full Key
Select from Tables not Views
More Joins = More SQR and Oracle Overhead
Use procedures or delivered sqcs to obtain data
SQC Benefits
o Speeds up execution
o Returns consistent results
o Automatically updated by PeopleSoft during Upgrade

Design: PS Table Joins


A good knowledge of the underlying application will help when joining data and selecting
criteria. This can also be accomplished by looking at table key structures in application
designer.
The SQL Distinct Parameter
Distinct will slow down the performance of the database since it forces the database to
bring back all the rows of the criteria and then look for matches, and only show one of
the matches. Sub selects can be used in some cases to avoid the distinct parameter.
Since PERSONAL_DATA will only have one row for each person, use a sub select to get
all students who are enrolled and the distinct parameter is not needed. Use IN or
EXISTS in your sub select.
New SQL:
1SelectA.Military_Status
2fromPS_PERSONAL_DATAA,
3ANDA.EMPLIDin(SelectB.EMPLID
4FROMPS_STDNT_ENRLB
5WHEREB.EMPLID=$EMPLID
6ANDB.STRM=$STRM
7ANDB.ACAD_CAREER=$ACAD_CAREER
8ANDB.INSTITUTION=$INSTITUTION
9ANDB.STDNT_ERNL_STATUS='E')

Tuning SQL
In order to best utilize the resources available, the SQL code must be efficient. This is
accomplished through design and review of performance. Tuning consists of two parts: first,
ensuring that the table joins are correct and the proper criteria is used. This is based in good
design. Optimizing the SQL statements for more efficient execution against the database is the
second tenet of tuning.
SQL Analysis
The Explain Plan can be used to resolve problems with SQL. Additional Indexes may be
required or the SQL may need to be rewritten using Hints or a different order.
In order to perform tuning, the number of Consistent Gets needs to be determined. Consistent
Gets = Number of Logical Reads. Logical Reads remain the same and are not dependent on
caching as physical reads are. Cant tune to caching, need to tune to Consistent Gets.
PS_EXT_ACAD_DATA will be used to find the EMPLID and Ext_Org_ID. EXT_ORG_TBL
will be accessed to getfields. Should this be 1 Select with a Join or 2 Selects matching on
criteria?
PS_EXT_ORG_TBL
PS_EXT_ACAD_DATA

104,484 rows
4,528,528 rows

SQL 1
SELECT A.EMPLID, A.EXT_ORG_ID
, A.EXT_CAREER
, A.EXT_DATA_NBR
, A.TERM_YEAR
, B.ATP_CD
, B.FICE_CD
FROM
PS_EXT_ACAD_DATA A,
PS_EXT_ORG_TBL B
WHERE A.EXT_ORG_ID = B.EXT_ORG_ID
AND B.EFFDT = (SELECT MAX(B1.EFFDT)
FROM PS_EXT_ORG_TBL B1
WHERE B.EXT_ORG_ID = B1.EXT_ORG_ID
AND B1.EFFDT <= sysdate)
AND A.TERM_YEAR = (SELECT MAX(A1.TERM_YEAR)
FROM PS_EXT_ACAD_DATA A1
WHERE A.EMPLID = A1.EMPLID
AND A.EXT_ORG_ID = A1.EXT_ORG_ID
AND A.EXT_CAREER = A1.EXT_CAREER)
AND a.emplid = $EMPLID
SQL trace shows 527 consistent gets
3

SQL 2
SELECT EMPLID, EXT_ORG_ID, EXT_CAREER, EXT_DATA_NBR, TERM_YEAR
FROM PS_EXT_ACAD_DATA A
WHERE A.TERM_YEAR = (SELECT MAX(A1.TERM_YEAR)
FROM PS_EXT_ACAD_DATA A1
WHERE A.EMPLID = A1.EMPLID
AND A.EXT_ORG_ID = A1.EXT_ORG_ID
AND A.EXT_CAREER = A1.EXT_CAREER)
and a.emplid = '1664461'
SELECT EXT_ORG_ID, ATP_CD, FICE_CD
FROM PS_EXT_ORG_TBL B
WHERE B.EFFDT = (SELECT MAX(B1.EFFDT)
FROM PS_EXT_ORG_TBL B1
WHERE B.EXT_ORG_ID = B1.EXT_ORG_ID
AND B1.EFFDT <= sysdate)
AND EXT_ORG_ID = '83000004711'
SQL traces the 1st select in sql2, it shows 13 consistent gets
The 2nd selection sql2 shows 7 consistent gets
For a total of 20 consistent gets.
If both sets of code accomplish the same thing, then use the 2 selects in sql2!
Command Efficiency
LET
=
4 Statements at Compile and Run Time
MOVE
=
1 Statement at Compile and Run Time
SQR Arrays
Arrays are temporay structures created in the program memory. Tables reside on disc. Use
arrays to improve program efficiency when it is necessary to process all or some records at one
time.
Previous Row Drag-Along
Problem:
A line of output contains values from the previous line
Cause:
If no rows are returned, the SQR statements in begin-select paragraph are not executed. Unless
the variable have been initialized, they may still contain data from the previous row.
Solution:
Set the variables outside of SQR select paragraph. When SQL inside the begin-select paragraph
is called the &variables get reset automatically. This happens whether or not the select statement

returns any rows. If rows are returned, the &variables are set to the values of the fields selected.
If no rows are returned, the &variables are set to null.
begin-procedure get-name
begin-select
last_name
first_name
middle_name
from PS_NAMES A
where EMPLID = $Emplid
end-select
let $last_name =& last_name
let $middle_name = &middle_name
let $first_name = &first_name

set variables here

end-procedure
ORACLE HINT IN SQR
You can also reduce the cost of the SQL and execution time by adding the Hint to the sql in SQR
SELECT /*+ ALL_ROWS */
from employess
Where ...

Das könnte Ihnen auch gefallen