Oracle SQL tuning - Tips for writing more efficient SQL

1.Rewrite complex subqueries with temporary tables - Oracle created the global temporary table (GTT) and the SQL WITH operator to help divide-and-conquer complex SQL sub-queries (especially those with with WHERE clause subqueries, SELECT clause scalar subqueries and FROM clause in-line views).  Tuning SQL with temporary tables (and materializations in the WITH clause) can result in amazing performance improvements.
 
2.Use minus instead of EXISTS subqueries - Some say that using the minus operator instead of NOT IN and NOT Exists will result in a faster execution plan.
 
3.Use SQL analytic functions - The Oracle analytic functions can do multiple aggregations (e.g. rollup by cube) with a single pass through the tables, making them very fast for reporting SQL.
 
4.Re-write NOT EXISTS and NOT EXISTS subqueries as outer joins - In many cases of NOT queries (but ONLY where a column is defined as NULL), you can re-write the uncorrelated subqueries into outer joins with IS NULL tests.  Note that this is a non-correlated sub-query, but it could be re-written as an outer join.
select book_key from book 
where 
book_key NOT IN (select book_key from sales);


=======================================>
select b.book_key from book b, sales s 
where 
   b.book_key = s.book_key(+) 
and 
   s.book_key IS NULL;


5.Index your NULL values - If you have SQL that frequently tests for NULL, consider creating an index on NULL values.  To get around the optimization of SQL queries that choose NULL column values (i.e. where emp_name IS NULL), we can create a function-based index using the null value built-in SQL function to index only on the NULL columns.  


Note: Starting in Oracle 11g, there is new "create index" syntax that allows NULL values to be included in the index:

create index
    emp_ename_idx
on
   emp 
   (ename asc, 1)
;

Here, the "1" tells Oracle that to index on NULL values within the tables.

 6.Leave column names alone - Never do a calculation on an indexed column unless you have a matching function-based index (a.k.a. FBI).  Better yet, re-design the schema so that common where clause predicates do not need transformation with a BIF:where salary*5            > :myvalue
where substr(ssn,7,4)     = "1234"
where to_char(mydate,mon) = "january"


7.Avoid the use of NOT IN or HAVING. Instead, a NOT EXISTS subquery may run faster (when appropriate). 
 
8.Avoid the LIKE predicate = Always replace a "like" with an equality, when appropriate. 
 
9.Never mix data types - If a WHERE clause column predicate is numeric, do not to use quotes. For char index columns, always use quotes. There are mixed data type predicates:
where cust_nbr = "123"
where substr(ssn,7,4) = 1234


10.Use decode and case - Performing complex aggregations with the "decode" or "case" functions can minimize the number of times a table has to be selected. 
 
11.Don't fear full-table scans - Not all OLTP queries are optimal when they uses indexes.  If your query will return a large percentage of the table rows, a full-table scan may be faster than an index scan.  This depends on many factors, including your configuration (values for db_file_multiblock_read_count, db_block_size), query parallelism and the number of table/index blocks in the buffer cache.
 

12.Use those aliases - Always use table aliases when referencing columns.


http://www.dba-oracle.com/art_sql_tune.htm

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324607593&siteId=291194637