Showing posts with label 12c. Show all posts
Showing posts with label 12c. Show all posts

Tuesday, February 14, 2023

Using Parallelism in Oracle Database 12C

By Gowthami | apps-dba.com | Oracle Performance Series

Oracle Parallel Query (PQ) allows a single SQL statement to be executed by multiple parallel execution servers simultaneously, dramatically reducing elapsed time for large data operations. It is the primary tool for accelerating full table scans, large sorts, and bulk DML on multi-CPU systems.

Key Insight: Parallel query trades throughput for response time. A query using DOP 8 uses 8x the CPU and I/O resources but may run 6-7x faster. Use parallelism for batch jobs and large reports — not for OLTP queries where resource contention hurts all users.

Parallel Query Architecture

A parallel query uses a Query Coordinator (QC) — the session that submits the query — and multiple Parallel Execution Servers (PX servers) that perform the actual work. The QC distributes work, collects results, and returns them to the user.

-- Check current parallel configuration
SHOW PARAMETER parallel_max_servers;     -- max PX servers in pool
SHOW PARAMETER parallel_min_servers;     -- pre-started PX servers  
SHOW PARAMETER parallel_degree_policy;   -- MANUAL, AUTO, or ADAPTIVE

-- Check active parallel queries
SELECT qcsid, server#, degree, req_degree, sql_text
FROM v$px_session ps
JOIN v$sql s ON ps.sql_id = s.sql_id;

Enabling Parallelism

-- Method 1: Table-level parallel degree (persistent)
ALTER TABLE large_sales_table PARALLEL 8;

-- Method 2: Query-level hint (preferred for control)
SELECT /*+ PARALLEL(s, 8) */ 
       region, SUM(amount) 
FROM large_sales_table s
GROUP BY region;

-- Method 3: Session-level (affects all queries in session)
ALTER SESSION FORCE PARALLEL QUERY PARALLEL 4;
ALTER SESSION FORCE PARALLEL DML PARALLEL 4;
ALTER SESSION FORCE PARALLEL DDL PARALLEL 4;

-- Reset session-level parallelism
ALTER SESSION DISABLE PARALLEL QUERY;

Parallel DML Operations

-- Parallel DML must be explicitly enabled
ALTER SESSION ENABLE PARALLEL DML;

-- Parallel INSERT
INSERT /*+ PARALLEL(t, 8) APPEND */ INTO target_table t
SELECT /*+ PARALLEL(s, 8) */ * FROM source_table s
WHERE created_date >= DATE '2024-01-01';
COMMIT;

-- Parallel UPDATE (12c+)
UPDATE /*+ PARALLEL(e, 4) */ large_emp_table e
SET salary = salary * 1.05
WHERE department_id IN (10, 20, 30);
COMMIT;

-- Parallel CREATE TABLE AS SELECT (CTAS)
CREATE /*+ PARALLEL(8) */ TABLE archive_sales
PARALLEL 8 NOLOGGING AS
SELECT /*+ PARALLEL(s, 8) */ * FROM sales_history s
WHERE sale_year = 2023;

Monitoring Parallel Query Execution

-- Monitor active parallel queries (12c+)
SELECT sql_id, sql_text, px_servers_requested, px_servers_allocated,
       elapsed_time/1000000 elapsed_sec, cpu_time/1000000 cpu_sec
FROM v$sql_monitor
WHERE px_servers_requested > 0
  AND status = 'EXECUTING'
ORDER BY elapsed_time DESC;

-- Check parallel operations in execution plan
EXPLAIN PLAN FOR
SELECT /*+ PARALLEL(s, 4) */ region, SUM(amount)
FROM sales s GROUP BY region;

SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
-- Look for: PX COORDINATOR, PX SEND/RECEIVE, PX BLOCK ITERATOR

Choosing the Right Degree of Parallelism

Operation TypeRecommended DOPNotes
Ad-hoc large reports4-8Balance speed vs resource use
Nightly ETL/batchCPUs/2 to CPUsOff-peak, maximize throughput
CTAS / index rebuildCPUs or CPUs/2DDL benefits most from high DOP
OLTP queries1 (no parallel)Parallelism hurts OLTP concurrency
Exadata queriesAUTO or ADAPTIVEExadata manages DOP automatically

Summary

Oracle Parallel Query is a powerful weapon for large data operations. The key is using it judiciously — enable it for batch jobs, large reports, and maintenance operations on large tables, but avoid it for OLTP workloads. Always monitor with V$SQL_MONITOR to ensure parallel queries are actually running at the requested DOP.

Oracle Exadata - The Complete Guide

Learn how Exadata Smart Scan supercharges parallel query performance and master all Exadata-specific optimization techniques with Gowthami's guide.

Get the Book

