Database AdministrationUnit 59 min read
Database Performance Tuning & AWR Reports: Metrics, Tools & Optimization
Unit 5 of Database Administration covers Oracle’s Automatic Workload Repository (AWR), performance tuning methodologies, SQL optimization techniques, and hands-on use of AWR reports to diagnose bottlenecks—critical for DBAs managing high-transaction systems like eSewa or Ncell.
TAKEAWAYS:
- AWR captures 1-hour snapshots of database metrics (CPU, I/O, SQL) to identify performance trends and bottlenecks.
- Top 5 SQL statements in AWR’s “SQL Ordered by Elapsed Time” reveal the most resource-intensive queries needing optimization.
- Wait events (e.g.,
db file sequential read) pinpoint I/O or lock contention—fixes include indexing or partitioning. - Automatic Database Diagnostic Monitor (ADDM) in AWR provides actionable recommendations (e.g., “Add index on
customer_id”). - Flashback Database and Undo retention settings directly impact performance tuning for recovery scenarios.
- Real-world tie: Ncell’s billing system uses AWR to detect slow
INSERTqueries during peak hours (e.g., 6–9 PM) and pre-optimizes indexes.
1. Why Performance Tuning? The DBA’s Silent Battle
Databases slow down due to three killers:
- Poorly written SQL (e.g.,
SELECT * FROM large_tablewithoutWHERE). - Missing indexes (forcing full-table scans).
- Resource contention (CPU, memory, or I/O bottlenecks).
AWR (Automatic Workload Repository) is Oracle’s built-in time machine: it records metrics every 60 minutes (configurable) and lets you compare snapshots to spot regressions. Think of it as a flight data recorder for databases.
2. AWR Reports: The DBA’s Dashboard
AWR reports are generated via:
@?/rdbms/admin/awrrpt.sql
Key sections (visualized in the report):
| Section | What It Shows | Example Fix |
|---|---|---|
| Instance Efficiency | CPU, I/O, memory utilization trends | Add more RAM if buffer cache hit % < 90% |
| Top 5 SQL by Elapsed Time | Slowest queries (e.g., ORDER BY on unsorted columns) |
Rewrite with INDEX or PARTITION BY |
| Wait Events | Blockers like latch free waits (locks) |
Increase UNDO_RETENTION or add ROWID |
| ADDM Findings | Automated recommendations (e.g., “Add index”) | Run CREATE INDEX idx_customer_name ON customers(name); |
MERMAID: AWR Report Workflow
flowchart TD
A["AWR Snapshots (Hourly)"] -->|"Stored in SYSAUX tablespace"| B["AWR Repository"]
B --> C["Generate Report via awrrpt.sql"]
C --> D["Analyze: Top SQL/Waits"]
D --> E["ADDM: Recommend Fixes"]
E --> F["Implement: Indexes/Partitioning"]
F --> G["Verify: Next AWR Report"]Worked Example: eSewa’s Payment Query Slowdown
- Problem: During Diwali,
SELECT * FROM transactions WHERE status='completed'took 12 seconds (vs. 0.2s normally). - AWR Insight: The query was a full scan on a 50M-row table with no index.
- Fix: Added a composite index:
CREATE INDEX idx_txn_status ON transactions(status, amount); - Result: Query time dropped to 0.8 seconds.
3. Performance Tuning Techniques
A. SQL Optimization
Common Anti-Patterns (and fixes):
| Bad SQL | Problem | Fix |
|---|---|---|
SELECT * FROM orders |
Reads all columns (wastes I/O) | SELECT order_id, customer_id FROM orders |
WHERE TO_CHAR(date, 'YYYY') = '2023' |
Function on column prevents index use | WHERE date >= TO_DATE('2023-01-01', 'YYYY-MM-DD') |
| Nested loops with large tables | O(n²) complexity | Use HASH JOIN or MERGE JOIN |
MERMAID: SQL Execution Plan
graph LR
A["Query: SELECT * FROM orders WHERE customer_id = 100"] --> B["Full Table Scan"]
B --> C["10,000 rows scanned"]
C --> D["CPU: 500ms, I/O: 200ms"]
D --> E["Fix: Add INDEX on customer_id"]
E --> F["Index Range Scan\nCPU: 5ms, I/O: 1ms"]B. Indexing Strategies
- B-Tree Indexes: Best for equality (
=) and range (>,<) queries.CREATE INDEX idx_customer_email ON customers(email); - Bitmap Indexes: Ideal for low-cardinality columns (e.g.,
gender,status). - Function-Based Indexes: For queries using functions:
CREATE INDEX idx_upper_name ON employees(UPPER(last_name));
Worked Example: NEPSE Stock Data
- Problem:
SELECT symbol, price FROM stocks WHERE price > 1000scanned 1M rows. - Fix: Created a function-based index on
price:CREATE INDEX idx_high_value ON stocks(FLOOR(price/1000)); - Result: Scan reduced to 500 rows.
C. Partitioning
Split large tables by range, list, or hash to avoid full scans.
Example: Daraz Order History
CREATE TABLE orders (
order_id NUMBER,
customer_id NUMBER,
order_date DATE
) PARTITION BY RANGE (order_date) (
PARTITION p_2023 VALUES LESS THAN (TO_DATE('2024-01-01', 'YYYY-MM-DD')),
PARTITION p_2024 VALUES LESS THAN (TO_DATE('2025-01-01', 'YYYY-MM-DD'))
);
Benefits:
- Queries on
p_2023ignorep_2024data. - Easier backups (partition-level).
4. Advanced Tools in Oracle
| Tool | Purpose | Command/Example |
|---|---|---|
| ADDM | Automated diagnostics | EXEC DBMS_ADDM.ADDM_MAINTAIN_SNAPSHOT; |
| ASH (Active Session History) | Real-time wait analysis | SELECT * FROM V$ACTIVE_SESSION_HISTORY; |
| SQL Tuning Advisor | Recommends SQL rewrites | BEGIN DBMS_SQLTUNE.TUNE_SQL('SQL_ID'); END; |
| AWR Baselines | Compare performance over time | CREATE_BASELINE baseline1 USING SNAPSHOT... |
MERMAID: ADDM Workflow
sequenceDiagram
participant DBA
participant AWR
participant ADDM
DBA->>AWR: Take snapshot (SNAP_ID=123)
AWR->>ADDM: Analyze snapshots 120-123
ADDM->>DBA: "Findings:\n1. Missing index on INV_CUSTOMERS.NAME\n2. High latch contention on UNDO tablespace"
DBA->>ADDM: Implement fixes
ADDM->>DBA: Verify improvement in next snapshot5. Real-World Applications
Case 1: Khalti’s Transaction Logs
- Challenge: During Dashain, Khalti’s
INSERT INTO transactionsqueries caused lock contention on theTRANSACTIONStable. - AWR Insight:
enq: TX - row lock contentionwas the top wait event. - Fix:
- Increased
UNDO_RETENTIONto 7200 seconds. - Added a partition by range on
transaction_time.
- Increased
- Result: Transaction throughput increased by 40%.
Case 2: NTC’s Customer Billing
- Problem: Monthly billing reports (
SELECT * FROM customers JOIN bills) took 3 hours. - AWR Finding: The query was a cartesian product due to missing joins.
- Fix: Added a hash join hint:
SELECT /*+ HASH_SJ */ * FROM customers JOIN bills ON customers.id = bills.customer_id; - Result: Report time dropped to 12 minutes.
Case 3: Pathao Driver App
- Issue: Driver location updates (
UPDATE driver_locations SET lat=?, lng=?) caused highdb file scattered readwaits. - Solution:
- Created a composite index on
(driver_id, last_updated). - Used batch updates (100 rows at once) instead of row-by-row.
- Created a composite index on
6. Common Pitfalls & How to Avoid Them
| Mistake | Impact | Solution |
|---|---|---|
| Over-indexing | Slower INSERT/UPDATE (index maintenance) |
Monitor index usage in AWR; drop unused indexes. |
Not monitoring UNDO_RETENTION |
Long-running transactions fail | Set UNDO_RETENTION = 900 (15 mins). |
Ignoring PGA_AGGREGATE_TARGET |
Sort operations spill to disk | Increase PGA size for large sorts. |
Using SELECT * in production |
Network overhead | Fetch only needed columns. |
Exam Tip
AWR Questions:
- Memorize the 4 key sections of an AWR report (Instance Efficiency, Top SQL, Wait Events, ADDM).
- Know how to generate a report (
awrrpt.sql) and interpret ADDM findings. - Example Answer:
"The AWR report’s ‘Top 5 SQL by CPU’ section shows that
SELECT product_id FROM inventory WHERE stock < 10consumes 45% CPU. The wait eventdb file sequential readindicates missing indexes. The ADDM recommends creating an index onINVENTORY(STOCK), which would reduce full-table scans."
Performance Tuning:
- Always start with AWR before guessing fixes.
- Indexing: Know when to use B-Tree vs. Bitmap.
- Partitioning: Explain range vs. list partitioning with an example.
Real-World Scenarios:
- eSewa: Use AWR to detect slow payment queries during festivals.
- Ncell: Partition call logs by month to speed up billing reports.
- Daraz: Optimize
ORDER BYclauses for product searches.
Commands to Remember:
-- Check AWR snapshots SELECT snap_id, begin_interval_time, end_interval_time FROM dba_hist_snapshot; -- Generate AWR report @?/rdbms/admin/awrrpt.sql -- ADDM recommendations SELECT * FROM dba_advisor_findings;
Final Visual Summary
mindmap
root((Database Performance Tuning))
AWR Reports
Snapshots
ADDM Findings
Top SQL/Waits
SQL Optimization
Indexes
Partitioning
Query Rewrites
Tools
ASH
SQL Tuning Advisor
Real-World
eSewa: Payment Queries
Ncell: Billing Reports
Khalti: Transaction LogsBased on the TU BSc CSIT syllabus for Database Administration (CSC414), unit 5.
Discussion
Loading…