Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server...

57
Connecting to an Instance User Server Server User Client User Server Oracle database Server Application server Browser

Transcript of Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server...

Page 1: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Connecting to an Instance

User Server

ServerUser

Client

User Server

Oracle database

ServerApplication server

Browser

Page 2: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Oracle Memory Structures .

Java poolDatabase

buffer cacheRedo log

buffer

Shared pool Large pool

SGA

Streams pool

Serverprocess

1PGA

Serverprocess

2PGA

Backgroundprocess

PGA

Page 3: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Oracle Instance Management

SystemMonitor(SMON)

DatabaseWriter

(DBWn)

LogWriter(LGWR)

ProcessMonitor(PMON)

Archiver(ARCn)

SGA

Java pool

Shared pool Large poolStreams pool

Databasebuffer cache

Redo log buffer

Checkpoint(CKPT)

Controlfiles

Datafiles

Redo logfiles

Archive log files

Page 4: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Shared Pool

– The shared pool consists of:• Data dictionary cache containing information on

objects, storage, and privileges• Library cache containing information such as SQL

statements, parsed or compiled PL/SQL blocks, and Java classes

– Appropriate sizing of the shared pool affects performance by:• Reducing disk reads• Allowing shareable SQL code• Reducing parsing, thereby saving CPU resources• Reducing latching overhead, thereby improving

scalability

Page 5: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Shared SQL Areas

SGA Shared SQL

Cursor forSELECT statement 2

Cursor forSELECT statement 1

User A User B User C

SELECTstatement 1

SELECTstatement 2

SELECTstatement 1

Page 6: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Program Global Area (PGA)– PGA is a memory area that contains:

• Session information

• Cursor information

• SQL execution work areas–Sort area–Hash join area–Bitmap merge area–Bitmap create area

– Work area size influences SQL performance.– Work areas can be automatically or manually

managed.

Page 7: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

SQL Statement Processing Phases

CloseOpen

FetchBindParse Execute

Page 8: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

SQL Statement Processing Phases: Parse

– Parse phase:• Searches for the statement in the shared pool• Checks syntax• Checks semantics and privileges• Merges view definitions and subqueries• Determines execution plan

– Minimize parsing as much as possible:• Parse calls are expensive• Avoid reparsing• Parse once, execute many times

Page 9: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

SQL Statement Processing Phases: Bind

– Bind phase:• Checks the statement for bind variables

• Assigns or reassigns a value to the bind variable

– Bind variables impact performance when:• They are not used, and your statement would benefit

from a shared cursor

• They are used, and your statement would benefit from a different execution plan

Page 10: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

SQL Statement Processing Phases: Execute and Fetch

– Execute phase:• Executes the SQL statement • Performs necessary I/O and sorts for data

manipulation language (DML) statements

– Fetch phase:• Retrieves rows for a query• Sorts for queries when needed• Uses an array fetch mechanism

Page 11: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Processing a DML StatementDatabase

Data files

Control

files

Redolog files

UPDATE employees ...

Userprocess

SGA

Database buffer cache

Shared pool

Redo logbufferServer

process3

1

4

2

Page 12: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

InstanceCOMMIT Processing

Database

Data files

Control

files

Redolog files LGWR

Userprocess

SGA

Database buffer cache

Shared pool

Redo logbufferServer

process

Page 13: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

SQL Tuning Process: OverviewIdentify

high-load SQL

Analyze SQL

Takecorrective

action

Page 14: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Functions of the Oracle Query Optimizer

•The Oracle query optimizer determines the most efficient execution plan and is the most important step in the processing of any SQL statement. •The optimizer:– Evaluates expressions and conditions

– Uses object and system statistics

– Decides how to access the data

– Decides how to join tables

– Decides which path is most efficient

Page 15: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Top Database Performance Issues

