Disclaimer

Thursday, 2 September 2021

PERFORMANCE TUNNING

PERFORMANCE TUNNING

 

How to generate explain plan?

set autotrace tracelonly explain.


How to check explain plan for past ran SQLs and currently running SQLs statements?

select * from TABLE(dbms_xplan.display_awr('&SQL_ID'));

select * from TABLE(dbms_xplan.display_cursor('&SQL_ID'));


How to check SQL  Execution Plan is in the Plan History or SQL Plan Baseline?

Select sql_id from v$sql where sql_text like ‘select%’;

To check plan is in the baseline:

select SQL_HANDLE, PLAN_NAME from dba_sql_plan_baselines where SQL_TEXT like '%SPM_TEST%'

Query the DBA_SQL_PLAN_BASELINES.ACCEPTED

If ACCEPTED=NO ,  the plan is in the history and will not be used by the optimizer and If ACCEPTED=YES the plan is in the Plane Baseline and will be used by the optimizer

Load plans from SQL SET

DBMS_SPM.LOAD_PLANS_FROM_SQLSET()

Load plans from Cursor Cache

DBMS_SPM.LOAD_PLANS_FROM_CURSOR(‘SQL_ID’)

To change the status of plans in SQL history to ACCPETED which in turn moves them to SQL BASELINES

DBMS_SPM.EVOLVE_SQL_PLANS_BASELINE()

Use display_sql_plan_baseline to see Baseline Plan

select * from table(dbms_xplan.display_sql_plan_baseline('&sql_handle','&plan_name','typical'));

 

What are the init parameters related to performance/optimizer?

To influence the behavior of optimizer you can set the following parameters:

optimizer_mode = choose

optimizer_index_caching = 90

optimizer_index_cost_adj = 25

optimizer_max_permutations = 100

optimizer_use_sql_plan_baselines=true

optimizer_capture_sql_plan_baselines=true

optimizer_use_pending_statistics = true;

optimizer_use_invisible_indexes=true

_optimizer_connect_by_cost_based=false

_optimizer_compute_index_stats= true;

db_file_multiblock_read_count=64

 

Why does the same query takes different amount of time to run?

There are three broad reasons that queries take longer at different times. Either you are getting different performance because the system is under a different sort of load, you are getting different performance because of data volume changes, or you are getting different performance because you are getting different query plans


Different Data Volume

When you generate your initial timings, are you using data volumes that are similar to the volumes that your query will encounter when it is actually run? If you test a query on the first of the month and that query is getting all the data for the current month and performing a bunch of aggregations, you would expect that the query would get slower and slower over the course of the month because it had to process more and more data. Or you may have a query that runs quickly outside of month-end processing because various staging tables that it depends on only get populated at month end. If you are generating your initial timings in a test database, you'l very likely get different performance because test databases frequently have a small subset of the actual production data.


Different System Load

Different Query Plans

Over time, your query plan may also change because statistics have changed (or not changed depending on the statistic in question). Normally, that indicates that Oracle has found a more efficient plan or that your data volumes have changed and Oracle expects a different plan would be more efficient with the new data volume. If, however, you are giving Oracle bad statistics (if, for example, you have tables that get much larger during month-end processing but you gather statistics when the tables are almost empty), you may induce Oracle to choose a very bad query plan. Depending on the version of Oracle, there are various ways to force Oracle to use the same query plan. If you can drill down and figure out what the problem with statistics is, Oracle probably provides a way to give the optimizer better statistics.

 

Consolidating Multiple Trace Files

Use the trcsess command to merge multiple trace files into a single trace file.

c:\trace> trcsess output=combined.trc session=196.614 orcl1_ora_8432_mytrace1.trc

orcl1_ora_8432_mytrace2.trc

The trcsess command shown here combines two trace files generated for a session into a single trace file. The session parameter identifies the session with a session identifier, consisting of the session index and session serial number, which you can get from the V$SESSION view.

Tracing a SQL Session

execute dbms_monitor.session_trace_enable(session_id=>138,serial_num=>242,waits=>true,binds=>false);

 

Setting a Trace in a Running Session

You can set a trace in a running session using the operating system process ID (PID), with the help of the

