Lesson 31 — Analyzing SQL and Optimizing Access Paths (ภาษาไทย)
🧠 ภาพจำก่อนอ่าน: 🗄️ Data → ⚙️ Process → 🩺 Health
🎨 Visual Guide ของวิชานี้ — ภาพช่วยจำ ไม่แทนคำสั่งและเนื้อหาจริง
Monitoring & Troubleshooting · Lab 13
Curriculum: Oracle University 19c Administration Workshop Lesson 31. Original Thai guide, not reproduced from proprietary material. Page-level Student Guide check and actual Oracle testing: PENDING. SQL examples read existing metadata only;
EXPLAIN PLANmay write PLAN_TABLE rows so it is not part of read-only lab.
- อ่าน SQL execution plan จาก operation/access predicates/cardinality/cost
- เปรียบเทียบ full table scan, index access and join methods โดยใช้ estimated/actual row data
- เข้าใจ statistics, skew, histograms, bind variables, plan changes และ cardinality errors
- ทำการวิเคราะห์อย่างปลอดภัยก่อน index/SQL rewrite/hints/stats change
1. SQL optimizer mental model
หัวข้อที่มีชื่อว่า “1. SQL optimizer mental model”flowchart TD S[SQL statement and bind values] --> O[Optimizer] ST[Table, column and index statistics] --> O O --> P[Execution plan] P --> A[Access paths] P --> J[Join order and join methods] A --> R[Runtime waits and rows] J --> R
Oracle’s cost-based optimizer ประเมินต้นทุนของหลาย candidate plans จาก object statistics, predicates, estimated rows และ available access methods. Cost ไม่ใช่ milliseconds และแผนที่ cost ต่ำกว่าไม่ได้การันตี response time ต่ำกว่าในทุก runtime.
2. Common access paths
หัวข้อที่มีชื่อว่า “2. Common access paths”| Operation | ทำอะไร | เหมาะเมื่อ |
|---|---|---|
| TABLE ACCESS FULL | อ่าน table blocks จำนวนมาก | ต้องคืนหลายแถว หรือ table เล็ก/parallel เหมาะ |
| INDEX UNIQUE SCAN | หา entry ที่ match unique key | Equality predicate บน unique index |
| INDEX RANGE SCAN | อ่านช่วง index keys | Selective range/equality predicates |
| TABLE ACCESS BY INDEX ROWID | ไป table block ตาม rowid จาก index | ต้องคืน columns ที่ไม่ได้อยู่ใน index |
| HASH JOIN | สร้าง/probe hash สำหรับ matching keys | Many-row joins; memory และ spill สำคัญ |
| NESTED LOOPS | lookup inner row source ต่อ outer row | Outer cardinality เล็กและ inner access ดี |
| SORT MERGE JOIN | sort/merge inputs | บาง sorted/range workloads |
อย่าติดป้าย TABLE ACCESS FULL ว่าผิดเสมอ: ถ้าต้องอ่าน 70% ของตาราง ใช้ full scan อาจดีกว่า index lookup+random table I/O.
3. Estimated vs actual cardinality
หัวข้อที่มีชื่อว่า “3. Estimated vs actual cardinality”Optimizer stats เช่น NUM_ROWS, NDV, histogram และ extended statistics มีผลต่อ estimate. หาก estimate 10 rows แต่จริง 2,000,000 rows อาจเลือก join order/index strategy ผิด.
-- Read-only: existing metadata from authorized tables only.SHOW CON_NAMESELECT owner,table_name,num_rows,last_analyzed,stale_statsFROM dba_tab_statisticsWHERE owner='LAB_OWNER' AND object_type='TABLE'ORDER BY table_name;
SELECT sql_id,child_number,plan_hash_value,executions,elapsed_timeFROM v$sqlWHERE executions > 0 AND parsing_schema_name='LAB_OWNER'ORDER BY elapsed_time DESC FETCH FIRST 10 ROWS ONLY;Important: V$SQL.ELAPSED_TIME สะสมตลอด lifetime ของ child cursor (microseconds ใน documentation); ต้องเปรียบเทียบ deltas/time window, executions และ variance ก่อนสรุป. Output DBA_TAB_STATISTICS.NUM_ROWS เป็น optimizer statistic ไม่ใช่ transactional COUNT(*) แบบ real-time.
4. Display existing cursor plan, without EXPLAIN PLAN
หัวข้อที่มีชื่อว่า “4. Display existing cursor plan, without EXPLAIN PLAN”-- Example only: replace literal values with an approved SQL_ID and child number.-- Requires suitable SELECT grants and DBMS_XPLAN execution permissions.SELECT plan_table_outputFROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR( sql_id => 'abcdef1234567', cursor_child_no => 0, format => 'TYPICAL'));DISPLAY_CURSOR อ่าน execution plan ที่อยู่ใน cursor cache. ผลอาจไม่มีเมื่อ cursor aged out หรือสิทธิ์ไม่เพียงพอ. รูปแบบ ALLSTATS LAST ใช้ runtime plan stats ได้เมื่อมีการเก็บ execution statistics; อย่าสมมติว่า A-Rows มีให้ทุกแผน.
License rule: DISPLAY_AWR และ AWR/ASH/ADDM ต้องตรวจ Oracle Diagnostics Pack entitlement. SQL Tuning Advisor, SQL Profiles และ Real-Time SQL Monitoring มักอยู่ภายใต้ Tuning Pack; ควรตรวจ 26ai Licensing Manual ตาม edition/deployment ก่อนใช้งาน.
5. SQL tuning workflow
หัวข้อที่มีชื่อว่า “5. SQL tuning workflow”- ระบุ actual user impact, target SQL_ID, average/p95 latency and workload window.
- Capture baseline plan_hash_value, child cursor, parameter/bind-sensitive behavior and stats age.
- Compare estimated vs actual rows, key operations, predicates and join choice.
- Correlate wait events, buffer gets, physical reads, I/O latency and application fetch patterns.
- Simulate change in sandbox (index, rewrite, stats refresh, bind use) one variable at a time.
- Measure consistent test data/run plans; consider DML overhead of indexes, storage and locking.
- Have rollback and post-change verification before production deployment.
6. Case studies
หัวข้อที่มีชื่อว่า “6. Case studies”Case A: query has an index but optimizer chooses full scan. If predicate selects most rows, full scan may be correct; inspect NDV, histogram, estimated rows, table size and projection.
Case B: SQL slow after statistics maintenance. Compare plan hash and input values before/after; stats can change plan without code change. Never indiscriminately lock statistics as a first reaction.
Case C: high CPU? SQL with high cumulative elapsed_time could be waiting on I/O/locks rather than CPU. Compare timed waits and CPU accounting; lifetime cumulative totals cannot prove a recent incident.
Self-check
หัวข้อที่มีชื่อว่า “Self-check”- Cost vs actual elapsed time แตกต่างกันอย่างไร?
- เมื่อใด full table scan จึงเหมาะ?
- ทำไม bind-sensitive SQL อาจมี child cursors หลายตัว?
- จะตรวจว่าการเพิ่ม index ช่วย SELECT แต่ทำให้ DML แย่ลงอย่างไร?
- เพราะเหตุใด
EXPLAIN PLANไม่เท่ากับแผนที่ cursor ใช้งานจริง?
References
หัวข้อที่มีชื่อว่า “References”- Oracle 26ai Optimizer Concepts
- Oracle 26ai Execution Plan Display
- Oracle 26ai DBMS_XPLAN
- Oracle 26ai Licensing Information
Source-objective deepening — SQL Tuning Advisor and SQL Access Advisor
หัวข้อที่มีชื่อว่า “Source-objective deepening — SQL Tuning Advisor and SQL Access Advisor”วัตถุประสงค์ Lesson 31 รวมการรู้จัก tools ที่ช่วย tune SQL และ workload. ห้ามสรุปว่าแค่ใช้ DBMS_XPLAN เท่ากับใช้ SQL Advisors
- SQL Tuning Advisor: วิเคราะห์ SQL statement/high-load SQL และเสนอ SQL profile, statistics, index หรือ SQL rewrite candidates ตาม feature; ต้องตรวจ Tuning Pack licensing ใน environment จริง. Recommendation ไม่ใช่ auto-safe change.
- SQL Access Advisor: วิเคราะห์ workload เพื่อเสนอ physical-design alternatives เช่น indexes/materialized views/partitioning ตาม features/edition ที่รองรับ; ต้องทดสอบผลรวมของ workload ไม่ optimize แค่ query เดียว. Check licensing and supported feature/version before calls.
- Methodology: identify most expensive SQL by time/CPU/IO → analyze optimizer stats/cardinality and actual plan → evaluate advisor suggestion → test individually → test whole application/workload → document adverse effects on DML, storage, concurrency and rollback
- Stats management:
DBMS_STATSworkflows มีผลต่อ optimizer plan. ค่าที่ stale หรือ histogram ที่เลือกไม่เหมาะอาจกระทบ cardinality; ห้าม gather stats production แบบไม่วาง window/backup of stats. - Security: ข้อมูล SQL text/binds และ advisor reports อาจมี sensitive literals; อย่าคัดลอกลง public issue logs.
Tabletop: SQL A เร็วขึ้น 40% หลังเพิ่ม index แต่ batch DML B ช้าลง 35%. สรุปผลรวม workload ไม่ใช่ยกเลิกการวัด B; ระบุ plan stability, storage/redo impacts และ backout plan.
References: Oracle SQL Tuning Guide 26ai, Oracle 26ai Licensing.
🔎 Official-document validation — Cached Cursor Plan and Actual Rows (2026-10-21)
หัวข้อที่มีชื่อว่า “🔎 Official-document validation — Cached Cursor Plan and Actual Rows (2026-10-21)”DBMS_XPLAN.DISPLAY_CURSOR อ่าน Execution Plan ของ Cached Cursor ตาม SQL_ID และ CHILD_NUMBER — ไม่ใช่คำสั่ง Execute Business Query ซ้ำ. Oracle Package Reference ระบุสิทธิ์ READ/SELECT บน V$SQL, V$SQL_PLAN, V$SESSION, V$SQL_PLAN_STATISTICS_ALL (และสิทธิ์ Package ที่เกี่ยวข้อง) ไม่ควรยกระดับเป็น SYSDBA เพื่ออ่านแผน
-- Approved read-only session: replace placeholder with authorized SQL_ID.SELECT plan_table_outputFROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR( sql_id => 'abcdef1234567', cursor_child_no => 0, format => 'TYPICAL'));Plan Semantics: E-Rows คือ Estimate ส่วน A-Rows ใน ALLSTATS LAST ต้องมี Row-source Runtime Statistics ถูกเก็บจริง จึงไม่รับประกันว่ามีทุก Cursor. EXPLAIN PLAN อาจเขียน PLAN_TABLE และ Plan จาก Explain ไม่จำเป็นต้องตรงกับ Child Cursor ที่ใช้จริง
License: DISPLAY_CURSOR (Cache) ต้องแยกจาก DISPLAY_AWR ที่อ่าน AWR; SQL Tuning Advisor/Real-Time SQL Monitoring มี Tuning Pack Requirement ตาม Edition/License. ห้ามเรียก Adviser/Monitoring เพื่อทดลองใน Lab ที่ไม่ยืนยันสิทธิ์ก่อน
Official: 19c DBMS_XPLAN Security Model · Oracle 19c Licensing.
Status: DOC_REVIEWED / CURSOR PLAN NOT EXECUTED.
เมื่ออ่านและฝึกทบทวนจบแล้ว ทำเครื่องหมายเพื่อบันทึกใน Browser นี้