– Bad connection management– Poor use of cursors and the shared pool– Bad SQL– Nonstandard initialization parameters– I/O issues– Long full-table scans – In-disk sorts– High amounts of recursive SQL– Schema errors and optimizer problems

Page 16: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Overview of SQL Tuning

1. Identify causes of poor performance.

2. Identify problematic SQL.• Automatic: ADDM, Top SQL• Manual: V$ views, statspack

3. Apply a tuning method.• Manual tuning• Automatic SQL tuning

4. Implement changes to: • SQL statement constructs • Access structures such as indexes

Page 17: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Manual Tuning

1. Gather information about the referenced objects.

2. Gather optimizer statistics.

3. Review execution plans.

4. Restructure SQL statements.

5. Restructure indexes and create materialized views.

6. Maintain execution plans.

Page 18: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Oracle Optimizer•The optimizer creates an execution plan for every SQL statement by:– Evaluating expressions and conditions– Using object and system statistics– Deciding how to access the data– Deciding how to join tables– Deciding which path is most efficient– Comparing the cost for execution of different plans– Determining the least-cost plan

Page 19: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Functions of the Query Optimizer

Querytransformer

Estimator

Plan generator

Parsed query(from parser)

Query plan(to row-source generator)

DictionaryStatistics

Transformed query

Query + estimates

Page 20: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Setting the Optimizer Approach

– At the instance level, set the following parameter:

– For a session, use the following SQL command:

OPTIMIZER_MODE = {FIRST_ROWS(_n)|ALL_ROWS}

ALTER SESSION SET optimizer_mode = {first_rows(_n)|all_rows}

Page 21: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Optimizing SQL Statements•Best throughput– Time required to complete the request– Suitable for:

• Batch processing• Report applications

•Fast response– Time for retrieving the first rows – Suitable for:

• Interactive applications• Web-based or GUI applications

Page 22: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Access Paths

– Full table scan – Row ID scan – Index scan – Sample table scan

Page 23: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Full Table Scans

– Lack of index– Large amount of data– Small table

Page 24: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Index Scans

•Types of index scans:– Index unique scan (csak egyetlen értéket kell az indexben megkeresni)

– Index range scan – Index range scan descending– Index skip scan

Page 25: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Skip Scanning of Indexes

– Index skip scanning enables access through a composite index when the prefix column is not part of the predicate.

– Skip scanning is supported by:• Cluster indexes• Descending scans• CONNECT BY clauses

– Skip scanning is not supported by reverse key or bitmap indexes.

Page 26: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Index Scans

•Types of index scans:– Full scan (ha nem kell majd a táblát olvasni; egyesével olvassa a blokkokat)

– Fast-full index scan (ha minden keresett oszlop megvan az indexben; párhuzamosítható, több blokk olvasása egy időben)

– Index join (ha a join az indexek segítségével elvégezhető, és nem kell a táblát olvasni)

– Bitmap join (egy speciális ún. bitmap join index esetén)

Page 27: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Joining Multiple Tables

•You can join only two row sources at a time. Joins with more than two tables are executed as follows:1. Two tables are joined, resulting in a row source.

2. The next table is joined with the row source that results from step 1.

3. Step 2 is repeated until all tables are joined.

Page 28: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Join Terminology

– Join statement– Join predicate, nonjoin predicate– Single-row predicateSELECT c.cust_last_name,c.cust_first_name, co.country_id, co.country_nameFROM customers c JOIN countries co ON (c.country_id = co.country_id) AND ( co.country_id = '52790'OR c.cust_id = 205);

Join predicate

Nonjoin predicate

Single-row predicate

Page 29: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Join Terminology Natural join

Join with nonequal predicate

SELECT c.cust_last_name, co.country_nameFROM customers c NATURAL JOIN countries co;

SELECT s.amount_sold, p.promo_nameFROM sales s JOIN promotions p ON (s.time_id BETWEEN p.promo_begin_date AND p.promo_end_date );

Cross join

SELECT * FROM customers c CROSS JOIN countries co;

