Saturday, June 27, 2020

Database Buffer Cache

Database Buffer Cache

Oracle database buffer cache is typically the largest portion of the SGA. It has data that comes from the files on disk. Because accessing data from disk is slower than from memory, The main purpose of the database buffer cache is to cache the data in memory for quicker access.

Database Buffer cache:

  • Stores copies of data blocks that have been retrieved from the datafiles
  • Enables great performance gains when you obtain and update data
  • Managed through an LRU  algorithm.
  • DB_BLOCK_SIZE determines primary Block size.
When a query is processed, the oracle server process looks in the Database buffer cache for any blocks if needs. If the block is not found in the database buffer cache, the server process reads the block from the data file and places a copy in the database buffer cache. Because subsequent requests for the same block may find the block in memory, then the requests may not require physical reads. The Oracle Server uses an LRU algorithm to age out buffers that have not been accessed recently to make some room for new blocks in the Database Buffer Cache.

The database buffer cache consists of independent sub caches. 

  • DB_CACHE_SIZE
  • DB_KEEP_CACHE_SIZE
  • DB_RECYCLE_CACHE_SIZE

These Independent sub cache can be resized dynamically as below,

         ALTER SYSTEM SET DB_CACHE_SIZE = 100M;

DB_CACHE_ADVICE set to gather statistics for predicting different cache size behavior.

Statistics displayed by V$DB_CACHE_ADVICE

 

Sizing of the Database Buffer Cache: 

The size of each buffer in the Database Buffer Cache is equal to the size of an oracle block, and it is specified by the DB_BLOCK_SIZE parameter. The Database buffer cache consists of independent sub caches for buffer pools and for multiple block sizes. The Parameter DB_BLOCK_SIZE  determines the primary block size, which is used for the SYSTEM tablespace.

Three  parameters define the sizes of the Database Buffer Caches:  

  • DB_CACHE_SIZE: Using this parameter, Sizes the default buffer cache buffers only with the primary block size (The Block size defined by DB_BLOCK_SIZE parameter)  and it always exists and can not be set Zero.
  • DB_KEEP_CACHE_SIZE: Using this Parameter sizes keep buffer cache, which is used to retain blocks in the memory that are likely to be reused.
  • DB_RECYCLE_CACHE_SIZE: By using this parameter, Sizes the recycle buffer cache, which is used to eliminate blocks from memory that have little chance of being reused.


Buffer Cache Advisory

The buffer cache advisory feature enables and disables statistics gathering for predicting behavior with different cache sizes. The Information provided by these statistics can help you to determine the size of  Database Buffer Cache optimally for a given workload.

The Buffer Cache Advisory information is collected and displayed through the V$DB_CACHE_ADVISE View.

The Buffer Cache  Advisory is enabled via the DB_CACHE_ADVICE   initialization parameter. It is a dynamic parameter that can be altered using ALTER SYSTEM command. Three Values  (OFF, ON, READY) is available.

 

DB_CACHE_ADVICE  parameter values

OFF: Advisory is turned off and the memory for the advisory is not allocated.

ON:  Advisory is turned on and both CPU and memory overhead is incurred.

Note: Attempting to set the parameter to the ON state when it is in the OFF state may lead to the Fallowing Error: 

ORA-4031  Inability to allocate memory from the Shared Pool when the parameter switched to turned ON.

If The parameter is in a READY state it can be set to ON without error because the memory is already allocated.

READYAdvisory is turned off but the memory for the advisory remains allocated. Allocating the memory before the advisory is actually turned ON  and it will avoid the risk of ORA - 4031.  If the Parameter is Switched to this state from OFF, It Is Possible that an ORA-4031 will be raised.

 

Thank you !!


Wednesday, June 17, 2020

Shared Pool

Hey Folks,

Here we shall review about Shared Pool :

Shared Pool : Shared pool is a part  of SGA and it is used to store :

  • Most recently executed SQL statement
  • Most recently used data definitions 

It consist of three key performance - related memory structures :

  • Library Cache
  • Data Discretionary cache
  • Result cache

Shared pool sized by the Parameter " SHARED_POOL_SIZE "

           ALTER SYSTEM SET SHARED_POOL_SIZE = 64M;

