Tuesday, October 11, 2022

Is flushing shared pool and buffer cache is good?

Flushing shared pool and buffer cache is not recommended and we should not perform on PROD environment

Flush Shared pool meaning flushing cached execution plan and SQL Queries from memory.
Flush buffer cache meaning flushing cached data from memory.

Database restart which internally flush both shared pool and buffer cache.

Flushing the data buffer cache & Shared pool is not recommend on Production Environment.
It may lead to increase the performance overhead, especially on RAC databases.

Flush buffer cache may lead to disk I/0 overhead.

https://docs.oracle.com/database/121/ARPLS/d_result_cache.htm#ARPLS202

1. Before flushing shared pool and buffer cache 

[root@oraclelab1 ~]# su - oracle
Last login: Fri Aug 26 18:47:01 IST 2022 on pts/1
[oracle@oraclelab1 ~]$
[oracle@oraclelab1 ~]$ ps -ef|grep smon
oracle    4467     1  0 Aug09 ?        00:00:19 ora_smon_DEVDB
[oracle@oraclelab1 ~]$
[oracle@oraclelab1 ~]$ . oraenv
ORACLE_SID = [oracle] ? DEVDB
The Oracle base has been set to /u01/app/oracle
[oracle@oraclelab1 ~]$ sqlplus / as sysdba

SQL*Plus: Release 19.0.0.0.0 - Production on Sat Aug 27 01:56:44 2022
Version 19.14.0.0.0

Copyright (c) 1982, 2021, Oracle.  All rights reserved.

Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.14.0.0.0

SQL> select count(*) from v$sql;

  COUNT(*)
----------
      7049

SQL> exec dbms_result_cache.memory_report;

PL/SQL procedure successfully completed.

SQL> set serveroutput on;
SQL> exec dbms_result_cache.memory_report;
R e s u l t   C a c h e   M e m o r y   R e p o r t
[Parameters]
Block Size          = 1K bytes
Maximum Cache Size  = 18048K bytes (18048 blocks)
Maximum Result Size = 902K bytes (902 blocks)
[Memory]
Total Memory = 800200 bytes [0.085% of the Shared Pool]
... Fixed Memory = 12424 bytes [0.001% of the Shared Pool]
... Dynamic Memory = 787776 bytes [0.084% of the Shared Pool]
....... Overhead = 165184 bytes
....... Cache Memory = 608K bytes (608 blocks)
........... Unused Memory = 12 blocks
........... Used Memory = 596 blocks
............... Dependencies = 11 blocks (11 count)
............... Results = 585 blocks
................... SQL     = 3 blocks (3 count)
................... PLSQL   = 6 blocks (6 count)
................... Invalid = 576 blocks (576 count)

PL/SQL procedure successfully completed.

2. After flushing shared pool and buffer cache

SQL> startup force;
ORACLE instance started.

Total System Global Area 3690985848 bytes
Fixed Size                  8903032 bytes
Variable Size             989855744 bytes
Database Buffers         2684354560 bytes
Redo Buffers                7872512 bytes
Database mounted.
Database opened.
SQL> select count(*) from v$sql;

  COUNT(*)
----------
       445

SQL> set serveroutput on;
SQL> exec dbms_result_cache.memory_report;
R e s u l t   C a c h e   M e m o r y   R e p o r t
[Parameters]
Block Size          = 1K bytes
Maximum Cache Size  = 18048K bytes (18048 blocks)
Maximum Result Size = 902K bytes (902 blocks)
[Memory]
Total Memory = 202648 bytes [0.022% of the Shared Pool]
... Fixed Memory = 5848 bytes [0.001% of the Shared Pool]
... Dynamic Memory = 196800 bytes [0.021% of the Shared Pool]
....... Overhead = 164032 bytes
....... Cache Memory = 32K bytes (32 blocks)
........... Unused Memory = 25 blocks
........... Used Memory = 7 blocks
............... Dependencies = 5 blocks (5 count)
............... Results = 2 blocks
................... Invalid = 2 blocks (2 count)

PL/SQL procedure successfully completed.

SQL>

Manually flush buffer cache & shared pool cache without bouncing the database:
Standalone Database:
alter system flush buffer_cache;
alter system flush shared_pool;

RAC Database:
alter system flush buffer_cache global;
alter system flush shared_pool global;

