DB - Tuning

Goals for SQL Tuning

数据库优化的基本层次

http://www.cnblogs.com/easypass/archive/2010/12/08/1900127.html

db-tuning-levels

Example - Format SQL Statements

FROM TABLE1,                              Smallest Table
     TABLE2,                              to
     TABLE3                               Largest Table, also BASE TABLE
WHERE TABLE1.COLUMN = TABLE3.COLUMN       Join condition
  AND TABLE2.COLUMN = TABLE3.COLUMN       Join condition
[ AND CONDITION1 ]                        Least restrictive
[ AND CONDITION2 ]                        Most restrictive

http://www.informit.com/library/content.aspx?b=STY_Sql_24hours&seqNum=136

Example - Combine Multiples Scans with CASE Statements

Often, it is necessary to calculate different aggregates on various sets of tables. Usually, this is done with multiple scans on the table, but it is easy to calculate all the aggregates with one single scan. Eliminating n-1 scans can greatly improve performance.

Bad example

SELECT COUNT (*)
  FROM employees
  WHERE salary < 2000;

SELECT COUNT (*)
  FROM employees
  WHERE salary BETWEEN 2000 AND 4000;

SELECT COUNT (*)
  FROM employees
  WHERE salary>4000;

->

Good example

SELECT COUNT (CASE WHEN salary < 2000
                   THEN 1 ELSE null END) count1,
       COUNT (CASE WHEN salary BETWEEN 2001 AND 4000
                   THEN 1 ELSE null END) count2,
       COUNT (CASE WHEN salary > 4000
                   THEN 1 ELSE null END) count3
  FROM employees;

http://docs.oracle.com/cd/B19306_01/server.102/b14211/sql_1016.htm#i35699

Fork me on GitHub