Showing posts with label Performance Tuning. Show all posts
Showing posts with label Performance Tuning. Show all posts

Cardianlity and Selectivity


Cardinality - no of distinct rows.

Selectivity -  no of distinct rows./ total rows.

Highly selective index - has small no of distinct values, for low selective indexes better do a full table scan.

If the optimizer feels that the selectivity of the index is low it ignores. In that case we give hints.

Tuning Tools


 We all know auto tracle and explain plan. Lets see the uncommon

10g auto tools:

 
ADDM, Auto stats collection, Sql Tuning Advisor.

AWR collects, processes and maintains performace stats for problem dedection and self tuning.

SGA_TARGET - for tuning caches and pools.

End to End Tracing

trcsess - command line utility, merges trace info into a single file for processing with TKPROF.


11g auto Tools:
 
Automatic Memory Tuning - MEMORY_TARGET

Resource Manager

Automatic AWR Baselines

Adaptive Metric Baselines

TKPROF:
The TKPROF program converts Oracle trace files into a more readable form

ALTER SYSTEM SET TIMED_STATISTICS = TRUE;

ALTER SESSION SET SQL_TRACE = TRUE;

TKPROF <trace-file> <output-file> explain=user/password@service table=sys.plan_table

Index Usage


When indexes are used:

 
when u equate a value of a range of values for an indexed column.

using functions, concatenating something on cols restrict the use of indexes.

using max and min on an index col uses and index.

In most cases no:

 
is not null will not, but will if the selected col is part of a index.

!= and not in will not, but will if the selected col is part of a index and if the optimizer fells that it would be faster.


When indexes are not used:
is null will not use an index since nulls are not stored in index.

Explain Plan: Execution Paths and Hints


USE_MERGE - two inputs are seperately sorted and merged. No sort is required if the table has indexes. waits until both the sorts are performed.

Good when NO INDEXES are in the tables.
When both are almost equal in size. big or small.

USE_NL - for each record in the first table the second will be queried. so its good if there is an index in the second (driven) table. the best selective should be the driver.

When the tables are unequal in size.
When u need the FIRST_ROWS. (FOR OLTP)

USE_HASH - two tables are compared in memory. One is loaded in memory and hash functions are applied. Then the hashing function compares the other.

When a table is small.
For OLTP.

The table listed first in a set of tables of the same level, is the driving table.

For views, Hints can be given as  view.tab_alias.table or the system generated alias like @SEL$2  t1 index_name. if u want to generate query block name, use QB_NAME(qb) FULL(@qb e)  where e is the table alias.

If a hint specifies an unavailable access path, then the optimizer ignores it.

Three operations sort rows without grouping - order by (SORT ORDER BY), distinct and set operators (SORT UNIQUE NOSORT) and Joins (SORT JOIN)
Note: Union all is a row operation. Others uses set operations.

Grouping functions uses - SORT AGGREGATE and SORT GROUP BY if group by clause is specified.

Merging of query with view can be disabled with NO_MERGE

Oracle tries to combine subqueries with the rest of the query to get more execution options.

Hints:
OPT_PARAM - to set an initialization parameter for the duration of the current query only.

INDEX_SS  - suggest skip scan when the leading column of a composite index is not used.

INDEX_ASC, INDEX_DESC - san in specified order.

INDEX_FFS - Fast full scan

FIRST_ROWS, ALL_ROWS - Instruct the optimizer to choose the best approach to give the first or all rows.

ROWID - Table access by index rowid.

AND_EQUAL - merge results of multiple index scans.

DRIVING_SITE- instructs the optimizer to execute the query at a different site than that selected by the database. DRIVING_SITE(tab1), if table is  tabl1@r1, query is executed at rq.

CURSOR_SHARING_EXACT Will not replace literals in SQL statements with bind variables.

INDEX_JOIN - instructs the optimizer to use an index join as an access path.

USE_CONCAT - instructs the optimizer to transform combined OR-conditions in the WHERE clause of a query into a compound query using the UNION ALL set operator

REWRITE - instructs the optimizer to rewrite a query in terms of materialized views, when possible, without cost consideration.

PUSH_PRED - forces  join perdiacte into a view.
PUSH_SUBQ - forces non merged subqueries to be evaluated asap

FACT - instructs the optimizer that the table specified in tablespec should be considered as a fact table.

INDEX_COMBINE - instructs the optimizer to use a bitmap access path for the table.

MODEL_MIN_ANALYSIS - instructs the optimizer to omit some compile-time optimizations of spreadsheet rules.

MONITOR - forces real-time SQL monitoring for the query, even if the statement is not long running.