oradebug utility. Once you identify the PID of the session you want to trace, issue the following

commands to trace the session.

SQL> connect / as sysdba

SQL> oradebug setospid <SPID>

SQL> oradebug unlimit

SQL> oradebug event 10046 trace name context forever,level 12

SQL> oradebug event 10046 trace name context off

 

Why SQL statements regress?

Optimizer related-

->Stale/missing statistics, Improper optimizer configuration Upgraded DB to new optimizer, Changing statistics( refresh)  ,Changing data (plan do not scale with data)

,Bind sensitive SQL with bind peaking ,Not parallized (not scalling to large data) ,Improper parallized (skew plan)

Resource and contention issue related-

->Hardware resource crunch such as CPU, Memory, IO and network ,Data fragmentation ,Logical contention ,row lock or latch contention , Block update contention.

 

Database is Hung?

->Temp tbs is full and no space for sorting.

->Undo tbs is full and no space to generate redo.

->Archive log mount point or FRA full.

->Online redo log file is missed.

->Physical memory is exhausted or CPU utilization is high.

->No space left on $ORACLE_HOME mount point to generate audit trace file.

 

DATAPUMP


 DATAPUMP 

What are the differences between expdp and exp (Data Pump or normal exp/imp)?

Data Pump is server centric (files will be at server).

Data Pump has APIs, from procedures we can run Data Pump jobs.In Data Pump, we can stop and restart the jobs.

Data Pump will do parallel execution.Tapes & pipes are not supported in Data Pump.Data Pump consumes more undo tablespace.Data Pump import will create the user, if user doesn’t exist.

 

Why expdp is faster than exp (or) why Data Pump is faster than conventional export/import?

Data Pump is block mode, exp is byte mode.

Data Pump will do parallel execution.

Data Pump uses direct path API.

 

How to improve expdp/ impdp performance?

Using parallel option which increases worker threads. This should be set based on the number of cpus.

 

In Data Pump, where the jobs info will be stored (or) if you restart a job in Data Pump, how it will know from where to resume?

Whenever Data Pump export or import is running, Oracle will create a table with the JOB_NAME and will be deleted once the job is done. From this table, Oracle will find out how much job has completed and from where to continue etc.


How to import only metadata?

CONTENT= METADATA_ONLY


How to import into different user/tablespace/datafile/table?

REMAP_SCHEMA

REMAP_TABLESPACE

REMAP_DATAFILE

REMAP_TABLE

REMAP_DATA


How to append the table rows in the existing table?

TABLE_EXIST_ACTIONS=APPEND

 

Using Data Pump, how to export in higher version (11g) and import into lower version (10g), can we import to 9i?

Import data pump can always read export datapump dumpfile sets created by older versions of database. In your case it works, normal expdp on 10g and impdp on 11g

VERSION parameter in datapump is for other way around, if you want to import data taken from 11g into 10g database you need to specify VERSION while taking backup.

 





TKPROF (Trace Kernel Profile)

 

Tkprof command line options


  • explain ( must use username/password)
  • table (plan_table)
  • sys (statements issued against data dictionary)
  • sort (isolate sql statements )
  • print (limit number of sql statements to formatted trace file)
  • aggregate (should statistics from multiple user of same sql be clubbed together)
  • wait (wait statistics happened during trace)

 

Sorting options

 

  • Parse (syntax check, privileges …)
    • Prscnt = no. of times sql was parsed
    • Prscpu = amount of CPU spent for parsing sql
    • Prsels = amount of elapsed time spent for parsing sql
    • Prsdsk = no. of disk reads while sql was parsed
    • Prsqry = no. of “consistent gets” read while parsing
  • Execute (create table, alter table …)
    • Execnt
    • Execpu
    • Exeels
    • Exedsk
    • Exeqry = no. of “consistent gets” read while executing the SQL
  • Fetch (only for select)
    • Fchcnt
    • Fchcpu
    • Fchels
    • Fchdsk

 

tkprof ora_1234.trc trace.txt sys=no \ explain=shekhar/password print=10 \

insert=trace.sql sort=fchcpu

 

 