Page 30: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

SQL:1999 Outer Joins

– Plus (+) sign is not used.– The OUTER JOIN keyword is used instead.

SELECT s.time_id, t.time_idFROM sales sRIGHT OUTER JOIN times tON (s.time_id = t.time_id);

Page 31: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Full Outer Joins

– A full outer join acts like a combination of the left and right outer joins.

– In addition to the inner join, rows in both tables that have not been returned in the result of the inner join are preserved and extended with nulls.

SELECT c.cust_id, c.cust_last_name , co.country_nameFROM customers cFULL OUTER JOIN countries coON (c.country_id = co.country_id);

Page 32: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Join Order Rules

Rule 2

For outer joins, the table with the outer-joined table must come after the other table in the join order for processing the join.

Rule 1

A single-row predicate forces its row source to be placed first in the join order.

Az a tábla a vezető tábla (külső ciklus), amelynek minden sorát meg kell őriznünk, hiszen csak a legvégén tudjuk, hogy egy sornak akadt-e párja. Ha nem akadt, akkor pár nélkül tesszük be az eredménybe.

Page 33: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Join Optimization

– As a first step, a list of possible join orders is generated.

– This potentially results in the following:

– Parse time grows factorially when adding tables to a join.

Number of Tables Join Orders---------------- -----------2 2! = 23 3! = 64 4! = 24

Page 34: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Join Methods

– A join operation combines the output from two row sources and returns one resulting row source.

– Join operation types include the following:• Nested loop join• Sort-merge join• Hash join

Page 35: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Nested Loop Joins

– One of the two tables is defined as the outer table (or the driving table).

– The other table is called the inner table.– For each row in the outer table, all matching

rows in the inner table are retrieved.

For each row in the outer table

For each row in the inner table

Check for a match

Page 36: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Nested Loop Join Plan

Nested loops

Table access(Outer/driving table)

Table access(Inner table)

1

2 3

Page 37: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

When Are Nested Loop Joins Used?

• Nested loop joins are used when:– Joining a few rows that have a good driving condition– Order of tables is important– USE_NL(table1 table2)hint is used

Page 38: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Hash Joins

• A hash join is executed as follows:– Both tables are split into as many partitions as required,

using a full table scan.– For each partition pair, a hash table is built in memory

on the smallest partition.– The other partition is used to probe the hash table.

Page 39: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Sort-Merge Joins

• A sort-merge join is executed as follows:1. The rows from each row source are sorted

on the join predicate columns.

2. The two sorted row sources are then mergedand returned as the resulting row source.

Page 40: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Sort-Merge Join PlanMerge

Sort Sort

Table access Table access

1

2 3

4 5

Page 41: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

How the Query Optimizer Chooses Execution Plans for Joins

•The query optimizer determines: – Row sources– Type of join– Join method– Cost of execution plans– Other costs such as:

• I/O

• CPU time• DB_FILE_MULTIBLOCK_READ_COUNT

– Hints specified

Page 42: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Subqueries and Joins

– Subqueries (like joins) are statements that reference multiple tables

– Subquery types:• Noncorrelated subquery

• Correlated subquery• NOT IN subquery (antijoin)• EXISTS subquery (semijoin)

Page 43: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Sort Operations

– SORT UNIQUE– SORT AGGREGATE– SORT GROUP BY – SORT JOIN– SORT ORDER BY

Page 44: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Tuning Sort Performance

– Because sorting large sets can be expensive, you should tune sort parameters.

– Note that DISTINCT, GROUP BY, and most set operators cause implicit sorts.

– Minimize sorting by one of the following:• Try to avoid DISTINCT and GROUP BY.• Use UNION ALL instead of UNION.• Enable index access to avoid sorting.

Page 45: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Top-N SQL

SELECT *FROM (SELECT prod_id , prod_name , prod_list_price , prod_min_price FROM products ORDER BY prod_list_price DESC)WHERE ROWNUM <= 5;

