Friday, 28 February 2025

Optimizing Long-Running SQL Queries

In this blog post, we will explore various methods to find, analyze, and optimize long-running SQL queries in Oracle.


Indexing for Performance Improvement

Indexes can significantly improve query performance. To check for missing indexes, you can analyze execution plans using the following:


SELECT * FROM v$sql_plan WHERE sql_id = '<SQL_ID>';


If the execution plan shows full table scans where indexes could be useful, consider creating indexes:


CREATE INDEX idx_column_name ON table_name (column_name);


Optimizing Execution Plans

Use EXPLAIN PLAN to understand how Oracle executes queries:


EXPLAIN PLAN FOR SELECT * FROM employees WHERE department_id = 10;

SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);


Review the execution plan and ensure the query is utilizing indexes efficiently.


Using Query Hints

SELECT /*+ INDEX(table_name index_name) */ column1, column2 FROM table_name WHERE column1 = 'value';


Partitioning Large Tables

Partitioning large tables can improve query performance by reducing the amount of data scanned:


CREATE TABLE sales_partitioned (

    sale_id NUMBER,

    sale_date DATE,

    amount NUMBER

)

PARTITION BY RANGE (sale_date) (

    PARTITION sales_2024 VALUES LESS THAN (TO_DATE('2025-01-01', 'YYYY-MM-DD')),

    PARTITION sales_2025 VALUES LESS THAN (TO_DATE('2026-01-01', 'YYYY-MM-DD'))

);


Identifying Long-Running SQL Queries in Oracle Database

 

Identifying Long-Running SQL Queries in Oracle Database

In any Oracle database environment, long-running SQL queries can impact performance, causing bottlenecks, and degrade the overall user experience. 

Identifying and optimizing these SQL queries is crucial for maintaining database efficiency. In this blog post, we will see the various methods to find and analyze long-running SQL queries in Oracle.

1. Using V$SESSION and V$SQL

Oracle provides dynamic performance views such as V$SESSION and V$SQL to monitor active sessions and queries. To find long-running queries, execute the following SQL statement:

SELECT s.sid, s.serial#, s.username, s.status, s.schemaname, s.osuser,
       s.machine, s.program, q.sql_id, q.sql_text, s.logon_time,
       (sysdate - s.logon_time) * 24 * 60 AS minutes_running
FROM v$session s
JOIN v$sql q ON s.sql_id = q.sql_id
WHERE s.status = 'ACTIVE'
ORDER BY minutes_running DESC;

This query will help you identify active sessions, their corresponding SQL queries, and how long they have been running.

2. Using V$SESSION_LONGOPS

For queries that take a significant amount of time, Oracle tracks their progress in V$SESSION_LONGOPS. The following SQL query helps you to monitor long-running operations:

SELECT sid, serial#, opname, target, sofar, totalwork,
       ROUND(sofar/totalwork*100,2) AS percent_done, elapsed_seconds
FROM v$session_longops
WHERE totalwork > 0 AND sofar <> totalwork
ORDER BY elapsed_seconds DESC;

This view provides insights into the execution progress of long-running queries.

3. Using Active Session History (ASH)

Oracle Active Session History (ASH) collects sampled session activity, which can be used to analyze performance bottlenecks. The following query retrieves details on long-running queries from ASH:

SELECT sql_id, session_id, session_serial#, event, wait_class,
       sample_time, time_waited
FROM v$active_session_history
WHERE sample_time > SYSDATE - INTERVAL '10' MINUTE
ORDER BY sample_time DESC;

ASH provides granular insights into SQL execution patterns over a specific time frame.

4. Identifying Blocking Sessions

Long-running queries might be blocked by other sessions, leading to performance degradation. To identify blocking sessions, use the following query:

SELECT blocking_session, sid, serial#, wait_class, event, seconds_in_wait
FROM v$session
WHERE blocking_session IS NOT NULL;

This will help you in diagnosing contention issues within the database.

5. Checking AWR Reports for SQL Performance

Oracle’s Automatic Workload Repository (AWR) captures detailed performance statistics. The following query lists the top 10 longest-running SQL queries from AWR:

SELECT sql_id, elapsed_time_total, executions,
       elapsed_time_total/executions AS avg_elapsed_time
FROM dba_hist_sqlstat
ORDER BY elapsed_time_total DESC
FETCH FIRST 10 ROWS ONLY;

AWR provides historical performance data, enabling better tuning and optimization.

Conclusion

Finding long-running SQL queries in Oracle is essential for performance tuning and system health. By leveraging views like V$SESSION, V$SESSION_LONGOPS, V$ACTIVE_SESSION_HISTORY, and DBA_HIST_SQLSTAT, you can proactively monitor, analyze, and optimize queries to improve overall database performance. 

Regularly analysis and tuning can significantly reduce query execution times and enhance user experience.


Oracle SQL Developer: Hidden Gems, Tips and Tricks - Part2

 

Oracle SQL Developer: Tips & Tricks for the Developers.


Oracle SQL Developer is a powerful, free integrated development environment (IDE) for working with Oracle databases. 

Many of the developers use it for writing queries and managing schemas, however there are numerous hidden features and tricks that can significantly boost productivity and efficiency. 

We will see some of the best tips and how you can make most of the Oracle SQL Developer tool.

1. Customizing SQL Developer

Enabling Dark Mode & Custom Themes

SQL Developer supports UI customization, including dark mode. To enable it:

  1. Navigate to Tools > Preferences.

  2. Go to Code Editor > PL/SQL Syntax Colors.

  3. Choose a dark color scheme or customize individual colors.

Customizing Keyboard Shortcuts

Use the following to increase efficiency, modify shortcuts:

  1. Tools > Preferences > Shortcut Keys.

  2. Assign frequently used functions (e.g., CTRL+ENTER for executing queries).

Using Code Templates & Snippets

You can use frequently used SQL patterns as templates:

  1. Go to Tools > Preferences > Database > SQL Editor Code Templates.

  2. Define a new template like:

    SELECT * FROM $TABLE$ WHERE $COLUMN$ = '$VALUE$';
  3. Use it in the editor by typing the shortcut and pressing TAB.

Setting Up Auto Format for SQL Queries

SQL Developer has a built-in formatter:

  1. Tools > Preferences > Database > SQL Formatter.

  2. Adjust settings (e.g., indentation, keyword casing).

  3. Press CTRL+F7 to auto-format queries.


2. 

Generating ER Diagrams Automatically

You can generate the visualization of the database structures with ER diagrams:

  1. Right-click on a connection and select Data Modeler > Import > Data Dictionary.

  2. Select tables and generate the diagram.

Tracking SQL History & Restoring Lost Queries

You can use this option in order to recover a lost query :

  • Use F8 to open SQL History and retrieve past queries.

  • Use View > Files to browse auto-saved scripts.

Using the Hierarchical Profiler to Optimize Queries

Find slow execution queries:

  1. Enable profiling in Tools > Preferences > Database > PL/SQL Profiler.

  2. Run DBMS_HPROF for performance analysis.

Running Multiple Queries in Parallel

  • Open multiple SQL worksheet tabs (CTRL+SHIFT+N).

  • Run different queries simultaneously using different connections.


3. Useful Tools and Utilities

Database Copy & Compare Tool

Compare databases without external tools:

  1. Tools > Database Copy to duplicate schemas.

  2. Tools > Database Diff to compare changes.

Exporting Query Results to Excel, JSON, and XML

Quickly export data:

  • Run a query, right-click the results, select Export.

  • Choose XLS, JSON, XML, CSV formats.

Using SQL Developer as an SSH Client

Connect to databases via SSH:

  1. Tools > Preferences > Database > SSH.

  2. Configure tunneling to access remote databases securely.

