SQL Performance Tuning Tips
By Puneet Goenka
Tuning Tips and Techniques
Oracle’s SQL is a very flexible language. You can use many different SQL statements to accomplish the same purpose. Yet, although dozens of differently constructed queries and retrieval statements can produce the same result, in a given situation only one statement will be the most efficient choice.
It is much harder to write efficient SQL than it is to write functionally correct SQL A SQL choice is correct only if it produces the right result in the shortest possible amount of time, without impeding the performance of any other system resources.
Sharing SQL Statements
Parsing a SQL statement and figuring out its optimal execution plan are time-consuming operations, Oracle holds SQL statements in memory after it has parsed them Whenever you issue a SQL statement, Oracle first looks in the context (SGA) area to see if there is an identical statement there To be shared, the SQL statements must truly be the same
Name From Student Where Student_Number = ‘0220’
. NAME FROM STUDENT WHERE STUDEN_NUMBER = ‘0220’
Select Student_Number. For example the following two select statements are NOT the same:
Using Bind variables when possible
Try using Bind Variable instead of Literals. Also. LAST_NAME FROM Client WHERE CLIENT_NUM = :Client_Num
You do not need to create a new cursor or re-parse the SQL statement if the value of the bind variable changes. if another session executes the same statement. it is likely to find them in the Shared Pool. LAST_NAME FROM Client WHERE CLIENT_NUM = 1200
Since the CLIENT_NUMBER is likely to be different for every execution.
. since the name of the bind variable does not change from execution to execution. Consider the following SQL statement –
SELECT FIRST_NAME. we will almost never find a matching statement in the Shared Pool and consequently the statement will have to be reparsed every time Consider the following approach –
the original ROWID points to the new location or the new ROWID and so on. If the record block or location was changed for any reason. Use ROWID whenever possible to get the best performance out of your retrievals
.Using ROWID When Possible
Each record added to the database has a unique ROWID and will
never change until the delete statement issued on that record.
cursor accounts_cur is select acct_no. … … end loop. … for acct_rec in accounts_cur loop … update account set … … where rowid = acct_rec.acct_rowid. .
. branch Rowid acct_rowid. . currency. . … … From account where .
avoid including a HAVING clause in the SELECT statements. HAVING clause usually used to filter a SELECT statement containing group functions. and etc. The HAVING clause filters selected rows only after all rows have been fetched. summing.Using WHERE in Place of HAVING
In general. This could include sorting. select * from account where cust_Active_flag = ‘y’ having group = ‘001’ Instead use select * from account where cust_Active_flag = ‘y’ and group = ‘001’
The UNION operation sorts the result set to eliminate any rows. UNION ALL includes duplicate rows and does not require a sort.Using UNION ALL instead of UNION
The SORT operation is very expensive in terms of CPU consumption. use UNION ALL
. Unless you require that these duplicate rows be eliminated. which are within the sub-queries.
.Using NOT EXISTS in place of NOT IN for indexed
In sub-query statements such as the following. select * from Student where STUDENT_NUM not in (select STUDENT_NUM from CLASS) So useselect * from STUDENT C where not exists (select 1 from CLASS A where A. the NOT IN clause causes an internal sort/merge.STUDENT_NUM = C.
the NOT IN clause causes an internal sort/merge select * from system_user where su_user_id not in
(select ac_user from account) INSTEAD USE select * from system_user where su_user_id in (select su_user_id from system_user minus select ac_user from account)
.Using IN with MINUS in place of NOT IN for non
In sub-query statements such as the following.
Using Joints in Place of EXISTS for Unique Scan Indexes and
In general join tables rather than specifying sub-queries for them such as the following:
select acct_ID.def_curr = '001'
. branch B where b.branch and A. currency.code = A.currency. branch from account where exists (select 1 from branch where code = branch and def_curr = '001') With join select acct_ID. branch from account A.
FULL(table_name). RULE. ACCT_ID_IND) */ NAME.
. Some commonly used Hints are: CHOOSE. INDEX(table_name index_name). ACCT_ALLOCATION_PERCENTAGE FROM ACCOUNTS A WHERE ACCOUNT_ID = :ACCT_ID AND CLIENT_ID= :CLIENT_ID In the above SQL statement.Influencing the Optimizer using HINTS
Hints are special instructions to Optimizer. SELECT /*+RULE*/ NAME. an Index Hint has been used to force the use of a particular index. SELECT /*+ INDEX(A. USE_NL. ACCT_ALLOCATION_PERCENTAGE FROM ACCOUNTS WHERE ACCOUNT_ID = 1200 The above SQL statement will be processed using the RULE based optimizer. USE_HASH(table_name). You can change the Optimization goal for an individual statement by using Hint. PARALLEL(table_name parallelism) etc.
Generally Indexes are more selective if the column/columns have a large number of unique values. it is called CONCATENATED INDEX . But they do not come without a cost. which may slow down performance Besides. the usefulness of an Index depends on selectivity of a column/columns. Indexes must be updated during INSERT. be sure to use LEADING columns
. If an Index contains more than one column.Using Indexes to Improve Performance
Indexes primarily exist to enhance performance. Concatenated index is often more selective than a single key index. UPDATE and DELETE operation. Column positions play an important role in Concatenated index. While using Concatenated Index.
If more than 52%.Which is Faster: Indexed Retrieval or Full-table
Full-table scans can be efficient because they require little disk movement. The disk starts reading at one point and continues reading contiguous data blocks.
. this percentage defers from table to table and depends on the physical I/O. Index retrievals are usually more efficient when retrieving few records or when using joints with other tables. of the table retrieved a full table scan is better.
Select * from Account Where substr(ac_acct_no.
. avoid doing calculations on indexed columns. In general.1) = ‘1’ Instead use Select * from Account Where ac_acct_no like ‘1%’ Note : The SQL functions MIN and MAX are exceptions to this rule and will utilize all available indexes.Avoiding Calculations on Indexed Columns
The optimizer does not use an index if the indexed column is a part of a function (in the WHERE clause). apply function and concatenating on an indexed columns.1.
Remember.Avoiding NOT on Indexed Columns
In general avoid using NOT when testing indexed columns. it will choose not to use index and will perform a full-table scan instead.
When Oracle encounters a NOT.
For example the following select statement will never use the index on STUDENT_NUM column Select * from student Where STUDENT_NUM not like ‘9%’
. but not what is NOT in a table. indexes are built on what is in a table.
Using OR on an indexed column causes the optimizer to perform a full-table scan rather than an indexed retrieval.
.Using UNION in Place of OR
In general. always consider the UNION verb instead of OR verb in the WHERE clauses.
. And the conditions which filter out the maximum records should be placed at the end after the joins as the parsing is done from BOTTOM to TOP.
FROM EMP E WHERE SAL > 50000 AND JOB = ‘CLERK’ AND 25 < (SELECT COUNT(*) FROM EMP WHERE MGR = E.EMPNO). .Position of Joins in the WHERE Clause
Table joins should be written first before any condition of WHERE clause. . Least Efficient : SELECT .
.EMPNO ) AND SAL > 50000 AND JOB = ‘CLERK’.
Most Efficient :
SELECT . . . FROM EMP E WHERE 25 < (SELECT COUNT(*) FROM EMP WHERE MGR = E.
ora parameters:None None hash_area_size
sort_area_size hash_join_enabled db_file_multi block_
Features:Works with any join I/O for masterdetail Queries
Better than nested
loop when indesx is missing or search critiria is not restrictive
Better than nested
loop when index is missing or search criteria is not restrictive
.Side by Side Comparison of Join Methods
Join When can be used: joins on complete cluster key of clustered Optimizer hint: use_nl Any join Equi joins only Equi joins only Equi Nested Loops Join Sort-Merge Join Hash Join Cluster
Use_merge Temporary segments
None Storage Disk I/O
Resource concerns: CPU init.
it uses an internal sort/merge procedure to join those tables. SELECT COUNT(*) FROM TABA. so the table name you specify last (driving table) is actually the first table processed. TABB
SELECT COUNT(*) FROM TABB. When ORACLE processes multiple tables. Next.09 seconds elapsed
0. Table TABB has 1 row. you must choose the table containing the lowest number of rows as the driving table. For example: Table TABA has 16.96 seconds elapsed
26.384 rows. ORACLE parser always processes table names from right to left. it scans and sorts the first table (the one specified last in the FROM clause). First. If you specify more than one table in a FROM clause of a SELECT statement. it scans the second table (the one prior to the last in the FROM clause) and merges all of the rows retrieved from the second table with those retrieved from the first table.
CAT_NO = C. CATEGORY C WHERE E. The EMP table represents the intersection between the LOCATION table and the CATEGORY table. EMP E WHERE E.LOCN = L. LOCATION L. FROM EMP E. . The intersection table is the table that has many tables dependent on it.LOCN AND E.EMP_NO BETWEEN 1000 AND 2000
..g.EMP_NO BETWEEN 1000 AND 2000 AND E. SELECT .CAT_NO AND E. If three tables are being joined. FROM LOCATION L. . .CAT_NO AND E. select the intersection table as the driving table. . CATEGORY C.LOCN = L. E.CAT_NO = C.LOCN is more efficient than this next example: SELECT .
because of conversion this statement will actually be processed as: Select * from Account Where to_number(ACCOUNT_ID) = 90426001
. the character column automatically has its type converted to numeric.Problems when Converting Index Column Types
Oracle performs simple column type conversion.
Select * from Account Where ACCOUNT_ID = 90426001 In fact. or casting. when it compares columns of different type. If a numeric column is compared to an alphabetic column.
But the following statement: Select * From acc_txn Where acc_txn_ref_no = ‘119990012890’ Will be processed as: Select * From acc_txn Where acc_txn_ref_no = to_number(‘119990012890’ )
You can achieve the same result much more efficiently with DECODE:
. For example: SELECT COUNT(*). SUM(SAL) FROM EMP WHERE DEPT_NO = 0030 AND ENAME LIKE ‘SMITH%’. SELECT COUNT(*).Use DECODE to Reduce Processing
The DECODE statement provides a way to avoid having to scan the same rows repetitively or to join the same table repetitively. SUM(SAL) FROM EMP WHERE DEPT_NO = 0020 AND ENAME LIKE ‘SMITH%’.
. DECODE can be used in GROUP BY or ORDER BY clause effectively. Similarly.0020.NULL)) D0030_COUNT. ‘X’.‘X’.SELECT COUNT(DECODE(DEPT_NO.0030. 0030. SAL. NULL)) D0020_COUNT. SAL. NULL)) D0030_SAL FROM EMP WHERE ENAME LIKE ‘SMITH%’. SUM(DECODE(DEPT_NO. NULL)) D0020_SAL.0020.
minimize the number of table lookups in queries. To improve performance. For example: Least Efficient :
SELECT TAB_NAME FROM TABLES WHERE TAB_NAME = (SELECT TAB_NAME FROM TAB_COLUMNS WHERE VERSION = 604) AND DB_VER = (SELECT DB_VER FROM TAB_COLUMNS WHERE VERSION = 604)
. particularly if your statements include sub-query SELECTs or multi-column UPDATEs.
DB_VER)= (SELECT TAB_NAME. DB_VER FROM TAB_COLUMNS
VERSION = 604)
.Most Efficient :
SELECT TAB_NAME FROM TABLES WHERE (TAB_NAME.
EXISTS is a faster alternative because the RDBMS kernel realizes that when the sub-query has been satisfied once.DEPT_NO Most Efficient : SELECT DEPT_NO.DEPT_NO = E. the query can be terminated. departments that have many employees). DEPT_NAME FROM DEPT D.Use EXISTS in Place of DISTINCT
Avoid joins that require the DISTINCT qualifier on the SELECT list when you submit queries used to determine information at the owner end of a one-to-many relationship (e.DEPT_NO = D.
Least Efficient : SELECT DISTINCT DEPT_NO. EMP E WHERE D.
.g. DEPT_NAME FROM DEPT D WHERE EXISTS (SELECT ‘X’ FROM EMP E WHERE E.
Some Do’s and Don’ts
Some SELECT statement WHERE clauses do not use indexes at all.
. an alternative approach. If you have specified an index over a table that is referenced by a clause of type shown in this section Oracle will simply ignore the index. which will allow you to get better performance out of your SELECT statements is suggested. For each clause that cannot use an index.
Do Not Use:
Select * from Account Where substr(ac_acct_no.1) = ‘9’
Select * from Account Where ac_acct_no like ‘9%’
Do Not Use: Select * From fin_trxn Where ft_trxn_ref_no != 0 Use:
Select * From fin_trxn Where ft_trxn_ref_no > 0
Do Not Use: Select * From account Where ac_type || ac_branch = ‘sav001’
Select * From account Where ac_type = ‘sav’ And ac_branch = ‘sav001’
Do Not Use:
Select * From CLIENT where to_char(CUTT_OFF_TIME.’yyyymmdd’) = to_char(sysdate.’yyyymmdd’)
Select * From CLIENT Where CUT_OFF_DATE >= trunc(sysdate) and CUT_OFF_TIME < trunc(sysdate) + 1
Do Not Use: Select * From acct_trxn Where to_char(at_value_date.’yyyymmdd’) Use: Select * From acct_trxn Where at_value_date >= trunc(sysdate) + 1
.’yyyymmdd’) > to_char(sysdate.
’yyyymmdd’) < to_char(sysdate. Do Not Use: Select * From acct_trxn Where to_char(at_value_date.’yyyymmdd’) Use:
Select * From acct_trxn Where at_value_date < trunc(sysdate)
Do Not Use: Select * From acct_trxn Where to_char(at_value_date.’yyyymmdd’) >= to_char(sysdate.’yyyymmdd’) Use: Select * From acct_trxn Where at_value_date >= trunc(sysdate)
Do Not Use:
Select * From acct_trxn Where to_char(at_value_date.’yyyymmdd’) Use: Select * From acct_trxn Where at_value_date < trunc(sysdate) + 1 Do Not Use: Select count( *) From BROKER Use: Select count(PRIMARY_KEY or a non null INDEX column or 1 ) From Broker
.’yyyymmdd’) <= to_char(sysdate.
The SQL parser handles all the field references by obtaining the names of valid columns from the data dictionary and substitutes them on the command line.Avoid Using SELECT * Clauses
The dynamic SQL column reference (*) gives you a way to refer to all of the columns of a table.the * has to be converted to each column in turn. Do not use the * feature because it is very inefficient -.
. which is time consuming.
Execution Plan ---------------------------------------------------------0 SELECT STATEMENT Optimizer=CHOOSE (Cost=3 Card=10 Bytes=170) 1 0 SORT (ORDER BY) (Cost=3 Card=10 Bytes=170) 2 1 TABLE ACCESS (FULL) OF ‘AQUATIC_ANIMAL’ (Cost=1 Card=10 Bytes=170)
.g SQL> SET AUTOTRACE ON EXPLAIN
SQL> SELECT animal_name FROM aquatic_animal ORDER BY animal_name.Using SQL*Plus Autotrace
If you’re using SQL*Plus you can take advantage of the auto trace feature to have queries explained automatically. E.
ANIMAL_NAME -----------------------------Batty Bopper Flipper
3 rows selected. SQL*Plus will execute the query and display the execution plan following the results.
you can turn the feature off by issuing the SET AUTOTRACE OFF command. If a query generates a lot of I/O and consumes a lot of CPU.
. In that case use following : SQL> SET AUTOTRACE TRACEONLY EXPLAIN you are through using autotrace. you won’t want to kick it off just to see the execution plan. SQL*Plus does execute the query.