The shared pool environment contains both fixed and variable structure. The fixed structures remains relatively the same size whereas the variable structure grows and shrink based on user and program requirements. the actual sizing for the fixed and variable structure is based on an initialization parameter and the work of an Oracle Internal algorithms.

The size of shared pool as total size determined by the initialization parameter SHARED_POOL_SIZE. The Default value of the parameter is 8 M on 32 - bit platforms and 64 M on 64 - bit platforms. Increasing the value of this parameter is increases the amount of memory reserved   for the shared pool.

In the shared pool have many caches including the Library Cache and the Data Dictionary Cache automatically increase and decrease in size as needed. Old entries are aged out to accommodate new entries when shared pool runs out of space.

Cache Miss on the library cache or Data Dictionary Cache is more more expensive than a miss on database buffer cache. For this reason the shared pool should be sized to ensure that frequently used data is cached.

Large memory allocation required in shared pool for several features, such as shared server, Parallel Query OR  Recovery Manager. Oracle Recommends  to use a separate memory area, that is called  "large pool" to protect the System Global Area (SGA) memory used by  these features. 

Sometime  make memory allocation out of the shared pool for  JAVA, PL/SQL, Or SQL cursors larger than 5 KB. To Enable these allocations to occur more efficiently Oracle Database preserve a small amount of shared pool memory and that preserved memory is called  Reserved Pool. It is Used if the shared pool Runs out of Space.

Sizing  the Shared Pool :

Shared Pool consist of three important memory structure:

  1. Library Cache Library Cache is part of shared pool in the SGA and it Plays the important role and special functionality as below 
  • Stores information about the most recently used SQL and PL/SQL Statements
  • Enables the sharing of commonly used statements
  • It is managed by least Recently used (LRU) algorithm
  • Consist of two structures

            --> Shared SQL area

            --> Shared PL/SQL  area

  • Size determined by shared pool sizing

The Library cache stores executable forms of SQL  cursors, PL/SQL programs, and JAVA Classes, Which are collectively referred to as the application code.

When the application code is executed and parsed or program unit is called the memory is allocated. If the size is too small, Statements are continually reloaded into the library cache and which affect performance. The library cache is managed by an LRU algorithm.

Oracle database attempts to reuse existing code if it has been executed previously and can be shared. If the parsed representation of the SQL statement exists in library cache and it can be shared, then the database reuses the existing code. This is Known as a Soft Parse, Or a Library Cache Hit.

If Oracle Database cannot use the existing code, Then database must build a new executable version of the application code. This is known as Hard Parse or, a Library Cache Miss. 

In Order to Perform a Hard Parse Oracle Database uses more resources than during a Soft Parse. Resources used for a Soft Parse include CPU and Library Cache Latch Gets. Resources required for Hard Parse Include additional CPU, Library Cache Latch Gets and the Shared Pool Latch Gets. A Hard Parse may occur on either the parse step or the execute step When processing a SQL Statement.

When the Library Cache fills  then the Less/least used execution paths and Parse tress are removed from the Library Cache to make rooms for the new Entries. If the SQL or PL/SQL statements are not reused then they are eventually aged out, Then Oracle Database implicitly re-parses the statement to create a new Shared SQL area for it, and execute the statement. This also results in a Hard Parse.  To reduce Library Cache Misses on execution calls, allocate more memory to the Library Cache.

When an application makes a parse call for a SQL statement, if the parsed representation of the statement does not exist in the Library Cache, then Oracle Database Parse the statement and stores the Parsed form in the Shared Pool. To Reduce the Library Cache Misses on Parse calls, ensure that all the shareable SQL statements are stored in the Shared Pool whenever Possible.

The Library Cache consist of two structures:

  • Shared SQLThe shared SQL area stores and shares the Execution Plan and Parse Tree for SQL Statements runs against the Database. If the Second time, Same SQL statements executes, then it is able to take advantage of the Parse information available in the shared SQL area to faster its executions. To make an assurance that the SQL statements  use a  shared SQL area whenever possible and for that  The Text, Schema and bind variables must be exactly the same.
  • Shared PL/SQL : The Shared PL/SQL area stores the shares the most recently executed PL/SQL Statements. Parsed and Complied Program Unit and Procedures ( Functions, Packages, Triggers) are stored in this area.

   2.  Data Dictionary Cache :

