SQL Queries - Best Practices
SQL Queries - Best Practices
1. Always use the where clause in your select statement to narrow the number
of rows returned.
If we don’t use a where clause, the Oracle performs a full table scan on our
table and returns all of the rows.
Ex:
Replace
SELECT * FROM DEPT WHERE DEPTNO IN
(SELECT DEPTNO FROM EMP E)
With
SELECT * FROM DEPT D WHERE EXISTS
(SELECT 1 FROM EMP E WHERE D.DEPTNO = E.DEPTNO)
Note: IN checks all rows. Only use IN if the table in the sub-query is
extremely small.
3. When you have a choice of using the IN or the BETWEEN clauses in your SQL,
use the BETWEEN clause as it is much more efficient than IN.
Depending on the range of numbers in a BETWEEN, the optimizer will choose
to do a full table scan or use the index.
5. Use equijoins. It is better if you use with indexed column joins. For maximum
performance when joining two or more tables, the indexes on the columns to
be joined should have the same data type.
6. Avoid a full-table scan if it is more efficient to get the required rows through
an index. It decides full table scan if it has to read more than 5% of the table
data (for large tables).
7. Avoid using an index that fetches 10,000 rows from the driving table if you
could instead use another index that fetches 100 rows and choose selective
indexes.
9. Choose the join order so you will join fewer rows to tables later in the join
order.
use smaller table as driving table
have first join discard most rows
10. Set up the driving table to be the one containing the filter condition that
eliminates the highest percentage of the table.
11. In a where clause (or having clause), constants or bind variables should
always be on the right hand side of the operator.
13. If you want the index used, don’t perform an operation on the field.
Replace
SELECT * FROM EMPLOYEE WHERE SALARY +1000 = :NEWSALARY
With
SELECT * FROM EMPLOYEE WHERE SALARY = :NEWSALARY –1000
14. All SQL statements will be in mixed lower and lower case. All reserve words
will be capitalized and all user-supplied objects will be lower case. (Standard)
16. Try joins rather than sub-queries which result in implicit joins
Replace
SELECT * FROM A WHERE A.CITY IN (SELECT B.CITY FROM B)
With
SELECT A.* FROM A, B WHERE A.CITY = B.CITY
17. Replace Outer Join with Union if both join columns have a unique index:
Replace
SELECT A.CITY, B.CITY FROM A, B WHERE A.STATE=B.STATE (+)
With
SELECT A.CITY, B.CITY FROM A, B WHERE A.STATE=B.STATE
UNION
SELECT NULL, B.CITY FROM B WHERE NOT EXISTS
(SELECT 'X' FROM A.STATE=B.STATE)
18. Use bind variables in queries passed from the application (PL/SQL) so that the
same query can be reused. This avoids parsing.
19. Use Parallel Query and Parallel DML if your system has more than 1 CPU.
20. Match SQL where possible. Applications should use the same SQL statements
wherever possible to take advantage of Oracle's Shared SQL Area. The SQL
must match exactly to take advantage of this.
21. No matter how many indexes are created, how much optimization is done to
queries or how many caches and buffers are tweaked and tuned if the design
of a database is faulty, the performance of the overall system suffers. A good
application starts with a good design.
SELECT DISTINCT
SELECT UNIQUE
SELECT ....ORDER BY...
SELECT....GROUP BY...
CREATE INDEX
CREATE TABLE.... AS SELECT with primary key specification
Use of INTERSECT, MINUS, and UNION set operators
Unindexed table joins
Some correlated sub-queries