How to read formatted trace file

 

  • Count : number of time the phase (parse, execute, fetch occur)
  • CPU: CPU time (in seconds) used to the phase
  • Elapsed: elapsed time (in seconds) for the phase
  • Disk: no. of data blocks read from disk
  • Query: No of blocks read in “consistent get” mode
  • Current: No. of blocks read in “db block” mode
  • Rows: no. of rows affected by sql

 

What to look for in formatted trace file

 

SQL Statements that:

  • Consume excess cpu resources
  • Take long time to parse, execute or fetch
  • Read too many data blocks from disk and less from SGA
  • Access many data blocks but return few rows
  • Do not use bind variables
  • Look for utilization of indexes, FTS in Explain plan

 

When to use TKPROF

  • When a particular session is having performance issues
  • When you want to capture SQL statements from front-end app (especially purchased out-of-the-box systems)


How to create a raw trace file

  • ALTER SYSTEM SET TIMED_STATISTICS = TRUE;
  • user_dump_dest must be set
  • Create plan table (@/rdbms/admin/utlxplan.sql)
  • Start trace
    • DBMS_SYSTEM.SET_SQL_TRACE_IN_SESSION(sid,serial#,true)è use v$session to find sid and serial#
    • ALTER SESSION SET SQL_TRACE = TRUE;
    • DBMS_SESSION.SET_SQL_TRACE(TRUE);
  • Trace file naming convention: instance_ora_pid.trc
  • Stop generating trace files  by using same commands (FALSE)



Sample of formatted trace file

 



 

 



Additional Notes

 

A 'db block get' is a current mode get. That is, it's the most up-to-date copy of the data in that block, as it is right now, or currently. There can only be one current copy of a block in the buffer cache at any time. Db block gets generally are used when DML changes data in the database. In that case, row-level locks are implicitly taken on the updated rows. There is also at least one well-known case where a select statement does a db block get, and does not take a lock. That is, when it does a full table scan or fast full index scan, Oracle will read the segment header in current mode (multiple times, the number varies based on Oracle version).

A 'consistent get' is when Oracle gets the data in a block which is consistent with a given point in time, or SCN. The consistent get is at the heart of Oracle's read consistency mechanism. When blocks are fetched in order to satisfy a query result set, they are fetched in consistent mode. If no block in the buffer cache is consistent to the correct point in time, Oracle will (attempt to) reconstruct that block using the information in the rollback segments. If it fails to do so, that's when a query errors out with the much dreaded, much feared, and much misunderstood ORA-1555 "snapshot too old".

 

If non-DBAs need access to trace files, add

_trace_files_public = true to the parameter file to avoid permissions problems on Unix platforms

 

 

 

RAC Architecture - Basic

 




Private Interconnect: It is a high bandwidth & low latency communication setup used for transferring cluster specific heartbeat messages.
In RAC environment, cluster Interconnect can also be used for some high level operations like:
1.       Used for identifying the health, status and message synchronization between all the nodes of the cluster.
2.       Used for maintaining the global resource lock request within the cluster.
3.       Heavily used for transferring the oracle data blocks from one instance buffer cache to other instance buffer cache.
4.       The more the inter instance update will increase more traffic on the network, that’s why is it recommended to have high bandwidth for the cluster interconnect and that could be vendor specific

Global Resource Directory- GRD:
1.       It is new memory component and is a part of shared pool and we can find only in RAC specific instances.
2.       Oracle automatically maintains GRD consistency among all the nodes of the cluster
3.       GRD maintains metadata information of the blocks which are present in database buffer cache(DBBC)
4.       GRD Information like:
a.        SCN #
b.       DBA- Data Block Address/ Data Block Identifier
                                                               i.      It is a combination of file id + block id
c.        Location of the most recent version of the block.
d.       Mode of the block:
                                                               i.      NULL(N) : It indicates that no access rights are available and the block can be accessed locally
                                                              ii.      SHARED (S): it indicates that access rights are available and block can be accessible by multiple instances.
                                                            iii.      EXCLUSIVE(X): It indicates that block can be accessible by only one instance exclusively, basically during DML operations.
e.       Role of the block:
                                                               i.      LOCAL(L): It indicates that the block image is present in only one instance
                                                              ii.      GLOBAL (G): it indicates that the block image is present in multiple instances.
f.         Type of the block image:
                                                               i.      Current (CURR)
                                                              ii.      Consistent read (CR)
                                                            iii.      Past image (PI)







GRD Example:




SCN#
DBA
Location
Mod of the block
Role of the block
Type of the image
U1 (user1 : Select statement from instance 1) N1 (node1)
125
1521
(fileid:15, block id:21)
N1 – Node1
Null (N)
Local (L)
CURR
U1 (User1: issued an update statement)
128
1521
N1
N Ã  X
Null to exclusive mode
L
PI-Past image

CURR Ã  CR

Cache fusion
U2(user2: update statement on same table form node2)
135
1521
N2
XàSàX
G
CURRà CR
               





GRD internally maintains by coordinating GES & GCS.

 Global Enqueue Service – GES: It coordinates with global resource lock request and non cache (other the DBBC) fusion by using LMC & LCK background processes

 Global Cache Services – GCS: It coordinates with cache fusion operations with the help of LSMn background process

Cache Fusion:
-          It is the process of transferring the available data blocks from one instance to buffer cache to another instance buffer cache
-          It is to avoid more costly hardware intensive disk I/O.
o    Disk read Ã  millisecond
o    Cache read Ã  nanosecond
-          It has two phases:
o    Cache fusion phase1 was supported with oracle8i, OPS for only read operations, still I/O has to happen for DML operations
o    From 9i, RAC onwards cache fusion phase2 was introduced for both data read & DML operations.

LMSn – Global Cache Service Process:
-          It is responsible for transferring the data blocks from 1 instance to another instance buffer cache.
-          This process can be controlled by the parameter “GCS_SERVER_PROCESSES”
GCS_SERVER_PROCESSES   = 1 (Default)
                                                = 10 (9.2)
                                                = 20 (10.1)
                                                = 36 (10.2) [0-9 & a-z]
               
Note: Cache fusion operations we can call It as cache coherent technique or soft ping where as disk read we can call as hard ping.

LMON - Global Enqueue Server Monitor:
-          It is responsible for maintaining the consistency for GRD among all the instance of a database
-          It is also responsible for GRD recovery for the failed instance.

LMD – Global Enqueue Service Daemon:
-          It is responsible for maintaining global resource lock requests i.e... Requests for the resource which are coming from the other instances. It will process them and maintain a queue

LCK – Lock Process: It is responsible for non cache fusion operations

DIAG: It is responsible for updating the diagnostic information in alert log files & trace files whenever server process or any other background processes need diagnosis.





Do you know we can start a listener without listener.ora?

Do you know we can start a listener without listener.ora? 

The listener.ora is a file that stores listener configuration information. Because the configuration parameters have default values, it is possible to start and use a listener with no configuration. The below screenshot shows I don't have a listener.ora file at the normal location ($ORACLE_HOME/nework/admin), and I don't have listener up and running:

exxxxxspd02 /apps/opt/oracle/product/12.1.0/network/admin [minidb] > ls -l
total 7
drwxr-xr-x   2 oracle   dba            6 Mar  6 08:41 samples
-rw-r--r--   1 oracle   dba          373 Mar 16  2014 shrept.lst
-rw-r--r--   1 oracle   dba          127 Mar  6 08:18 tnsnames.ora

exxxxxspd02 /apps/opt/oracle/product/12.1.0/network/admin [minidb] > lsnrctl status

LSNRCTL for Solaris: Version 12.1.0.2.0 - Production on 06-MAR-2015 08:42:20

Copyright (c) 1991, 2014, Oracle.  All rights reserved.

Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
TNS-12541: TNS:no listener
 TNS-12560: TNS:protocol adapter error
  TNS-00511: No listener
   Solaris Error: 146: Connection refused

Now I started up a listener:
exxxxxspd02 /apps/opt/oracle/product/12.1.0/network/admin [minidb] > lsnrctl start

LSNRCTL for Solaris: Version 12.1.0.2.0 - Production on 06-MAR-2015 08:44:59

Copyright (c) 1991, 2014, Oracle.  All rights reserved.

Starting /apps/opt/oracle/product/12.1.0/bin/tnslsnr: please wait...

TNSLSNR for Solaris: Version 12.1.0.2.0 - Production
Log messages written to /apps/opt/oracle/diag/tnslsnr/exxxxxspd02/listener/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=exxxxxspd02)(PORT=1521)))

Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Solaris: Version 12.1.0.2.0 - Production
Start Date                06-MAR-2015 08:44:59
Uptime                    0 days 0 hr. 0 min. 0 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Log File         /apps/opt/oracle/diag/tnslsnr/exxxxxspd02/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=exxxxxspd02)(PORT=1521)))
The listener supports no services
The command completed successfully
Notice by default this listener is listening on port 1521 and supports no services. 
I can register my minidb with this local listener through a mechanism called "Dynamic Service Registration". 

