Professional Documents
Culture Documents
2. Avoid the use of NOT IN, HAVING and NOT= in where clause, instead,
use the NOT EXIST clause.
7. Careful while you are creating table in the DATATYPE part and the size of
the variable because it has got a great impact on the storage of the table.
8. Careful while creating INDEXES because too much indexes for a table is
slowdown the efficiency, in terms of Update and insert statements so be
clear which index you need to create on which column.
The DISTINCT clause creates a lot of extra work for Database Server,
and reduces the physical resources that other SQL statements have at
1
their disposal. Because of this, only use the DISTINCT clause if it is
necessary.
10. When using the UNION statement keep in mind that, by default, it performs
the equivalent of a SELECT DISTINCT on the final result set. In other
words, UNION takes the results of two like record sets, combines them, and
then performs a SELECT DISTINCT in order to eliminate any duplicate
rows. This process occurs even if there are no duplicate records in the final
record set. If you know that there are duplicate records, and this presents a
problem for your application, then by all means use the UNION statement to
eliminate the duplicate rows.
On the other hand, if you know that there will never be any duplicate rows,
or if there are, and this presents no problem to your application, then you
should use the UNION ALL statement instead of the UNION statement.
The advantage of the UNION ALL is that is does not perform the SELECT
DISTINCT function, which saves a lot of unnecessary SQL Server
resources from being using.
11. The Query Optimizer will perform a table scan or a index scan on a table if
the WHERE clause in the query contains an OR operator and if any of the
referenced columns in the OR clause are not indexed (or does not have a
useful index). Because of this, if you use many queries with OR clauses,
you will want to ensure that each referenced column in the WHERE clause
has a useful index
Don't use ORDER BY in your SELECT statements unless you really need
to, as it adds a lot of extra overhead. For example, perhaps it may be
more efficient to sort the data at the client than at the server. In other
cases, perhaps the client doesn't even need sorted data to achieve its
goal. The key here is to remember that you shouldn't automatically sort
data, unless you know it is necessary.
13. If you want to boost the performance of a query that includes an AND
operator in the WHERE clause, consider the following:
• Of the search criterions in the WHERE clause, at least one of them should
be based on a highly selective column that has an index.
2
• If at least one of the search criterions in the WHERE clause is not highly
selective, consider adding indexes to all of the columns referenced in the
WHERE clause.
• If none of the column in the WHERE clause are selective enough to use
an index on their own, consider creating a covering index for this query.
14. Avoid using NOT IN , NOT= , NOT LIKE , NOT EXIST in the inner where
clause because it will use index rather full table scan. EXAMPLE
15. Do not use SELECT * in your queries. Always write the required column
names after the SELECT statement, like SELECT CustomerID,
CustomerFirstName, City. because parsing is done for all the columns used
From all tables specified in FROM clause and This technique results in less
disk IO and less network traffic and hence better performance.
17. Do not let your front-end applications query/manipulate the data directly
using SELECT or INSERT/UPDATE/DELETE statements. Instead, create
stored procedures, and let your applications access these stored
procedures. This keeps the data access clean and consistent across all the
modules of your application, at the same time centralizing the business logic
within the database. So as much as possible keep the SQL / PL/SQL
statements in procedures and functions because it’s eliminate Re - parsing.
19. Try to put all the SQL /PLSQL statements in the database procedure and
function so that Parsing will be eliminated.
20. Order of the tables in Joins: If you specify 2 or more tables in the FROM
clause of a SELECT statement, then Oracle parser will process the tables
3
from right to left, so the table name you specify last will be processed first.
In this case you have to choose one table as driving table. Always choose
the table with less number of records as the driving table.
21. Never compare NULL to anything else: All expressions return NULL if
one of the operands is NULL. This is applicable for all operators except
Concatenation operator (||).
Use Bind Variables: It is also better to use bind variables in queries. That
way the query becomes generic and therefore re-usable. For example,
instead of writing a query likes -
Change it to -
22. Use table alias: Always use table alias and prefix all column names with
the aliases when you are using more than one table.
24. Name the Columns in a Query:There are 2 good reasons why it is better
to name the columns in a query rather than to use "select * from ...".
1. Network traffic is reduced. This can have a significant impact on
performance if the table has a large number of columns, or the table has a
long or long raw column (both of which can be up to 2 GB in length).
These types of columns will take a long time to transfer over the network
and so they should not be fetched from the database unless they are
specifically required.
2. It could save the need for changes in the future. If any columns is
added to or removed from the base table/view, then “select * “statement
can produce wrong results set and statement may fail.
4
Use of expressions and indexes: The optimizer fully evaluates
expressions whenever possible and translates certain syntactic constructs
into equivalent constructs. This is done either because Oracle can more
quickly evaluate the resulting expression than the original expression or
because the original expression is merely a syntactic equivalent of the
resulting expression. Any computation of constants is performed only once
when the statement is optimized rather than each time the statement is
executed. Consider these conditions that test for salaries
greater than $2000.
sal > 24000/12
sal > 2000
sal*12 > 24000
For Example:
SELECT * FROM emp WHERE NOT deptno = 0;
5
SELECT * FROM emp WHERE SUBSTR (ename, 1, 3) = 'MIL';
Rather than
6
CASE statement to count only the rows where the condition is valid. For
example: