10 Advanced Oracle DB Tuning Techniques
10 Advanced Oracle DB Tuning Techniques
1. Poor-performing scenario
2. Step-by-step optimization process
3. Optimized implementation
4. Impact & validation (SQLs, procedures, shell script)
Optimization Process
Impact
Optimization Process
Impact
Optimization Process
Impact
Optimization Process
--1. Implement bulk collect and FORALL:
FORALL i IN 1..l_customers.COUNT
UPDATE accounts SET balance = balance * 1.05
WHERE customer_id = l_customers(i);
COMMIT;
END;
/
--2. Add parallel hint:
COMMIT;
END;
/
Impact
Optimization Process
Impact
Optimization Process
Impact
Optimization Process
Optimization Process
Impact
-- Long-running processing
DBMS_LOCK.SLEEP(30); -- Simulate work
-- Long-running processing
DBMS_LOCK.SLEEP(30); -- Simulate work
IF %ROWCOUNT = 0 THEN
RAISE_APPLICATION_ERROR(-20001, 'Order was modified by another session');
END IF;
COMMIT;
END;
/
DBMS_AQADM.CREATE_QUEUE(
queue_name => 'order_queue',
queue_table => 'order_queue_tab');
-- Enqueue procedure
CREATE OR REPLACE PROCEDURE enqueue_order(p_order_id NUMBER) IS
l_enqueue_options DBMS_AQ.ENQUEUE_OPTIONS_T;
l_message_props DBMS_AQ.MESSAGE_PROPERTIES_T;
l_message_handle RAW(16);
l_message SYS.AQ$_JMS_TEXT_MESSAGE;
BEGIN
l_message := SYS.AQ$_JMS_TEXT_MESSAGE.construct;
l_message.set_text(p_order_id);
DBMS_AQ.ENQUEUE(
queue_name => 'order_queue',
enqueue_options => l_enqueue_options,
message_properties => l_message_props,
payload => l_message,
msgid => l_message_handle);
COMMIT;
END;
/
-- Dequeue procedure
CREATE OR REPLACE PROCEDURE dequeue_orders IS
l_dequeue_options DBMS_AQ.DEQUEUE_OPTIONS_T;
l_message_props DBMS_AQ.MESSAGE_PROPERTIES_T;
l_message_handle RAW(16);
l_message SYS.AQ$_JMS_TEXT_MESSAGE;
l_order_id NUMBER;
BEGIN
l_dequeue_options.wait := DBMS_AQ.NO_WAIT;
l_dequeue_options.navigation := DBMS_AQ.FIRST_MESSAGE;
l_order_id := TO_NUMBER(l_message.get_text());
-- Process order
UPDATE orders SET status = 'PROCESSED'
WHERE order_id = l_order_id;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
EXIT;
END;
END LOOP;
END;
/
Impact
Optimization Process
Impact
Bash
#!/bin/bash
# Oracle Performance Health Check Script
# Configuration
DB_USER="perf_user"
DB_PASS="secure_password"
DB_HOST="oracle-db.example.com"
DB_PORT="1521"
DB_SERVICE="ORCLPDB1"
OUTPUT_DIR="/tmp/oracle_perf_$(date +%Y%m%d_%H%M%S)"
mkdir -p $OUTPUT_DIR
# *Plus connection string
CONN_STR="$DB_USER/$DB_PASS@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(
HOST=$DB_HOST)(PORT=$DB_PORT))(CONNECT_DATA=(SERVICE_NAME=$DB_SE
RVICE)))"
spool ${OUTPUT_DIR}/ash_report.html
SELECT output FROM TABLE(DBMS_WORKLOAD_REPOSITORY.ash_report_html(
(SELECT dbid FROM v\$database),
(SELECT instance_number FROM v\$instance),
TO_DATE(TO_CHAR(SYSDATE-1/24, 'DD-MON-YYYY HH24:MI:SS'), 'DD-MON-
YYYY HH24:MI:SS'),
SYSDATE,
SYSDATE,
SYSDATE-1/24,
SYSDATE,
'text',
'html'
));
spool off
EOF
This comprehensive guide covers 10 critical Oracle performance tuning techniques with detailed
before-and-after scenarios, scripts, and a complete shell script for performance analysis. Each
technique demonstrates measurable improvements in query performance, resource utilization,
and overall database efficiency.