Tuesday, October 18, 2022

Modify AWR interval and retention periods in 12C

By Gowthami | apps-dba.com | Oracle Database Administration Series

AWR (Automatic Workload Repository) is Oracle's built-in performance data collection framework. By default, AWR snapshots are taken every 60 minutes and retained for 8 days. For detailed performance analysis or compliance requirements, DBAs often need to adjust these settings. This post covers how to modify AWR snapshot interval and retention in Oracle 12c.

What You Will Learn: How to view current AWR settings, modify the snapshot interval and retention period using DBMS_WORKLOAD_REPOSITORY, and verify the changes in Oracle Database 12c.

Default AWR Settings

ParameterDefault ValueDescription
Snapshot Interval60 minutesHow often AWR takes a snapshot
Retention Period8 days (11520 minutes)How long snapshots are kept
Top SQLTop 30Number of top SQL statements captured

Check Current AWR Settings

Query the DBA_HIST_WR_CONTROL view to see current configuration:

SQL> SELECT dbid,
       snap_interval,
       retention,
       topnsql
FROM dba_hist_wr_control;

DBID        SNAP_INTERVAL      RETENTION           TOPNSQL
----------- ---------------    ----------------    -------
1234567890  +00000 01:00:00.0  +00008 00:00:00.0   DEFAULT

Modify AWR Interval and Retention

Use the DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS procedure. All time values are in minutes.

Syntax:

BEGIN
  DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS(
    retention  => <retention_in_minutes>,
    interval   => <interval_in_minutes>,
    topnsql    => <number_of_top_sql>,
    dbid       => <dbid>
  );
END;
/

Example 1: Change Interval to 30 Minutes, Retain 14 Days

BEGIN
  DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS(
    retention => 20160,  -- 14 days (14 * 24 * 60)
    interval  => 30      -- 30 minutes
  );
END;
/

Example 2: Disable AWR Snapshots

Setting interval to 0 disables automatic snapshot collection:

BEGIN
  DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS(
    interval => 0
  );
END;
/

Example 3: Re-enable with Custom Settings

BEGIN
  DBMS_WORKLOAD_REPOSITORY.MODIFY_SNAPSHOT_SETTINGS(
    retention => 43200,  -- 30 days
    interval  => 60      -- 60 minutes (default)
  );
END;
/

Verify the Changes

SQL> SELECT snap_interval, retention
FROM dba_hist_wr_control;

SNAP_INTERVAL        RETENTION
-------------------  -------------------
+00000 00:30:00.0    +00014 00:00:00.0

AWR Retention Quick Reference

Retention PeriodMinutes Value
7 days (default)10080
14 days20160
30 days43200
60 days86400
90 days129600

Important Considerations

  • Increasing retention increases SYSAUX tablespace usage—monitor space carefully
  • Shorter intervals (e.g., 15 minutes) are useful during performance troubleshooting but increase storage usage
  • AWR is a licensed feature—requires Oracle Diagnostics Pack
  • In a CDB (12c+), AWR settings apply per PDB when PDB-level AWR is enabled

Master Oracle Exadata

This post is part of our Oracle Database Administration series. Get our comprehensive Exadata guide with AWR analysis techniques, performance tuning, and real-world case studies.

Get the Exadata PDF Guide

Wednesday, September 21, 2022

Create SQL Plan Baselines for High Resource Intensive SQL's from AWR - 12C


=> Identify SNAP ID's for the interval

Here I am collecting resource intensive SQL's ran on a week from Monday (12-SEP-22) to Friday (16-SEP-22)


SQL> show pdbs

    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 SITPDB                         READ WRITE NO

SQL> alter session set container=SITPDB;

Session altered.

SQL>
SQL>
SQL> select min(snap_id) from dba_hist_snapshot where trunc(begin_interval_time)='12-SEP-22';

MIN(SNAP_ID)
------------
        3324

SQL> select max(snap_id) from dba_hist_snapshot where trunc(begin_interval_time)='16-SEP-22';

MAX(SNAP_ID)
------------
        3439

SQL> select user from v$database;

USER
------------------------------
SYS
SQL>


=> Create an AWR Baseline


BEGIN
 DBMS_WORKLOAD_REPOSITORY.create_baseline (
 start_snap_id => 3324,
 end_snap_id => 3439,
 baseline_name => 'weekly_baseline_sep_12_16');
END;
/

PL/SQL procedure successfully completed.
SQL>


=> Create a SQL Tuning Set Object


BEGIN
 dbms_sqltune.create_sqlset(
 sqlset_name => 'weekly_awr_dev1'
 ,description => 'STS from AWR');
END;
/

PL/SQL procedure successfully completed.
SQL>


=> Populate the SQL Tuning Set with High-Resource Queries Found in AWR Baseline

Here I am populating top 20 SQL's based on elapsed_time


DECLARE
 base_cur dbms_sqltune.sqlset_cursor;
BEGIN
 OPEN base_cur FOR
 SELECT value(x)
 FROM table(dbms_sqltune.select_workload_repository(
 'weekly_baseline_sep_12_16', null, null,'elapsed_time',
 null, null, null, 20)) x;
 dbms_sqltune.load_sqlset(
 sqlset_name => 'weekly_awr_dev1',
 populate_cursor => base_cur);
END;
/

PL/SQL procedure successfully completed.
SQL>


To view the queries within the SQL tuning set, run below query 

select * from dba_sqlset_statements where sqlset_name = 'weekly_awr_dev1';














=> Use the Tuning Set As Input to DBMS_SPM to Create Plan Baselines for Each Query Contained in the SQL Tuning Set


DECLARE
 dev_plan1 PLS_INTEGER;
BEGIN
 dev_plan1 := dbms_spm.load_plans_from_sqlset(
 sqlset_name=>'weekly_awr_dev1');
END;
/

PL/SQL procedure successfully completed.
SQL>


Now each query in the SQL tuning set has an enabled plan baseline associated with it.

SQL> select sql_handle, plan_name, enabled, accepted from dba_sql_plan_baselines order by elapsed_time desc;











Tuesday, July 5, 2022

Change CDB and PDB names in 12c

Change CDB name from DEVCDB to TESTCDB

*********************************************
Backup database before performing this action
*********************************************



	

SQL> SHUTDOWN IMMEDIATE;
Database closed.
Database dismounted.
ORACLE instance shut down.

SQL> STARTUP MOUNT
ORACLE instance started.
Total System Global Area 2.0133E+10 bytes
Fixed Size                  3721176 bytes
Variable Size            2684356648 bytes
Database Buffers         1.7381E+10 bytes
Redo Buffers               63385600 bytes
Database mounted.
SQL> exit

[oracle@hostname ~]$ nid TARGET=/ DBNAME=TESTCDB
DBNEWID: Release 12.1.0.2.0 - Production on Fri Jul 1 12:44:48 2022
Copyright (c) 1982, 2014, Oracle and/or its affiliates.  All rights reserved.
Connected to database DEVCDB (DBID=2721637057)
Connected to server version 12.1.0
Control Files in database:
    /oradata/devcdb/oradata/DEVCDB/controlfile/o1_mf_kcx8zx9n_.ctl
    /oradata/devcdb/fast_recovery_area/DEVCDB/controlfile/o1_mf_kcx8zxcj_.ctl
Change database ID and database name DEVCDB to TESTCDB? (Y/[N]) => Y
Proceeding with operation
Changing database ID from 2721637057 to 2850317248
Changing database name from DEVCDB to TESTCDB
    Control File /oradata/devcdb/oradata/DEVCDB/controlfile/o1_mf_kcx8zx9n_.ctl - modified
    Control File /oradata/devcdb/fast_recovery_area/DEVCDB/controlfile/o1_mf_kcx8zxcj_.ctl - modified
    Datafile /oradata/devcdb/oradata/DEVCDB/datafile/o1_mf_system_kcx8xpcx_.db - dbid changed, wrote new name
    Datafile /oradata/devcdb/oradata/DEVCDB/datafile/o1_mf_sysaux_kcx8wm74_.db - dbid changed, wrote new name
    Datafile /oradata/devcdb/oradata/DEVCDB/datafile/o1_mf_undotbs1_kcx8ytp3_.db - dbid changed, wrote new name
    Datafile /oradata/devcdb/oradata/DEVCDB/datafile/o1_mf_system_kcx9039v_.db - dbid changed, wrote new name
    Datafile /oradata/devcdb/oradata/DEVCDB/datafile/o1_mf_users_kcx8yslq_.db - dbid changed, wrote new name
    Datafile /oradata/devcdb/oradata/DEVCDB/datafile/o1_mf_sysaux_kcx9039r_.db - dbid changed, wrote new name
    Datafile /oradata/devcdb/oradata/DEVCDB/E2BA8506A8CF2123E053052AA8C092B1/datafile/o1_mf_system_kcx9b6cy_.db - dbid changed, wrote new name
    Datafile /oradata/devcdb/oradata/DEVCDB/E2BA8506A8CF2123E053052AA8C092B1/datafile/o1_mf_sysaux_kcx9b6d3_.db - dbid changed, wrote new name
    Datafile /oradata/devcdb/oradata/DEVCDB/E2BA8506A8CF2123E053052AA8C092B1/datafile/o1_mf_users_kcx9bb00_.db - dbid changed, wrote new name
    Datafile /oradata/devcdb/oradata/DEVCDB/datafile/o1_mf_temp_kcx901k3_.tm - dbid changed, wrote new name
    Datafile /oradata/devcdb/oradata/DEVCDB/datafile/pdbseed_temp012022-07-01_11-37-02-AM.db - dbid changed, wrote new name
    Datafile /oradata/devcdb/oradata/DEVCDB/E2BA8506A8CF2123E053052AA8C092B1/datafile/o1_mf_temp_kcx9b6d3_.db - dbid changed, wrote new name
    Control File /oradata/devcdb/oradata/DEVCDB/controlfile/o1_mf_kcx8zx9n_.ctl - dbid changed, wrote new name
    Control File /oradata/devcdb/fast_recovery_area/DEVCDB/controlfile/o1_mf_kcx8zxcj_.ctl - dbid changed, wrote new name
    Instance shut down
