How to Identify Slow SQL Queries in Oracle (Practical Guide for Performance Tuning)
Performance tuning is one of the most valuable skills when working with Oracle Database. Slow SQL queries can degrade application performance, increase CPU usage, and frustrate users.
In this guide, we’ll cover how to identify slow SQL using:
V$SQLV$SQLAREAAWR Reports
Simple and effective tuning ideas
๐ 1. Using V$SQL to Find Slow Queries
The V$SQL view contains detailed statistics about SQL statements currently in the shared pool.
✅ Find Top Slow Queries by Elapsed Time
SELECT
sql_id,
executions,
elapsed_time/1000000 AS elapsed_sec,
cpu_time/1000000 AS cpu_sec,
disk_reads,
buffer_gets,
sql_text
FROM
v$sql
ORDER BY
elapsed_time DESC
FETCH FIRST 10 ROWS ONLY;
๐ Key Columns Explained
elapsed_time→ total execution timecpu_time→ CPU usagedisk_reads→ physical I/O (slow operations)buffer_gets→ logical readsexecutions→ how many times query ran
๐ Focus on:
High elapsed time
High disk reads
High buffer gets
๐ฏ Find Slow Queries Per Execution
SELECT
sql_id,
executions,
elapsed_time / DECODE(executions,0,1,executions) / 1000000 AS avg_time_sec,
sql_text
FROM
v$sql
ORDER BY
avg_time_sec DESC;
๐ This helps identify queries that are slow individually (not just frequent ones).
๐ 2. Using V$SQLAREA (Aggregated View)
V$SQLAREA provides aggregated statistics for SQL statements.
✅ Top Queries by Resource Usage
SELECT
sql_id,
executions,
buffer_gets,
disk_reads,
elapsed_time/1000000 AS elapsed_sec,
sql_text
FROM
v$sqlarea
ORDER BY
buffer_gets DESC
FETCH FIRST 10 ROWS ONLY;
๐ When to Use V$SQLAREA
To identify heavy queries overall
When multiple executions are involved
For workload-level analysis
๐ 3. Using AWR Reports (Advanced Method)
๐ What is AWR?
AWR (Automatic Workload Repository) stores historical performance data in Oracle Database.
๐ ️ Generate AWR Report
@$ORACLE_HOME/rdbms/admin/awrrpt.sql
๐ Key Sections in AWR Report
๐ฅ Top SQL by Elapsed Time
Shows queries consuming most time
๐ฝ Top SQL by Disk Reads
Identifies I/O-heavy queries
๐ง Top SQL by CPU Time
CPU-intensive SQL
๐ What to Look For
High elapsed time SQL
SQL with high executions
SQL with high I/O
๐ AWR is best for:
Historical analysis
Production troubleshooting
Identifying trends
๐จ 4. Common Signs of Slow SQL
High response time
High CPU usage
Excessive disk reads
Full table scans
Long-running sessions
๐ ️ 5. Simple SQL Tuning Ideas
✅ 1. Use Proper Indexes
CREATE INDEX idx_emp_name ON emp(name);
๐ Helps avoid full table scans
✅ 2. Avoid SELECT *
-- Bad
SELECT * FROM employees;
-- Good
SELECT id, name FROM employees;
๐ Reduces unnecessary data retrieval
✅ 3. Use Bind Variables
SELECT * FROM emp WHERE emp_id = :id;
๐ Improves parsing efficiency
✅ 4. Check Execution Plan
EXPLAIN PLAN FOR
SELECT * FROM emp WHERE id = 100;
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);
๐ Understand how Oracle executes query
✅ 5. Reduce Full Table Scans
Add indexes
Use selective conditions
✅ 6. Optimize Joins
Use proper join conditions
Avoid Cartesian joins
✅ 7. Gather Statistics
EXEC DBMS_STATS.GATHER_SCHEMA_STATS('HR');
๐ Helps optimizer choose better plans
๐งช 6. Real-World Example
Problem Query
SELECT * FROM orders WHERE customer_id = 101;
Issue
No index on
customer_idFull table scan
Solution
CREATE INDEX idx_orders_cust ON orders(customer_id);
๐ Result:
Query becomes significantly faster
⚖️ V$SQL vs V$SQLAREA vs AWR
| Feature | V$SQL | V$SQLAREA | AWR |
|---|---|---|---|
| Data Type | Real-time | Aggregated | Historical |
| Scope | Individual SQL | Combined SQL | System-wide |
| Use Case | Immediate tuning | Workload analysis | Deep performance analysis |
๐ Conclusion
Identifying slow SQL in Oracle Database involves a combination of:
Real-time views (
V$SQL,V$SQLAREA)Historical analysis (AWR Reports)
Once identified, applying simple tuning techniques like indexing, query optimization, and statistics gathering can dramatically improve performance.
๐ Next Topics You Can Cover
Since you're building performance-focused content, you can also write about:
SQL Execution Plans deep dive
Index types in Oracle
Wait events and session tuning
ASH vs AWR
Just tell me if you want those ready ๐






0 comments:
Post a Comment