38
9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Embed Size (px)

Citation preview

Page 1: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

9Copyright © 2006, Oracle. All rights reserved.

Tuning the Buffer Cache

Page 2: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-2

Objectives

After completing this lesson, you should be able to do the following:

• Describe the buffer cache architecture

• Size the buffer cache

• Resolve common performance issues related to the buffer cache

• Use common diagnostic indicators to suggest a possible solution

Page 3: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-3

Spfile

Temp

Data file

Undo

Oracle Database Architecture

PMON SMON RECO MMON

MMNL

PSP0

MMAN DBWn LGWR CKPT

CJQ0

S000

D000QMNC

QnnnFMON

ARCnCTWR

RVWR

Fixedsize

Sharedpool

Largepool

Javapool

Streamspool

Defaultbuffercache

Keepbuffercache

Recyclebuffercache

nKbuffer

caches

Redolog

buffer

ASHbuffer

Sortextentpool

Globalcontext

pool

SGA

Flashbackbuffer

Instance

Flashbacklogs

Redo logfiles

Archivelog files

Controlfiles

SYSTEM

SYSAUXChangetracking

file

Passwordfile

Page 4: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-4

Buffer Cache: Highlights

• Scalable architecture:– Multiversion concurrency control– Proprietary LRU-based replacement policy– Cache fusion

• Incremental checkpointing mechanism

• Advisors

• Private pool for I/O intensive operations

Page 5: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-5

Database Buffers

SGA

DB buffer cache

DBWn

Server

Page 6: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-6

Buffer Hash Table For Lookups

Hashbuckets

table

Cachebufferchainslatches

Bufferhash

chains

CR clones

Buffer headers

A

B

C

C

E

F

Buffercache

C C E

A B

F

Page 7: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-7

Working Sets

Default Keep RecyclenK Buffer

cache

C

A

B

A

C

Working set 1 Working set 2

Auto-tuned

…D

F

E

D

F

B

D

E

F

Buffercache

LRU CKPTs LRU CKPTs

C

A

Page 8: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-9

Tuning Goals and Techniques

• It is recommended to use Automatic Shared Memory Management (ASMM).

• Tuning goals:– Servers find data in memory– No waits on the buffer cache

• Diagnostic measures:– Wait events– The V$DB_CACHE_ADVICE view and cache hit ratio

• Tuning techniques:– Review ADDM recommendations if ASMM is used.– Reduce the number of blocks required by SQL

statements.– Increase buffer cache size, use multiple buffer pools.

Page 9: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-11

Symptoms

The symptoms that indicate a buffer cache problem:

• Latch:cache buffer chains

• Latch:cache buffer LRU chains

• Buffer busy waits

• Read waits

• Free buffer waits

• Cache hit ratio

Page 10: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-12

Cache Buffer Chains Latch Contention

Contention for this latch indicates:

• Multiple processes attempting to access the same “hot” block

• Excessive block replacement

Page 11: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-13

Finding Hot Segments

• Characteristics of cache buffer chains latch contention: – Many accesses to one or more block under the

same latch – Worse with larger block sizes

• To find hot segments:

SQL> SELECT * FROM ( SELECT owner, object_name, 2 object_type, statistic_name, sum(value) 3 FROM V$SEGMENT_STATISTICS 4 GROUP BY owner, object_name, object_type, 5 statistic_name 6 ORDER BY SUM(value) DESC) 7 WHERE ROWNUM < 10;

Page 12: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-14

Buffer Busy Waits

• Application-level contention for buffers in the buffer cache

• Identify buffer busy waits contention:

SELECT object_name, valueFROM V$SEGMENT_STATISTICSWHERE statistic_name 'buffer busy waits' AND value > 20000;

SELECT class, countFROM V$WAITSTATWHERE count>0ORDER BY count DESC;

Page 13: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-15

Calculating the Buffer Cache Hit Ratio

SELECT name, valueFROM v$sysstatWHERE name IN ('db block gets from cache', 'consistent gets from cache', 'physical reads cache');

PHYSICAL READS = 'physical reads cache')

LOGICAL READS = ('consistent gets from cache' + 'db block gets from cache')

HIT RATIO = 1 - PHYSICAL READS/LOGICAL READS

Page 14: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-16

Buffer Cache Hit Ratio Is Not Everything

• A badly tuned database can still have a hit ratioof 99% or better.

• Hit ratio is only one part in determining tuning performance.