For that to happen, I need to configure an initialization parameter called "LOCAL_LISTENER":
exxxxxspd02 /apps/opt/oracle/product/12.1.0/network/admin [minidb] > sqlplus / as sysdba

SQL*Plus: Release 12.1.0.2.0 Production on Fri Mar 6 08:50:13 2015

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


Connected to:
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options

SQL> show parameter local_list

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
local_listener                       string
SQL> alter system set local_listener='(ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))' scope=both;

System altered.

SQL> ho lsnrctl status

LSNRCTL for Solaris: Version 12.1.0.2.0 - Production on 06-MAR-2015 08:51:47

Copyright (c) 1991, 2014, Oracle.  All rights reserved.

Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Solaris: Version 12.1.0.2.0 - Production
Start Date                06-MAR-2015 08:44:59
Uptime                    0 days 0 hr. 6 min. 48 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Log File         /apps/opt/oracle/diag/tnslsnr/exxxxxspd02/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=exxxxxspd02)(PORT=1521)))
Services Summary...
Service "minidb" has 1 instance(s).
  Instance "minidb", status READY, has 1 handler(s) for this service...
The command completed successfully

What happened at background is that by default, the LREG background process can register service information with its local listener on the default local address of TCP/IP, port 1521.
exxxxxspd02 /apps/opt/oracle/product/12.1.0/network/admin [minidb] > ps -ef |grep lreg |grep -v grep
  oracle 28186     1   0 08:24:15 ?           0:00 ora_lreg_minidb

