Structured Query Language (SQL) Tuning

Tuning SQL is one of those skills, which is part art and part science.  However, there are a few fundamental approaches, which can help ensure optimal SQL select statement performance.

Structuring your SQL

By Structuring SQL Statements, much performance can be gained through good SQL statement organization and sound logic.

Where Clause Concepts:

Use criteria ordering and Set Theory thinking.  SQL  can be coupled with set-theory to aid conception of the operations being conducted. Order your selection criteria to execute criteria which arrives at the smallest possible row set first. Doing so reduces the volume of rows to be processed by follow-on operations. This does require an understanding of the data relationships to be effective.

Join Rules (equijoins, etc.)

When  constructing your joins, consider these rules:

  • Join on keys and indexed columns: The efficiency of your program improves when tables are joined based on indexed columns, rather than on non-indexed ones.
  • Use equijoins (=), whenever possible
  • Avoid using of sub-queries
  • Re-write EXISTS and NOT EXISTS subqueries as outer joins
  • Avoid OUTER Joins on fields containing nulls
  • Avoid RIGHT OUTER JOINS: Always select FROM your primary table (or derived table) and LEFT OUTER JOIN to auxiliary tables.
  • Use Joins Instead of Subqueries: A join can be more efficient than a correlated subquery or a subquery using IN. Use caution when specifying ORDER BY with a Join: When the results of a join must be sorted, limiting the ORDER BY to columns of a single table can cause the database to avoid a sort.
  • Provide Adequate Search Criteria: When possible, provide additional search criteria in the WHERE clause for every table in a join. These criteria are in addition to the join criteria, which are mandatory to avoid Cartesian products

Order of Operations SQL & “PEMDAS”

To improve your SQL, careful attention needs to be paid to the mathematical order of operations; especially, parentheses since they not only set the order of operation but also the boundaries of each subset operation.

  • PEMAS is “Parentheses, Exponents, Multiplication and Division, and Addition and Subtraction”.
  • Use parentheses () to group and specify the order of execution. SQL observes the normal rules of arithmetic operator precedence.
PrecedenceOperator(s)Operation(s)Notes
1( )ParenthesesIf the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several un-nested parentheses, then parentheses are evaluated left to right.
2*
/
%
Multiplication
Division
Modulus
If there are several, evaluation is left to right.
3+
Addition
Subtraction
If there are several, evaluation is left to right.

Index Leveraging (criteria ordering, hints, append, etc.)

  • Avoid Full Table Scans: within the scope of a SQL statement, there are many conditions that will cause the SQL optimizer to invoke a full-table scan.  Avoid Queries:
  • with NULL Conditions (Is NUll, Is Not NUll)
  • Against Unindexed Columns
  • with Like Conditions
  • with Not Equals Condition (<>, !=, not in)
  • with use built-in Function (to_char, substr, decode, UPPER)
  • Use UNION ALL instead of UNION if business rules allow
  • UNION: Specifies that multiple result sets are to be combined and returned as a single result set. Query optimizer performs extra work to return to avoid duplicate rows.
  • UNION ALL: Incorporates all rows into the results. This includes duplicates. Query optimizer just needs to concatenate the result sets with no extra work
  • Use stored procedures instead of ad hoc queries when possible. Stored procedures are precompiled and cached
  • Avoid cursor use when possible
  • Select only the rows needed
  • Use NOLOCK hint in the select statement to avoid blocking
  • Commit transactions in smaller batches
  • Whenever possible use tables instead of views
  • Make sure comparison columns whether using JOIN or WHERE clause are exactly the same data type. For example, if we are comparing Varchar column to nchar columns the query optimizer has to do a CONVERT before comparing the values

Note: You do not necessarily need to remove all full table scans from your query’s execution plan. Tables with few rows, few columns, or thin columns may fit into few database blocks. In this case, a full table scan will always be the most efficient access

Oracle SQL Date Formatting Examples

Below is a SQL of various examples of Oracle date formats, which I have found to be a useful reference.

Select

SYSDATE,

TO_CHAR(SYSDATE,’YYYY-MM-DD AD’) AS ANNO_DOMINI,

TO_CHAR(SYSDATE,’YYYYMMDD’) AS SORT_PATTERN1,

TO_CHAR(SYSDATE,’YYYY/MM/DD’) AS DATE_PATTERN1,

TO_CHAR(SYSDATE,’DD/MM/YYYY’) AS DATE_PATTERN2,

TO_CHAR(SYSDATE,’DD/MON/YYYY’) AS DATE_PATTERN3,

TO_CHAR(SYSDATE,’DD-MON-YY’) AS DATE_PATTERN4,

TO_CHAR(SYSDATE,’CC’) AS CENTURY,

TO_CHAR(SYSDATE,’YYYY’) AS FOUR_DIGIT_YEAR,

TO_CHAR(SYSDATE,’YY’) AS YEAR_OF_CENTURY,

TO_CHAR(SYSDATE,’J’) AS JUALIAN_DATE,

