Você está na página 1de 2

SQL Tuning Tips

Always SELECT exact column names than using SELECT * FROM TABLE_NAME.
There is always a primary key index on a table. Thus accessing only primary key from a table will
only read the index file and should completely ignore the table itself.
Always use alias for Column Names.
Use Index only when 3% to 5% of data is to be filtered in WHERE clause.
Dont use Indexes for small tables.
When more than one clauses are used in WHERE, then one clause will use index and all others
are applied as filters. Index column will be selected depending upon, which returns minimum
rows.
Use IN Statement preferably over OR Statement, while filtering data from one column.
Use EXIST Statement preferably over IN Statement while dealing with correlated sub query
(Correlated sub query runs separately against each row/result of Parent query).
Use function-based indexes when functions are used in a Statement.
Try to match comparison condition column sequences with existing index column sequences,
although it is not strictly necessary.
Try to use unique, single-column indexes wherever possible.
A single-column unique index is much more likely to produce exact hits. An exact hit is the
fastest access method.
Order by Statement is ignored when primary-key column is fetched because primary key index is
used and it fetches the results in required order.
If we have two reference keys, which in combination makes a composite primary key, using
foreign key in WHERE clause hits reference key index and using both keys in WHERE clause uses
primary key index (which is better).
If we have two reference keys, which in combination make a composite primary key, using
foreign key in WHERE clause hits reference key index and using both in Order by clause will use
Primary key index (which is better). This trick of Using order by clause overrides the
specifications of WHERE clause. An important point in this scenario is described in next bullet
point.
In case of Composite Primary Key. If we want to use both columns (of composite Key) in Order
By clause, we should write columns in same order as in index, which ignores the Order by clause
and saves the sorting cost.
If we have two non-unique indexes on two foreign keys, using one in WHERE clause uses an
index, while using other in Order by clause will override the index hit by WHERE clause.
Above Points described using Order by clause to override WHERE clause index hits. But this can
be very costly if we use one column of composite primary key in WHERE clause and other in
Order by clause, which makes Order by clause to re-sort results from WHERE clause. In this
scenario, WHERE clause index hit will be entertained and then sorting will be done.
Try not to override the WHERE clause with the ORDER BY clause because the Optimizer may
choose a less efficient method of execution based on the ORDER BY clause.
Foreign key non-unique index gives better performance in Group by Statement, as compared to
primary key composite unique index, since the composite index is much larger in both size and
rows.
Reversing the order of columns in group statement doesnt affect the cost.

Você também pode gostar