Executing SQL Scripts via Command-Line Interface (CLI)

Run SQL scripts from the command line:

sqldeveloper.exe -script my_script.sql

This is useful for automation and batch processing.


4. Debugging & Performance Tuning

Using the Explain Plan & Autotrace Effectively

Analyze query performance:

  • Press F10 to generate an execution plan.

  • Use Autotrace (CTRL+SHIFT+E) for real-time performance metrics.

Leveraging Real-Time SQL Monitoring

For long-running queries, use the following SQL Query

SELECT * FROM V$SQL_MONITOR;

This helps track execution times and bottlenecks.

Finding Unused Indexes and Optimizing Execution Plans

Identify unused indexes:

SELECT * FROM DBA_INDEX_USAGE;

This can help remove unnecessary indexes to improve performance.


5. Advanced Scripting and Automation

Creating and Running PL/SQL Debug Sessions

To debug stored procedures:

  1. Enable debugging in Tools > Preferences > Debugger.

  2. Right-click a PL/SQL procedure and select Run with Debugger.

Scheduling and Automating Database Tasks

Use DBMS_SCHEDULER to automate jobs:

BEGIN
  DBMS_SCHEDULER.CREATE_JOB (
    job_name => 'MY_BATCH_JOB',
    job_type => 'PLSQL_BLOCK',
    job_action => 'BEGIN my_procedure; END;',
    start_date => SYSTIMESTAMP,
    repeat_interval => 'FREQ=DAILY; BYHOUR=1',
    enabled => TRUE
  );
END;
/

Building Custom Reports in SQL Developer

Create reports under View > Reports and define custom queries to generate insights.

Final Conclusion : 

Oracle SQL Developer is much more than just an SQL editor. By leveraging these useful tips, you can significantly boost your efficiency and productivity. 

Whether you’re customizing the UI, automating tasks, or optimizing queries, these tips will make SQL Developer a powerhouse for database professionals.

Oracle SQL Developer - Hidden Gems, Tips and Tricks - Part1

 

Oracle SQL Developer: Hidden Gems, Tips & Tricks


In this post, we will see some of the features which are available in Oracle SQL Developer which can be utilized in building stuffs. we’ll explore some lesser-known features, shortcuts, and productivity hacks that can significantly improve your workflow.

Oracle SQL Developer is a powerful tool for database development and administration, yet many users only scratch the surface of its capabilities. 

1. Customizing SQL Developer for Efficiency

  • Using Code Templates & Snippets
  • Enabling Dark Mode & Custom Themes
  • Customizing Keyboard Shortcuts
  • Setting Up Auto Format for SQL Queries

2. Features You Might Not Be Using

  • Generating ER Diagrams Automatically
  • Running Multiple Queries in Parallel
  • Tracking SQL History & Restoring Lost Queries
  • Using the Hierarchical Profiler to Optimize Queries

3. Key Tools and Utilities

  • Database Copy & Compare Tool
  • Exporting Query Results to Excel, JSON, and XML
  • Using SQL Developer as an SSH Client
  • Executing SQL Scripts via the Command-Line Interface (CLI)

4. Debugging & Performance Tuning

  • Using the Explain Plan & Autotrace Effectively
  • Leveraging Real-Time SQL Monitoring
  • Finding Unused Indexes and Optimizing Execution Plans

5. Advanced Scripting and Automation

  • Creating and Running PL/SQL Debug Sessions
  • Scheduling and Automating Database Tasks
  • Building Custom Reports in SQL Developer

By mastering these features, you can turn Oracle SQL Developer into a productivity powerhouse tool.


In the next post we will see deeper into some of the features....


How to Export Database in Oracle SQL Developer

Here are the steps to for exporting the data in Oracle SQL Developer. These are very simple steps which can be performed to extract any of the database objects.

To export a database in Oracle SQL Developer, follow these steps:

Method 1: Export Using Data Pump

  1. Open Oracle SQL Developer and connect to your database.
  2. Open DBA Panel:
    • Go to ViewDBA.
    • Right-click your connection and select Add Connection.
    • Expand the connection and navigate to Data PumpExport Jobs.
  3. Create an Export Job:
    • Right-click Export Jobs and select New Export Job.
    • Choose Full, Schema, Table, or Tablespace Export.
    • Configure the directory, file name, and options (compression, parallelism, etc.).
    • Click Start to export.

Method 2: Export Schema/Tables Using SQL Developer GUI

  1. Connect to the database in SQL Developer.
  2. Go to Tools → Database Export.
  3. Select Export Options:
    • Choose whether to export a full schema, specific tables, or objects.
    • Set output format (SQL script, CSV, etc.).
  4. Choose Save Location:
    • Specify file name and directory for the exported file.
  5. Click Finish to generate the export file.

Method 3: Export Data Only (CSV, Excel, etc.)

  1. Right-click a table and select Export.
  2. Choose the format (CSV, Excel, JSON, XML, etc.).
  3. Specify the file location.
  4. Click NextFinish.

These are some of the methods which can be leveraged for the database export. Try and test it out with these methods and choose the best that suits you.

Wednesday, 12 February 2025

Oracle Fusion SQL Queries and API Payload Samples

Here are some of the SQL queries for extracting data from Oracle Fusions and API payload samples for integrations.

Note : These are for reference only. please refer to the latest updates/upgrades to these API's

SQL Queries for Common Fusion Data Extraction

1. Fetching Supplier Details (Fusion Financials - ERP)

SELECT supplier_name, supplier_number, taxpayer_id, status
FROM POZ_SUPPLIERS_VL WHERE status = 'ACTIVE' ORDER BY supplier_name;

2. Fetching Employee Details (Fusion HCM)

SELECT person_id, full_name, person_number, email_address, hire_date, assignment_status
FROM PER_PEOPLE_F_VL WHERE TRUNC(hire_date) >= TRUNC(SYSDATE) - 30 -- Last 30 days hires ORDER BY hire_date DESC;

3. Fetching Invoice Details (Fusion ERP - Payables)

SELECT invoice_id, invoice_number, supplier_name, invoice_amount, invoice_status
FROM AP_INVOICES_V WHERE invoice_status = 'Validated' ORDER BY invoice_date DESC;

4. Fetching Purchase Orders (Fusion SCM)

SELECT po_header_id, po_number, supplier_name, total_amount, po_status
FROM PO_HEADERS_VL WHERE po_status = 'APPROVED' ORDER BY creation_date DESC;

5. Fetching Journal Entries (Fusion Financials - GL)

SELECT je_header_id, je_batch_id, je_category, ledger_name, period_name, accounted_dr, accounted_cr
FROM GL_JE_HEADERS WHERE period_name = 'JAN-2025';

Oracle Fusion API Payload Samples

Fusion APIs are REST-based and use JSON for communication. Below are examples of how to interact with them.

1. Fetching Supplier Data via REST API

API Endpoint:

http
GET /fscmRestApi/resources/11.13.18.05/suppliers?q=SupplierName='ABC Corp'

Response (JSON):

json
{ "items": [ { "SupplierId": 100000, "SupplierName": "Corp", "SupplierNumber": "SUP12345", "TaxpayerId": "TX987654", "Status": "ACTIVE" } ] }

2. Fetching Employee Data via HCM REST API

API Endpoint:

http
GET /hcmRestApi/resources/11.13.18.05/workers?q=PersonNumber='EMP00123'

Response (JSON):

{
"items": [ { "PersonId": 10000, "FullName": "Doe", "PersonNumber": "EMP00123", "Email": "doe@example.com", "HireDate": "2024-01-15", "AssignmentStatus": "ACTIVE" } ] }

3. Creating a New Supplier Using REST API

API Endpoint:

http
POST /fscmRestApi/resources/11.13.18.05/suppliers

Request Body (JSON):

json
{
"SupplierName": "Tech Solutions", "SupplierNumber": "SUP123", "TaxpayerId": "TX55555", "Status": "ACTIVE" }

Response (JSON):

json
{ "SupplierId": 1111, "SupplierName": "Tech Solutions", "SupplierNumber": "SUP1111", "Status": "ACTIVE" }

4. Creating an Invoice via REST API

API Endpoint:

http
POST /fscmRestApi/resources/11.13.18.05/invoices

Request Body (JSON):

json
{ "InvoiceNumber": "INV1111", "SupplierNumber": "SUP12345", "InvoiceAmount": 1200, "Currency": "USD", "InvoiceDate": "2024-02-10" }

Response (JSON):

json
{ "InvoiceId": 45678, "InvoiceNumber": "INV1111", "InvoiceAmount": 1200, "Status": "Validated" }

5.Updating Employee Email via HCM REST API

API Endpoint:

http
PATCH /hcmRestApi/resources/11.13.18.05/workers/10234

Request Body (JSON):

json
{ "Email": "doe@newcompany.com" }

Response (JSON):

json
{ "PersonId": 111, "FullName": "Doe", "Email": "doe@newcompany.com" }

Oracle Fusion : Tables, Views & Integration Strategies

Oracle Fusion Applications store data in underlying tables, but direct table access is not typically provided to end-users. Instead, Oracle offers public views and APIs for data retrieval and integration.

1. Base Tables vs. Interface Tables

Base tables store transactional and master data (e.g., HCM_EMPLOYEES, PO_HEADERS_ALL).

Interface tables (staging tables) facilitate data imports (e.g., AP_INVOICES_INTERFACE for invoices).

OTBI and BIP Reports for extracting Fusion data without direct table access.

2. Key Tables & Views for Common Modules

Here are some critical tables and views in Oracle Fusion for different modules:

Fusion Financials (ERP)
  • AP_INVOICES_ALL – Stores invoice details.
  • AP_SUPPLIERS – Holds supplier information.
  • GL_JE_HEADERS & GL_JE_LINES – Stores journal entries.
  • Fusion View: AP_INVOICE_V (for secured access).
Fusion HCM (Human Capital Management)
  • PER_ALL_PEOPLE_F – Employee records (date-tracked).
  • PER_JOBS_F – Job details.
  • Fusion View: PER_PEOPLE_F_VL (secured employee data view).
Fusion SCM (Supply Chain Management)
  • PO_HEADERS_ALL – Purchase order details.
  • INV_MATERIAL_TXNS – Inventory transactions.
  • Fusion View: PO_HEADERS_VL.

3. Extracting Data from Oracle Fusion

Here are some integration approaches:

A. Using BI Publisher (BIP) for Table Extraction
  1. Create a Data Model in BIP.
  2. Use SQL queries to fetch data from Fusion Views (e.g., SELECT * FROM PER_PEOPLE_F_VL).
  3. Schedule reports for automated data extraction in CSV/Excel format.
B. File-Based Data Import (FBDI) for Bulk Data Loads
  • Oracle provides FBDI templates for mass data uploads.
  • Data is staged in interface tables before processing into base tables.
  • Example: Importing invoices using AP_INVOICES_INTERFACE.
C. Oracle REST & SOAP APIs for Real-time Integration
  • Use REST APIs for fetching Fusion data:
    GET /fscmRestApi/resources/11.13.18.05/suppliers
  • Example: Fetching employee details via HCM REST API:
    GET /hcmRestApi/resources/11.13.18.05/workers?q=PersonNumber='44444'
D. Oracle Integration Cloud (OIC) for Automated Sync
  • OIC connects Oracle Fusion with external apps.
  • Prebuilt adapters for ERP, HCM, and SCM.
  • Uses orchestration workflows for automation.