The Second major Cache area within the Shared Pool is the Data Dictionary Cache. The Data Dictionary Cache holds the internal data Structure of the Oracle Database.

The Data Dictionary is accessed very often by various Oracle Processes. Dictionary Cache is the memory area that is designed to hold the  Database Data dictionary data. It is also known as the ROW Cache Because it holds the data in ROWs format instead of buffers.

When an Oracle Database is first started, No data exists in  Data Dictionary Cache. The Data Dictionary Cache started to holds the dictionary data after the Database Startup  and  it is up and running for a period of time. It is used for storing the most recently used data definition in the Oracle Database.  These Data Definitions may include information about, Data Files, tables, Indexes, Privileges, Users etc, and any DML operation performed.

Caching these information in memory improves the performance especially for queries and updates using DML. During the parsing phase, the server process scans this memory structure to resolve the database object names and validate access.

Data dictionary Cache hold the information about Oracle Database is as below:

  • A Collection of the most recently used definitions in the database
  • Include information about database files, tables, indexes, columns, users, privileges, and other database objects
  • During the parse phase, the server process looks at the data dictionary for information to resolve object names and validate access
  • Caching data dictionary information into memory improves response time on queries on DML
  • Size determined by the Shared Pool Sizing

The data dictionary cache is also referred to as the dictionary cache or Row cache. Information about the database (User Account Data, Data File names, Segment names, Extent Locations, Table descriptions and user privileges) is stored in the data dictionary tables. When this Information is needed by the server, the data dictionary tables are read, and the data that is returned is stored in the data dictionary cache.

We can reduce the contention of Data dictionary cache by using locally managed tablespace (LMT)  instead of using dictionary managed tablespace (DMT). 

