Wednesday, 5 March 2025

How to Creating a Simple Batch Job in Oracle PL/SQL

 Here is the PL/SQL Snippet to create and schedule a batch job.

Note : Please test and modify accordingly as per your requirements.

BEGIN

    DBMS_SCHEDULER.CREATE_JOB (

        job_name        => 'MY_BATCH_JOB',

        job_type        => 'PLSQL_BLOCK',

        job_action      => 'BEGIN MY_PACKAGE.MY_PROCEDURE; END;',

        start_date      => SYSTIMESTAMP,

        enabled         => TRUE

    );

END;

Understanding Oracle DBMS_CLOUD Common Packages

Understanding Oracle DBMS_CLOUD Common Packages

Oracle Cloud provides the DBMS_CLOUD package to simplify the integration of cloud-based storage, data loading, and external data access. 

This package is useful for managing data in Oracle Autonomous Database and other cloud environments, enabling users to efficiently interact with external cloud services.

Overview of DBMS_CLOUD

The DBMS_CLOUD package is a collection of procedures and functions that help users perform operations such as loading data, managing credentials, and accessing external storage. It eliminates the need for complex scripting and provides secure access to cloud-based resources.

DBMS_CLOUD Packages and their usages.

1. DBMS_CLOUD.CREATE_CREDENTIAL

This procedure securely stores cloud service credentials, which are used to authenticate database access to object storage.

BEGIN
    DBMS_CLOUD.CREATE_CREDENTIAL(
        credential_name => 'my_credential',
        username => 'your_username',
        password => 'your_password'
    );
END;

2. DBMS_CLOUD.LIST_OBJECTS

Retrieves a list of objects from a specified cloud storage bucket.

SELECT * FROM TABLE(DBMS_CLOUD.LIST_OBJECTS(
    credential_name => 'my_credential',
    location => 'https://objectstorage.region.oraclecloud.com/n/namespace/b/bucket/o/'
));



















3. DBMS_CLOUD.GET_OBJECT

Downloads a file from cloud storage into the database directory.

BEGIN
    DBMS_CLOUD.GET_OBJECT(
        credential_name => 'my_credential',
        object_uri => 'https://objectstorage.region.oraclecloud.com/n/namespace/b/bucket/o/sample.csv',
        directory_name => 'DATA_PUMP_DIR',
        file_name => 'sample.csv'
    );
END;

4. DBMS_CLOUD.COPY_DATA

Loads data from an external source (such as an object storage CSV file) into an Oracle database table.

BEGIN
    DBMS_CLOUD.COPY_DATA(
        table_name => 'employees',
        credential_name => 'my_credential',
        file_uri_list => 'https://objectstorage.region.oraclecloud.com/n/namespace/b/bucket/o/employees.csv',
        format => JSON_OBJECT('delimiter' VALUE ',', 'skipheaders' VALUE 1)
    );
END;

5. DBMS_CLOUD.DELETE_CREDENTIAL

Removes a stored credential when it is no longer needed.

BEGIN
    DBMS_CLOUD.DELETE_CREDENTIAL('my_credential');
END;

Benefits of Using DBMS_CLOUD

  • Secure Cloud Storage Integration – Simplifies connecting Oracle databases with cloud storage services.
  • Efficient Data Loading – Enables quick and efficient bulk data imports from external sources.
  • Flexible Querying – Allows querying external data sources without physically moving data.
  • Improved Performance – Optimized for cloud environments, reducing manual data transfer overhead.

Understanding Oracle SQL ID and Its Role in Performance Issues

 Understanding Oracle SQL ID and Its Role in Performance Issues

In Oracle databases, the SQL ID is a fundamental identifier that helps database administrators (DBAs) track and analyze SQL statements. When performance issues arise, SQL IDs can provide critical insights into problematic queries, enabling optimization and troubleshooting.

What is an Oracle SQL ID?

An SQL ID is a unique identifier assigned to an SQL statement when it is first parsed and stored in the library cache. It remains constant for that particular SQL text, helping DBAs identify and analyze the query’s execution behavior over time.

Why is SQL ID Important in Performance Tuning?

SQL IDs are vital for performance troubleshooting because they allow DBAs to:

  • Locate specific queries in dynamic performance views.
  • Analyze execution plans to identify inefficiencies.
  • Monitor query performance using AWR (Automatic Workload Repository) and SQL Monitoring reports.
  • Compare query performance over different executions.