Nem kell a teljes halmazt rendezni, elég az 5 legkisebbet benntartani a memóriában.

Page 46: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Memory and Optimizer Operations– Memory-intensive operations use up work areas in

the Program Global Area (PGA).– Automatic PGA memory management simplifies and

improves the way PGA memory is allocated.– The size of a work area must be big enough to avoid

multipass execution.– A reasonable amount of PGA memory allows single-

pass executions.– The size of PGA is controlled with:

• PGA_AGGREGATE_TARGET • WORKAREA_SIZE_POLICY

Page 47: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Optimizer Hints: Overview

•Optimizer hints: – Are used to alter execution plans– Influence optimizer decisions – Provide a mechanism to instruct the optimizer to choose

a certain query execution plan

Page 48: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Hint Categories

There are hints for:– Optimization approaches and goals– Access paths– Query transformations– Join orders– Join operation– Parallel execution

Page 49: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Optimization Goals and Approaches

Instructs Oracle Server to optimize an individual SQL statement for fast response

FIRST_ROWS(n)

Chooses cost-based approach with a goal of best throughput

ALL_ROWS

Page 50: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Hints for Access Paths

Performs a full table scanFULL

Explicitly chooses a bitmap access pathINDEX_COMBINE

Scans an index in ascending orderINDEX_ASC

Scans an index in ascending orderINDEX

Accesses a table by ROWIDROWID

Page 51: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Hints for Access Paths

INDEX_JOIN Instructs the optimizer to use an index join as an access path

INDEX_DESC Chooses an index scan for the specified table

INDEX_FFS Performs a fast-full index scan

NO_INDEX Disallows using a set of indexes

AND_EQUAL Merges single-column indexes

Page 52: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Hints for Query Transformation

USE_CONCAT Rewrites OR into UNION ALL and disables INLIST processing

NO_EXPAND Prevents OR expansions

MERGE Merges a view for each query

NO_MERGE Prevents merging of mergeable views

STAR_TRANSFORMATION

Makes the optimizer use the best plan in which the transformation can be used

FACT Indicates that the hinted table should be considered as a fact table

NO_FACT Indicates that the hinted table should not be considered as a fact table

Page 53: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Hints for Join Orders

ORDERED Causes the Oracle Server to join tables in the order in which they appear in the FROM clause

LEADING Uses the specified table as the first table in the join order

Page 54: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Hints for Join Operations

USE_NL Joins the specified table using a nested loop join

NO_USE_NL Does not use nested loops to perform the join

USE_MERGE Joins the specified table using a sort-merge join

NO_USE_MERGE Does not perform sort-merge operations for the join

USE_HASH Joins the specified table using a hash join

NO_USE_HASH Does not use hash join

Page 55: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Hints for Suppressing Index Usage

Hint Description

NO_INDEX Disallows use of any indexes

FULL Forces a full table scan

INDEX or INDEX_COMBINE

Forces the optimizer to use a specific index or a set of listed indexes

Page 56: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Materialized View Hints

REWRITE Rewrites a query in terms of materialized views

REWRITE_OR_ERROR Forces an error if a query rewrite is not possible

NO_REWRITE Disables query rewrite for the query block

Page 57: Connecting to an Instance UserServer User Client UserServer Oracle database ServerApplication server Browser.

Specifying a Full Set of Hints

SELECT /*+ LEADING(e2 e1) USE_NL(e1) INDEX(e1 emp_emp_id_pk) USE_MERGE(j) FULL(j) */ e1.first_name, e1.last_name, j.job_id, sum(e2.salary) total_salFROM hr.employees e1,hr.employees e2,hr.job_history jWHERE e1.employee_id = e2.manager_idAND e1.employee_id = j.employee_idAND e1.hire_date = j.start_dateGROUP BY e1.first_name, e1.last_name, j.job_idORDER BY total_sal;