Database name changed to TESTCDB.
Modify parameter file and generate a new password file before restarting.
Database ID for database TESTCDB changed to 2850317248.
All previous backups and archived redo logs for this database are unusable.
Database is not aware of previous backups and archived logs in Recovery Area.
Database has been shutdown, open database with RESETLOGS option.
Succesfully changed database name and ID.
DBNEWID - Completed succesfully.
[oracle@hostname ~]$

SQL> startup nomount;
ORACLE instance started.
Total System Global Area 2.0133E+10 bytes
Fixed Size                  3721176 bytes
Variable Size            2684356648 bytes
Database Buffers         1.7381E+10 bytes
Redo Buffers               63385600 bytes

SQL> alter system set db_name='TESTCDB' scope=spfile;
System altered.

SQL> SHUTDOWN IMMEDIATE;
ORA-01507: database not mounted
ORACLE instance shut down.
SQL> exit

[oracle@hostname ~]$ export ORACLE_SID=TESTCDB

SQL> STARTUP MOUNT
ORACLE instance started.
Total System Global Area 2.0133E+10 bytes
Fixed Size                  3721176 bytes
Variable Size            2684356648 bytes
Database Buffers         1.7381E+10 bytes
Redo Buffers               63385600 bytes
Database mounted.
SQL>

SQL> ALTER DATABASE OPEN RESETLOGS;
Database altered.

SQL> show pdbs
    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 DEVPDB                         MOUNTED

SQL> alter pluggable database DEVPDB open;
Pluggable database altered.

SQL> show pdbs
    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 DEVPDB                         READ WRITE NO

SQL> select name from v$database;
NAME
---------
TESTCDB

SQL> select instance_name from v$instance;
INSTANCE_NAME
----------------
TESTCDB

SQL> show parameter db_name
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_name                              string      TESTCDB



    


Change PDB name from DEVPDB to TESTPDB (NON TDE Environment)

*********************************************
Backup database before performing this action
*********************************************


	

SQL> alter pluggable database DEVPDB close;
Pluggable database altered.

SQL> alter pluggable database DEVPDB unplug into '/orabin/app/product/12.1.0/dbs/DEVPDB_meta.xml';
Pluggable database altered.

SQL> drop pluggable database DEVPDB;
Pluggable database dropped.

SQL> create pluggable database TESTPDB using '/orabin/app/product/12.1.0/dbs/DEVPDB_meta.xml' NOCOPY;
Pluggable database created.

SQL> show pdbs
    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 TESTPDB                        MOUNTED

SQL> alter pluggable database TESTPDB open;
Pluggable database altered.

SQL> show pdbs
    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 TESTPDB                        READ WRITE NO

SQL> alter pluggable database all save state instances=all;
Pluggable database altered.

SQL> show pdbs
    CON_ID CON_NAME                       OPEN MODE  RESTRICTED
---------- ------------------------------ ---------- ----------
         2 PDB$SEED                       READ ONLY  NO
         3 TESTPDB                        READ WRITE NO

SQL> show parameter service
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
service_names                        string      TESTCDB


SQL> exit

Disconnected from Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options
[oracle@hostname ~]$



    


Change PDB name from DEVPDB to TESTPDB (TDE Environment)

*********************************************
Backup database before performing this action
*********************************************

In RAC Environment run the below commands from only one node, close the PDB on other nodes using below commands



	

alter pluggable database DEVPDB close immediate instances=all ;

alter pluggable database DEVPDB open restricted ;

alter session set container=DEVPDB ;

alter pluggable database rename global_name to TESTPDB ;

alter pluggable database close immediate ;

alter pluggable database open instances=all ;


    





Non-Equijoins and Self-Joins in Oracle SQL

Non-Equijoins and Self-Joins in Oracle SQL Non-Equijoins and Self-Joins in Oracle SQL: Complete Guide Most joins in SQL use the e...