TO_CHAR(SYSDATE,’Q’) AS CALENDAR_QUARTER_OF_YEAR,

TO_CHAR(SYSDATE,’WW’) AS CALENDAR_WEEK_OF_YEAR,

TO_CHAR(SYSDATE,’DDD’) AS NUMBER_OF_CALENDAR_DAY_OF_YEAR,

TO_CHAR(SYSDATE,’W’) AS WEEK_OF_MONTH,

TO_CHAR(SYSDATE,’DAY’) AS NAME_OF_DAY_OF_WEEK,

TO_CHAR(SYSDATE,’DY’) AS ABBREVIATED_DAY_OF_WEEK,

TO_CHAR(SYSDATE,’MM’) AS NUMERIC_MONTH_OF_YEAR,

TO_CHAR(SYSDATE,’MON’) AS ABBREVIATED_MONTH_OF_YEAR,

TO_CHAR(SYSDATE,’MONTH’) AS SPELLED_OUT_MONTH_OF_YEAR

FROM DUAL;

Related References

PS Query Timestamp to Date Logic

Converting a timestamp to date in PeopleSoft Query seems to be a common item, which I have helped others with, to the point that I kept a personal note on the coding logic.  So, I thought I would post this quick note in case it may be useful to someone.  This item would seem to be straight forward, however, PS query tries to help you with the process, so, some experimentation is usually involved to get the code to out just right.

Here is a sample expression I use for the timestamp to date conversion in PS Query.

  • Create an expression with an Expression Type of “Number
  • Set Length to 10
  • Sample code: TO_DATE(SUBSTR(<<RecordAlias.TimestampFieldName>>,1,10))
  • PS Query interpretation: TO_DATE(SUBSTR(TO_CHAR(CAST((<<RecordAlias.TimestampFieldName>>) AS TIMESTAMP),’YYYY-MM-DD-HH24.MI.SS.FF’),1,10))

Related References

PS Query Migration Validation SQLs

While PeopleSoft PeopleTools Application Designer is the best way to validate query migrations, sometimes a few SQL scripts can be helpful when verifying that queries and associated tree exist in an environment with the necessary nodes.  This SQL can be run in the appropriate SQL editor and was written against an Oracle RDBMS.

Is The Query in the Environment?

SELECT DISTINCT QRYNAME

FROM PSQRYFIELD

WHERE QRYNAME LIKE ’<<QUERYNAME>>%’;

Is Query Tree in Environment?

SELECT TREE_NAME

FROM PSTREENODE

WHERE TREE_NAME =’<<TREENAME>>’

ORDER BY 1;

Is Record in Query Security Tree?

SELECT TREE_NODE

FROM PSTREENODE

WHERE TREE_NAME =’<<TREENAME>>’

AND TREE_NODE LIKE ’%<<RecordName>>%’;

Related References

How to Verify PeopleSoft Record Build in Oracle

A frequent problem during migrations of PeopleSoft record definitions is the omission of a record build, even though the record exists in the PeopleTools project Meta Data.  An easily way to solve this issue is to run a SQL script to test the new record exist and/or that altered record field(s) exist.  A simple count script can provide the necessary answers and save lost project time and reduce testing defect research and resolution activities up front.  Here a couple of quick samples, which can be assembled into a script and run in Oracle SQL Developer as a script.

Verify that a new PeopleSoft record exists in Oracle

SELECT COUNT(*) AS CNT_<<RecordName>>

FROM PS_<<RecordName>>;

Verify that a new field exists on an existing PeopleSoft record in Oracle

SELECT COUNT(<<FieldName>>) As CNT_<<RecordName_FieldName>>

FROM PS_<<RecordName>>;

Note: This SQL can also, be used to see if a field was dropped, only it should produce an error for the removed field.

Related References

How to Add User Identification to a PeopleSoft Query?

In PeopleSoft (PS) query, it is possible to add the user name of the person who ran the query as a column.  To do so create an expression, substring the %Operator global variable, and use the expression as a field.

Example PeopleSoft Query Expression for UserID

SUBSTRING(%Operator,1,30)

PeopleSoft XLAT Value Lookup SQL

I’ve had this SQL snippet laying around for a while, so, before I lose it, I thought I would post it for future reference.  Below is SQL to lookup a XLAT values, in case you don’t want to or cannot go through PeopleSoft Application Designer.

XLAT Lookup SQL

SELECT XLAT.FIELDNAME,

XLATFIELDVALUE,

XLAT.XLATLONGNAME

FROM PSXLATITEM XLAT

WHERE XLAT.FIELDNAME = ‘<<FieldName>>’

AND XLAT.EFF_STATUS = ‘A’

AND XLAT.EFFDT = ( SELECT MAX(XLAT1.EFFDT)

FROM PSXLATITEM XLAT1

WHERE  XLATFIELDNAME =XLAT1.FIELDNAME

AND XLATFIELDVALUE = XLAT1.FIELDVALUE)

ODER BY XLAT.FIELDVALUE

Related References