NATIVE_FULL_OUTER_JOIN - instructs the optimizer to use native full outer join, which is a native execution method based on a hash join.

NO_EXPAND - instructs the optimizer not to consider OR-expansion for queries having OR conditions or IN-lists in the WHERE clause.

STAR_TRANSFORMATION - instructs the optimizer to use the best plan in which the transformation has been used.

UNNEST - instructs the optimizer to unnest and merge the body of the subquery into the body of the query block that contains it, allowing the optimizer to consider them together when evaluating access paths and joins.


LEADING - instructs the optimizer to use the specified set of tables as the prefix in the execution plan.
ORDERED - instructs Oracle to join tables in the order in which they appear in the FROM clause.

PARALLEL ( tab, no_of_parallel_process, no_of instances) - each instance will span the no_of parallel process.

PARALLEL_INDEX - instructs the optimizer to use the specified number of concurrent servers to parallelize index range scans, full scans, and fast full scans for partitioned indexes.

PQ_DISTRIBUTE - instructs the optimizer how to distribute rows of joined tables among producer and consumer query servers. Such distribution can improve the performance of parallel join operations.

Statistics are stored in:

DBA_TAB_STATISTICS
DBA_TAB_COL_STATISTICS
DBA_TAB_HISTOGRAMS

SQL Tuning Tips

Avoid un-necessary sorts - use union all, use compute stats while creating index.

Hint when needed.

Avoid Cartesian products

Avoid full table scans on large tables

Avoid joining too many tables

Use SQL standards and conventions to reduce parsing

Try to use indexed cols in the  WHERE clause

Use compound indexes with care (Do not repeat columns)

Skip -scan index (INDEX_SS)- to use a composite index even if the leading column is not a limiting condition in the sql.

Monitor index browning (due to deletions; rebuild as necessary)

Use literals in the WHERE clause (use bind variables)

Monitor V$SESSION_LONGOPS to detect long running operations

Use the SHARED_CURSOR parameter

Finally Test Correctly - with Large volumes, Large users.

Database Tuning Tips

Do as little as possible -  Focus on simplyfing the processes instead of achiving a theoretical design perfection.
Avoid multiple IO to the database - bundle queries to a procedure and call thay in ur app. again in procedures  reduce IO to the db.
Avoid multiple connections to the DB - Connecting to the db is the slowest process.
Store data the way u need it.
Go Atomic - Divide and conquer. Use partitions, Local Indexes.
Spread data across disks - Use different tablespaces for tables and indexes and partitions.
Use MVs  - reduce operations performed on tables.
Use parallelism - almost everything can be parallelized.
Avoid the need to query undo segements. - It need to look multipe locations for a piece of data.
Keep stats updated.
Use Disk caching.
Use Larger block size. - keeps more data in the memory.
Make sure blocks are densly packed. - adjust pctfree. mind row chaining and migration.
Try to perform sorts in memory.
Use a cpu with SMALL no of FAST processors.
Use Standard initailization params.
Use proper use of redo logs, checkpoints.
Have sufficient rollback segments.
Small tables can the kept in the keep pool using the buffer_pool clause of create table.
Be free from Deployment and Migration Errors.

The Golden Rules for Performance Tuning



Change one thing at a time and then measure the differences.

Tuning individual sql may yield little performance. The no. of executions of the sql may be the problem.

Top Ten Mistakes Found in Oracle Systems


1. Bad Connection Management
2. Bad Use of Cursors and the Shared Pool
3. Bad SQL
4. Use of Nonstandard Initialization Parameters
5. Getting Database I/O Wrong
6. Redo Log Setup Problems
7. Serialization of data blocks in the buffer cache due to lack of free lists, free list groups, transaction slots (INITRANS), or shortage of rollback segments.
8. Long Full Table Scans
9. High Amounts of Recursive (SYS) SQL
10. Deployment and Migration Errors

Row Chaning and Migration

Row Migration:
A row migrates when an update to that row would cause it to not fit on the block anymore (with all of the other data that exists there currently).  A migration means that the entire row will move and we just leave behind the «forwarding address». So, the original block just has the rowid of the new block and the entire row is moved.


Row Chaining:
A row is too large to fit into a single database block. For example, if you use a 4KB blocksize for your database, and you need to insert a row of 8KB into it, Oracle will use 3 blocks and store the row in pieces.

Some conditions that will cause row chaining are: Tables whose rowsize exceeds the blocksize. Tables with LONG and LONG RAW columns are prone to having chained rows. Tables with more then 255 columns will have chained rows as Oracle break wide tables up into pieces.
So, instead of just having a forwarding address on one block and the data on another we have data on two or more blocks.