DEV Community

Pranav Bakare
Pranav Bakare

Posted on

Detailed Explanation of DBMS PROFILER with its Steps

Detailed Explanation of START Profiling, EXECUTE PL/SQL Block, and STOP Profiling in DBMS Profiler

The DBMS_PROFILER package provides utilities for profiling the execution of PL/SQL code. Profiling helps identify performance bottlenecks by tracking execution times and other relevant metrics. Below is a breakdown of the profiling process, including how to start profiling, execute the PL/SQL block, and stop profiling, as well as the role of each procedure.


  1. START Profiling

Purpose:
The DBMS_PROFILER.START_PROFILING procedure starts the profiling session. When profiling is started, the Oracle Database begins tracking execution data for all subsequent PL/SQL code until profiling is stopped.

Syntax:

DBMS_PROFILER.START_PROFILING;

Explanation:

This procedure is used to initiate the collection of profiling data, including time spent executing the code, the number of executions of specific lines or statements, and other performance metrics.

Once profiling has started, all PL/SQL statements executed will be tracked by the profiler, including any subsequent procedure calls, loops, and SQL statements.

Profiling Data is stored in the internal profiling tables such as PROFILER_RUNS, PROFILER_LINES, and PROFILER_DATA.

Usage:
You typically call this procedure before executing the PL/SQL block that you want to profile. It is important to call this only once per profiling session. Any execution after calling START_PROFILING will be profiled until you stop profiling.

Example:

BEGIN
DBMS_PROFILER.START_PROFILING;
END;
/

In this example, the profiling session begins, and any code executed after this point will be profiled.


  1. EXECUTE PL/SQL Block

Purpose:
After starting profiling, you execute the PL/SQL block or the specific code you want to profile. The execution of the PL/SQL block will generate profiling data that is collected during the session.

Explanation:

The PL/SQL block can contain any executable PL/SQL code, such as anonymous blocks, procedures, or functions.

The profiler collects data while the PL/SQL code is running. This data includes metrics such as:

The total execution time for each line of code.

The number of executions of each line.

The total time spent in each line or SQL statement.

The time spent in any subprograms (procedures, functions).

Usage:
After starting the profiler, execute the PL/SQL block whose performance you want to analyze. This is usually the part of the code that you believe may have performance issues.

Example:

BEGIN
-- PL/SQL code to be profiled
FOR i IN 1..10000 LOOP
NULL; -- Replace with actual operations to profile
END LOOP;
END;
/

In this case, the code inside the FOR loop will be profiled. The DBMS_PROFILER package will track how many times each line inside the loop is executed and the time spent on those lines.


  1. STOP Profiling

Purpose:
The DBMS_PROFILER.STOP_PROFILING procedure stops the profiling session. This procedure signals the profiler to stop tracking execution data, and the collected data is finalized and saved in the profiling tables.

Syntax:

DBMS_PROFILER.STOP_PROFILING;

Explanation:

Stopping profiling is necessary to prevent the profiler from continuing to track data once the code block has completed.

After calling STOP_PROFILING, you can query the profiling tables to analyze the collected data, such as how much time was spent on each line of code, how many times each line was executed, and whether there were any errors.

The STOP_PROFILING procedure allows for efficient tracking of execution time and prevents overhead once profiling is no longer required.

Usage:
You should call this procedure after the PL/SQL block finishes execution, signaling that you want to stop collecting performance data.

Example:

BEGIN
DBMS_PROFILER.STOP_PROFILING;
END;
/

After executing your PL/SQL block and calling STOP_PROFILING, you can then query the profiling tables to analyze the results.


  1. How DBMS_PROFILER Works After START_PROFILING and STOP_PROFILING

Once the profiling session is complete (i.e., after the STOP_PROFILING is called), the DBMS_PROFILER package saves the execution metrics into system tables. This data can then be used to identify performance bottlenecks in the PL/SQL code.

The Data Captured:

Execution Time: The total amount of time spent on executing each line of code.

Call Count: The number of times a specific line of code was executed.

Statements Executed: The number of times SQL statements or PL/SQL code blocks were executed.

Errors: Any runtime errors encountered during the profiling session.

Key Profiling Tables

  1. PROFILER_RUNS: Contains information about each profiling run (e.g., start and end times).

  2. PROFILER_LINES: Contains a breakdown of time spent on each line of PL/SQL code.

  3. PROFILER_DATA: Contains statement-level profiling data (e.g., for SQL queries or function calls).

  4. PROFILER_ERRORS: Contains error information, such as when an exception is raised during profiling.


Steps to Profile a PL/SQL Block Using DBMS_PROFILER

  1. Start Profiling:

BEGIN
DBMS_PROFILER.START_PROFILING;
END;
/

  1. Execute the PL/SQL Block:

BEGIN
-- Your PL/SQL block or procedure to profile
FOR i IN 1..10000 LOOP
-- Example operation to be profiled
NULL;
END LOOP;
END;
/

  1. Stop Profiling:

BEGIN
DBMS_PROFILER.STOP_PROFILING;
END;
/

  1. Analyze Profiling Data:

After stopping the profiler, query the profiling tables to analyze the performance data:

SELECT * FROM DBMS_PROFILER.PROFILER_RUNS;
SELECT * FROM DBMS_PROFILER.PROFILER_LINES;
SELECT * FROM DBMS_PROFILER.PROFILER_DATA;
SELECT * FROM DBMS_PROFILER.PROFILER_ERRORS;


Best Practices for Using DBMS_PROFILER

Use in a Controlled Environment: Profiling adds overhead to execution, so it should be used in a development or test environment, not in production.

Profile Specific Code: Profile only the parts of the code that are suspected of having performance issues to avoid unnecessary profiling overhead.

Query Results Frequently: After stopping the profiling, query the results to identify bottlenecks and optimize them.


Summary

START Profiling begins tracking the execution metrics for your PL/SQL code.

Execute the PL/SQL Block performs the code whose performance you want to monitor.

STOP Profiling ends the profiling session and stores the data for later analysis.

Profiling data is stored in internal tables like PROFILER_RUNS, PROFILER_LINES, PROFILER_DATA, and PROFILER_ERRORS.

Top comments (0)