To ensure service registration works properly, the initialization parameter file should contain the following parameters:


  • SERVICE_NAMES for the database service name 
  •  INSTANCE_NAME for the instance name 
  •   LOCAL_LISTENER for the local listener 

Note the SERVICE_NAMES defaults to the global database name, a name comprising the DB_NAME and DB_DOMAIN parameters in the initialization parameter file. The value for the INSTANCE_NAME parameter defaults to the Oracle system identifier (SID).




How to find out the TOTAL/USED/AVAILABLE memory in AIX?

 How to find out the total/used/available memory in AIX?


Sometime we are in the situation to find out the used memroy and available memory of aix server. Using the below script we can find out the total/free/used memory in aix.


#!/usr/bin/ksh
#memory calculator
um=`svmon -G | head -2|tail -1| awk {'print $3'}`
um=`expr $um / 256`
tm=`lsattr -El sys0 -a realmem | awk {'print $2'}`
tm=`expr $tm / 1000`
fm=`expr $tm - $um`
echo "\n\n-----------------------";
echo "System : (`hostname`)";
echo "-----------------------\n\n";
echo "Memory Information\n\n";
echo "total memory = $tm MB"
echo "free memory = $fm MB"
echo "used memory = $um MB"
echo "\n\n-----------------------\n";


Save the above script in a file and execute it.

#vi mem_calci     ---> paste the above script and save it
#chmod +x mem_calci   ---> provide the execute permission
#./mem_calci     ---> run the script

Sample output:

System : (Hostname)
-----------------------
Memory Information

total memory = 8126 MB
free memory = 6328 MB
used memory = 1798 MB
-----------------------

Undo Queries

  SQL> SQL> select sum(bytes /(1024*1024*1024)) from dba_undo_extents where status='EXPIRED'; select sum(bytes /(1024*1024*102...