The data dictionary cache misses is the ratio of the number of data dictionary request resulting in cache misses (Requested Data dictionaries information that's are not found in data dictionary cache). The data dictionary cache holds the data dictionary data that is used and heavily referenced during SQL pre-execution processing and it improves the SQL query performance.

If you are having difficulties regarding the data dictionary cache misses, note:

"If the Miss Percentage above is greater than 10-15, you may need to tune your library cache usage by adding additional memory to the data dictionary cache. Increasing memory is done by increasing the SHARED_POOL_SIZE parameter in the INITsid.ora file.

Sizing of the data Dictionary :

The overall size is dependent on the size of the shared pool size and is Managed internally by the database. If the Data Dictionary Cache is too Small, then the database has to query the data dictionary tables repeatedly for information is  needed by the server. These queries are called recursive calls and are slower than the direct query on the data dictionary cache because direct queries do not use SQL.

3. Result Cache :

This Features is introduced and included in Shared pool of an SGA from oracle 11G Release. The result cache has received an attention due the capability to cached  SQL query and PL/SQL functions results in memory.

When a query is executed for the very first time, the user’s process searches for the data in the database buffer cache. If data is there (because someone else had retrieved this data before), it uses it; otherwise, it performs an I/O operation to retrieve data from the datafile on disk into the buffer cache, and from this data, the final result set is built.

Subsequently, if another query requires the same data set, the process uses the data from the buffer cache to build the result set required by the user. Well, here the result cache could be called a cache area, in this case, the shared pool. So, the Result Cache is an area in the shared pool and contains the end results of a query execution.

E.g. : Suppose one user executed a query and get the final result of 300 rows.  If another user executed the same query and then The user session will have to traverse through all these rows in the buffer cache once again to get the final result of 300 rows. So the result cache is introduced and using this result cache to get directly to those 300 rows the second time and every time going forward.

E.g. : Suppose one user executed a query and get the final result of 300 rows.  If another user executed the same query and then The user session will have to traverse through all these rows in the buffer cache once again to get the final result of 300 rows. So the result cache is introduced and using this result cache to get directly to those 300 rows the second time and every time going forward.

Now Question Comes that how the result cache will use when same query executed again and again. So for that use of result cache and get the faster result of the same query, We have to use OR pass RESULT_CACHE hint in the query.

SELECT /*+ RESULT CACHE */ SUM(Sal) FROM HR.employees WHERE DEPTNO=45;

When a query with  RESULT_CACHE  hint is run. Oracle Will  see if the Results of the query have already been Executed, Computed, and Cached and If so, Retrieve the data from the cache instead of querying the data blocks and computing the results again.  Take the fallowing important points into consideration.

  • The result Cache feature is useful  only for SQL queries that are executed over and over again frequently.
  • the underlying data doesn't change very often. When The data changes, the result set is removed from the cache

If you are executing the same queries over and over, using the RESULT_CACHE hint often makes subsequent queries run faster.

By default, the result_cache_max_size parameter is about 0.25% of the memory_target parameter or 1% of the shared_pool parameter. 

This parameter can be modified to control how much is stored in the Result Cache. The result_cache_max_result parameter specifies what percentage of result_cache_max_size a single Result Cache can use.  The default value is 5%.

Each result set is identified in the cache using a CACHE_ID, which is a 90-character long string. The CACHE_ID for a query does not match the SQL_ID used to identify the query in the library cache and contained in V$SQL. Unlike the SQL_ID, which is generated for every SQL query executed against an Oracle database, the CACHE_ID is for an area or bucket in the Result Cache section of the shared pool that stores the end result of the query.


Thank You!!


Monday, June 15, 2020

Memory Architecture

Hey Folks,

Here we shall know very little and important Facts about memory architecture.

When we talk about memory architecture of an ORACLE then it comes in our mind that the memory architecture is SGA (Shared Global Area) and PGA (Program Global Area) and  it comes Collectively in Memory architecture of an ORACLE.  

Here we shall know about  the what kind of information hold by the memory architecture in an ORACLE.

Oracle uses  memory to store information at run time and such is fallowing.

  • Program Code
  • Information about a connected session even if it is not currently active
  • Information About the program during the program Execution.  For e.g.,  the current state of a query from which rows are bring fetched.
  • Information that is shared and communicated among oracle processes. For e.g.  Locking Information.
  • Cached data that is also permanently stored on peripheral memory . For e.g. Data Block and redo Log Entries.
The Basic memory structures associated with oracle is :
  • System Global Area (SGA) : Which is Shared by all server and background processes.
  • Program Global Area (PGA) : Which is private to each server and background Process and there  is one PGA for an each processes.

System Global Area (SGA) :

A system global area  (SGA) is a group of shared memory architecture that contain data and control information  for one Oracle Database  Instance.

If  Multiple Users are concurrently connected to the same instance's SGA is shared among the users.

Memory allocations happens to the SGA from the RAM memory of the systems or Servers. When instance starts the SGA reserves memory from the RAM. When Instance Shut down then SGA allocated memory released automatically.

An SGA and oracle process collectively make up an oracle instance. Oracle Automatically allocates memory for an SGA when you start an instance, and the operating system reclaims the memory when you shutdown  the instance. Each instance has its own SGA.

The SGA is  Read/Write.  All users connected to a multiple process,  database instance can read Information contained within the instance's SGA, and Several processes write to the SGA during execution of an Oracle.

Program Global Area (PGA) :

A program Global area (PGA) is a memory region  that contain data and control information for a server process. 

When a user makes a request to connect with an instance then the server process start to communicate to instance on behalf of user process and for that server process PGA memory allocated. PGA allocates for each and every server process separately. PGA memory also allocates from the system/Servers RAM  memory.  

When Session is started then memory reserves for that session from the RAM and when the session is closed then memory is released itself for that particular session.

It is non shared memory created by Oracle when server process is started.  Only a Server process can  access the PGA. Memory Allocated for PGA  that is memory allocated from outside the Oracle Instance Memory.

Oracle Database Reads and Writes  information in the PGA on the behalf of the server process. One PGA exists for each server processes and each background processes. It Stores data and control information for a single server Process or a Single background process.


Thank You !!.

Please keep visiting this blog for more informative posts.