• Hit ratio does not determine whether a database is optimally tuned.

• Use the wait interface to examine whatis causing a bottleneck:– V$SESSION_WAIT– V$SESSION_EVENT– V$SYSTEM_EVENT

• Tune SQL statements.

Page 15: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-17

Interpreting Buffer Cache Hit Ratio

• Hit ratio is affected by data access methods:– Full table scans– Repeated scans of the same tables– Large table with random access– Data or application design

• Investigate increasing the cache size if:– Hit ratio is low– Application is tuned to avoid full table scans

Page 16: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-19

Read Waits

• List of wait events performing disk reads into the buffer cache:– db file sequential read– db file parallel read– db file scattered read

• If wait time for reads is high:– Tune the SQL statement that issues most disk

reads by sorting V$SQL by DISK_READS and BUFFER_GETS.

– Grow buffer cache if needed.– Reduce writes due to checkpointing.– Add more disk capacity.

Page 17: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-21

Free Buffer Waits

K

B

J

A

C

D

LRUlist

Server

Server

Server Write

Buffer

state

dirty

free

DBWn

Page 18: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-22

Solutions

The buffer cache solutions are applied depending on the symptoms:

• Properly size the buffer cache.

• Cache objects.

• Use the keep and recycle pools.

• Increase the writing speed of DBWn.

• Use private I/O pool.

Page 19: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-23

Sizing the Buffer Cache

Buffer cache size affects several tuning diagnostics. If the cache is too small:

• Extra reads due to block replacement

• Extra writes to move dirty blocks to disk

• Buffer cache LRU chains contention

Page 20: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-24

Buffer Cache Size Parameters

• Set the primary block size for the recycle, keep, and default buffer pools: DB_BLOCK_SIZE

• Set the size of the buffer pools:– DB_CACHE_SIZE– DB_KEEP_CACHE_SIZE– DB_RECYCLE_CACHE_SIZE

• Represent all memory for the buffer cache

• Are required to use buffer cache features:– Dynamic grow/shrink– Buffer cache advice– Multiple block sizes

Page 21: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-25

Dynamic Buffer CacheAdvisory Parameter

• The buffer cache advisory feature enables and disables statistics gathering for predicting behavior with different cache sizes.

• Use the information provided by these statistics to size the buffer cache optimally for a given workload.

• The buffer cache advisory is enabled by means of the DB_CACHE_ADVICE initialization parameter:– This parameter is dynamic and can be changed

using ALTER SYSTEM.– Three values are allowed: OFF, ON, and READY.

Page 22: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-26

View to Support Buffer Cache Advisory

• Buffer cache advisory information is collected inthe V$DB_CACHE_ADVICE view.

• The view contains different rows that estimate the number of physical reads for cache sizes between 10% and 200% of the current cache size.

• The rows also compute a physical read factor, which is the ratio of the number of estimated reads to the number of actual reads.

• Simulation is done for all buffer pools.

Page 23: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-27

Using the V$DB_CACHE_ADVICE View

SELECT size_for_estimate, buffers_for_estimate, estd_physical_read_factor, estd_physical_readsFROM V$DB_CACHE_ADVICEWHERE name = 'DEFAULT' AND advice_status = 'ON' AND block_size = (SELECT value FROM V$PARAMETER WHERE name = 'db_block_size');

Estd Phys Estd Phys Cache Size (MB) Buffers Read Factor Reads---------------- ------------ ----------- ------------(10%) 30 3,802 18.70 192,317,943… 243 30,416 1.33 13,720,149 273 34,218 1.13 11,583,180(Current) 304 38,020 1.00 10,282,475 334 41,822 .93 9,515,878… 577 72,238 .67 6,895,122(200%) 608 76,040 .66 6,739,731

Page 24: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-28

Using the Buffer Cache Advisory with EM

Page 25: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-29

Caching Tables

• Enable caching during full table scans by:– Creating the table with the CACHE clause

– Altering the table with the CACHE clause

– Using the CACHE hint in a query: select /* + cache */

• Caching tables put blocks at the MRU end of the LRU list.

• Guideline: Do not overcrowd the buffer cache.

• Use a keep pool.

Page 26: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-30

Multiple Buffer Pools

• Three buffer pools:– Default: SYS and nonflagged table or indexes– Keep: Hot objects– Recycle: Infrequent access

• Useful for small, simple schemas with well-known access paths

Default Keep Recycle

Auto-tuned