Regards,
Mallik

Check or get the Execution plan from SQL ID

Get the Execution plan from SQL ID!!!

Check the Execution plan from SQL ID!!!


High level steps

1. Get the SQL ID for the SQL Statement
2. Get the explain plain or execution plan for the SQL ID.


1. Get the SQL ID for the SQL Statement

- First you need to execute the SQL statement to get the SQL ID from v$sql view.
- You can get the SQL ID from AWR report or ASH report or you can get from the current session 

SQL> set pages 1000 lines 1000
SQL> SELECT * FROM EMP;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7839 KING                    17-NOV-81       5000                    10
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        950                    30
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

14 rows selected.
SQL> 

SQL> select sql_id from v$sql where sql_text like 'SELECT * FROM EMP';
SQL_ID
-------------
4ttqgu8uu8fus

2. Get the explain plain or execution plan for the SQL

SQL> select sql_id from v$sql where sql_text like 'SELECT * FROM EMP';

SQL_ID
-------------
4ttqgu8uu8fus

SQL> set line 200 pages 200
SQL> SELECT * FROM TABLE (DBMS_XPLAN.DISPLAY_CURSOR ('&sqlid'));
SQL> Enter value for sqlid: 4ttqgu8uu8fus
old   1: SELECT * FROM TABLE (DBMS_XPLAN.DISPLAY_CURSOR ('&sqlid'))
new   1: SELECT * FROM TABLE (DBMS_XPLAN.DISPLAY_CURSOR ('4ttqgu8uu8fus'))

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------

SQL_ID  4ttqgu8uu8fus, child number 0
-------------------------------------
SELECT * FROM EMP

Plan hash value: 3956160932

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |       |       |     3 (100)|          |
|   1 |  TABLE ACCESS FULL| EMP  |    14 |   532 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------
13 rows selected.
SQL>

Demo 2: (Best Query)

SQL> SELECT * FROM EMP where EMPNO=7839;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7839 KING       PRESIDENT            17-NOV-81       5000                    10

SQL> select sql_id from v$sql where sql_text like 'SELECT * FROM EMP where EMPNO=7839';

SQL_ID
-------------
dm0ndwh8c28aj

SQL> set line 200 pages 200
SQL> SELECT * FROM TABLE (DBMS_XPLAN.DISPLAY_CURSOR ('&sqlid'));
Enter value for sqlid: dm0ndwh8c28aj
old   1: SELECT * FROM TABLE (DBMS_XPLAN.DISPLAY_CURSOR ('&sqlid'))
new   1: SELECT * FROM TABLE (DBMS_XPLAN.DISPLAY_CURSOR ('dm0ndwh8c28aj'))

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID  dm0ndwh8c28aj, child number 0
-------------------------------------
SELECT * FROM EMP where EMPNO=7839

Plan hash value: 2949544139

--------------------------------------------------------------------------------------
| Id  | Operation                   | Name   | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |        |       |       |     1 (100)|          |
|   1 |  TABLE ACCESS BY INDEX ROWID| EMP    |     1 |    38 |     1   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN         | PK_EMP |     1 |       |     0   (0)|          |
--------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
   2 - access("EMPNO"=7839)
19 rows selected.
SQL>

Demo 3: (Bad Query)

SQL> set line 200 pages 200
SQL> SELECT * FROM TABLE (DBMS_XPLAN.DISPLAY_CURSOR ('&sqlid'));
Enter value for sqlid: 071z8v8jqhyx4
old   1: SELECT * FROM TABLE (DBMS_XPLAN.DISPLAY_CURSOR ('&sqlid'))
new   1: SELECT * FROM TABLE (DBMS_XPLAN.DISPLAY_CURSOR ('071z8v8jqhyx4'))

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQL_ID  071z8v8jqhyx4, child number 0
-------------------------------------
SELECT * FROM EMP where ENAME='KING'

Plan hash value: 3956160932

--------------------------------------------------------------------------
| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |      |       |       |     3 (100)|          |
|*  1 |  TABLE ACCESS FULL| EMP  |     1 |    38 |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter("ENAME"='KING')
18 rows selected.
SQL>

Regards,
Mallik

Automation Script | Archivelog Generation Hourly Monitoring

1. List out all the running databases and pic one database where we want to monitore the archive log generation from last 1 month. [oracle@o...