Identifying Performance Issues Using SQL ID

Once you have an SQL ID, you can investigate performance issues using several approaches:

1. Retrieve Execution Plan

Using the DBMS_XPLAN.DISPLAY_CURSOR function, you can analyze the execution plan to identify inefficiencies such as full table scans, missing indexes, or suboptimal join methods.

SELECT * FROM table(DBMS_XPLAN.DISPLAY_CURSOR('<Your SQL ID>', 0, 'ALL'));

2. Query SQL History from AWR

To see historical performance data related to an SQL ID, use:

SELECT * FROM dba_hist_sqlstat WHERE sql_id = <Your SQL ID>
;

This helps determine if performance degradation is recent or has been ongoing.

3. Check Active Sessions

If an SQL statement is currently running and causing performance issues, use:

SELECT sql_id, status, username, sql_text FROM v$session WHERE sql_id = <Your SQL ID>
;

This provides information on users executing the query and its current status.

Common Causes of SQL Performance Issues

  1. Full Table Scans – Ensure proper indexing to avoid unnecessary full table scans.
  2. Inefficient Joins – Optimize joins by using appropriate indexes and execution strategies.
  3. Outdated Statistics – Regularly update optimizer statistics using:
    EXEC DBMS_STATS.GATHER_SCHEMA_STATS('your_schema');
    
  4. High Parsing Overhead – Use bind variables to reduce excessive hard parsing.
  5. Blocking Sessions – Check for locking issues using V$LOCK and V$SESSION.

Conclusion

SQL ID is a powerful tool for diagnosing and resolving performance issues in Oracle databases. By leveraging execution plans, historical data, and session monitoring, DBAs can pinpoint problematic queries and implement optimizations. Regular performance analysis ensures smooth database operations and improved efficiency.

Monday, 3 March 2025

How to Find the SQL ID in Oracle Developer

 

How to Find the SQL ID in Oracle Developer

SQL ID is a unique identifier assigned by Oracle to each SQL statement executed in the database. It is essential for performance tuning, troubleshooting, and analyzing query execution plans. In this blog post, we will explore various methods to find the SQL ID in Oracle Developer and SQL*Plus.

1. Using V$SQL to Find SQL ID

Oracle provides the V$SQL view, which contains details about executed SQL statements, including the SQL ID. The following query retrieves the SQL ID for a specific SQL text:

SELECT sql_id, sql_text
FROM v$sql
WHERE sql_text LIKE '%your_query_pattern%';

Replace %your_query_pattern% with a part of your SQL statement to filter the results.

2. Finding SQL ID for a Running Query in V$SESSION

If a query is currently executing, you can find its SQL ID from the V$SESSION view:

SELECT sid, serial#, sql_id, sql_text
FROM v$session s
JOIN v$sql q ON s.sql_id = q.sql_id
WHERE s.status = 'ACTIVE';

This query lists active sessions and their corresponding SQL IDs.

3. Using SQL Developer’s Autotrace or Explain Plan

Steps to Find SQL ID in Oracle SQL Developer:

  1. Open Oracle SQL Developer and execute your query.
  2. Enable Autotrace:
    • Click on Query Builder > Enable Autotrace.
    • Run your SQL statement.
    • The SQL ID will be displayed in the execution plan output.
  3. Alternatively, run:
    SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(null, null, 'ALL'));
    
    This retrieves the SQL ID of the last executed statement in your session.

4. Using AWR to Retrieve SQL ID for Historical Queries

Oracle's Automatic Workload Repository (AWR) stores SQL execution history, which is useful when identifying SQL IDs for past queries:

SELECT sql_id, sql_text, elapsed_time_total
FROM dba_hist_sqlstat
ORDER BY elapsed_time_total DESC
FETCH FIRST 10 ROWS ONLY;

This retrieves the top 10 longest-running SQL queries along with their SQL IDs.

5. Finding SQL ID from an Active Session in ASH

Oracle’s Active Session History (ASH) provides detailed SQL execution information:

SELECT sql_id, session_id, sql_text
FROM v$active_session_history
WHERE sample_time > SYSDATE - INTERVAL '10' MINUTE;

This helps track recent SQL statements executed in the database.

Conclusion

Knowing how to find the SQL ID in Oracle is crucial for diagnosing performance issues and analyzing execution plans. Whether using V$SQL, V$SESSION, ASH, or AWR reports, you can quickly locate the SQL ID and use it for further tuning or investigation.