Page 27: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-32

Enabling Multiple Buffer Pools

• Use the BUFFER_POOL clause.

• This clause is valid for tables, clusters, and indexes.

• When altered, buffer pool is used for future reads.

• Objects can have more than one buffer pool.CREATE INDEX cust_idx … STORAGE (BUFFER_POOL KEEP …);

ALTER TABLE customer STORAGE (BUFFER_POOL RECYCLE);

ALTER INDEX cust_name_idx STORAGE (BUFFER_POOL KEEP);

Buffer pool 이 변경- 새로 읽어 들인 블록은 새로운 buffer pool 에 들어감- 이전에 남아 있던 블록은 유지 (flush out 될때까지 )

Page 28: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-33

Calculating the Hit Ratio for Multiple Pools

SQL> SELECT name, 1 - (physical_reads / 2 (db_block_gets + consistent_gets)) "HIT_RATIO" 3 FROM V$BUFFER_POOL_STATISTICS 4 WHERE db_block_gets + consistent_gets > 0;

NAME HIT_RATIO------------------ ----------KEEP .983520845RECYCLE .503866235DEFAULT .790350047

Page 29: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-35

Multiple Block Sizes

• Allow buffer caches for nonstandard block sizes• Parameters: DB_nK_CACHE_SIZE {n = 2, 4, 8, 16, 32}

• BLOCKSIZE attribute of CREATE TABLESPACE storage clause

• Intended for transportable tablespaces

Default

KeepRecycle

16 KB cache

Set

#1

Set

#2

Set

#3

Set

#4

Set

#5

Set

#6

Set

#7

Set

#8

Page 30: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-36

Multiple Database Writers

• Multiple database writers are a means to increase write throughput useful for large SMP systems.

• Buffer cache is partitioned between database writers by working sets.

• Each DBWn process scans its own assigned sets.

• The number of database writes can be manually controlled by DB_WRITER_PROCESSES.

Set

#1

Set

#3

Set

#2

Set

#4DBW0 DBW1

Page 31: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-37

Multiple I/O Slaves

• Allow DBW0 to write in parallelwhen asynchronous I/O is notwell supported

• DBW0 gathers batch of buffers

• Queues batch in round-robinorder with I/O slaves

• Waits for all slaves to complete

• Cannot combine multiple DBWnwith multiple I/O slaves

• Controlled by:– DBWR_IO_SLAVES– DISK_ASYNCH_IO

Set

#1

Set

#2

Set

#3

Set

#4

i000 i002

DBW0

i001

Page 32: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-38

Use Multiple Writers or I/O Slaves

To reduce free buffer waits:

• Implement asynchronous I/O first

• Use multiple writers when a database writer is consuming 100% of a CPU

• Use I/O slaves when asynchronous I/O is not supported

Page 33: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-39

Private Pool for I/O Intensive Operations

• Use for operations that:– Do not require caching– Require efficient I/O

• Use for large asynchronous I/O

• Requires global checkpoint to ensure consistent data

• Use only for direct path I/O:– Parallel direct inserts– SQL*Loader direct path : 직접 쓰기

Page 34: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-40

Automatically Tuned Multiblock Reads

• DB_FILE_MULTIBLOCK_READ_COUNT is automatically tuned.

• Simplifies the determination of the best value

• Optimal I/O size is platform dependent:– Cannot exceed 10% of the cache

Prefetch limited to 64 KB– Prevents swamping of the cache

• Automatically enabled if not set or set to zero

Page 35: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-41

Faster Instance Startup for Ultralarge Buffer Caches

• Database available at 10% initialization of buffer cache

• Remaining 90% formatted in the background by CKPT

• Leverages the dynamic buffer cache infrastructure

• Especially useful for very large buffer caches

Page 36: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-42

Flushing the Buffer Cache(for Testing Only)

ALTER SYSTEM FLUSH BUFFER_CACHE;

Page 37: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-43

Practice: Overview

This practice covers the following topics:

• Use the DB Buffer Cache Advisor to size the buffer cache

• Use the Keep Pool

Page 38: 9 Copyright © 2006, Oracle. All rights reserved. Tuning the Buffer Cache

Copyright © 2006, Oracle. All rights reserved.9-44

Summary

In this lesson, you should have learned how to:• Describe the buffer cache architecture

• Size the buffer cache

• Resolve common performance issues related to the buffer cache

• Use common diagnostic indicators to suggest a possible solution