
Database Indexing: Finding Data Faster
Explore how database indexes work under the hood. Understand B+ Tree data structures, disk page lookups, composite index prefix rules, covering indexes, and the write-amplification trade-offs of over-indexing.
Why Full Table Scans Collapse at Scale
Application တစ်ခုကို စတင်ရေးသားချိန်မှာ Database Table ထဲမှာ Rows အရေအတွက် ရာဂဏန်း၊ ထောင်ဂဏန်းသာ ရှိသေးတဲ့အတွက် Index မပါတဲ့ Query တွေဟာလည်း 1ms အတွင်း အဖြေပြန်ပေးနိုင်ပါတယ်။ ဒါပေမဲ့ Production ပေါ်မှာ Rows သန်းပေါင်းများစွာ ရှိလာတဲ့အခါ ရိုးရှင်းတဲ့ WHERE email = 'user@example.com' Query တစ်ခုက စက္ကန့်ပေါင်းများစွာ ကြာမြင့်သွားပြီး Database CPU ကို 100% အထိ မြင့်တက်သွားစေနိုင်ပါတယ်။
ဒီလိုဖြစ်ရတဲ့ အကြောင်းရင်းကတော့ Database Engine ဟာ Data တွေကို Disk ပေါ်မှာ Heap File အတွင်း 8KB Pages (Blocks) တွေအနေနဲ့ ရောက်တဲ့အစဉ်အတိုင်း သိမ်းဆည်းထားလို့ ဖြစ်ပါတယ်။ Index မရှိတဲ့အခါ Database ဟာ လိုချင်တဲ့ Row တစ်ခုတည်းကို ရှာဖို့ Disk ပေါ်က Pages အားလုံးကို အစကနေ အဆုံးအထိ တစ်ခုချင်းစီ ဆွဲတင်ဖတ်ရှုရတဲ့ Sequential Scan (O(N)) ကို လုပ်ဆောင်ရပါတယ်။ Database Index ဆိုတာကတော့ စာအုပ်တစ်အုပ်ရဲ့ နောက်ကျောက အညွှန်း (Index) လိုပဲ စာမျက်နှာအားလုံးကို လှန်ရှာစရာမလိုဘဲ Data ရှိတဲ့ Disk Page နဲ့ Offset (ctid) ကို တိုက်ရိုက် ညွှန်ပြပေးတဲ့ သီးခြား Data Structure တစ်ခု ဖြစ်ပါတယ်။
Inside the B+ Tree: How Databases Traverse Millions of Rows
Relational Databases (PostgreSQL, MySQL/InnoDB) အားလုံးနီးပါးရဲ့ Default Index အမျိုးအစားကတော့ B-Tree (အတိအကျအားဖြင့် B+ Tree) ဖြစ်ပါတယ်။ Binary Search Tree နဲ့ မတူဘဲ B+ Tree ရဲ့ Node တစ်ခုချင်းစီမှာ Keys အများအပြား (High Fan-out) သိမ်းဆည်းထားနိုင်တဲ့အတွက် Tree ရဲ့ အမြင့် (Height) ဟာ အလွန်နိမ့်ပါတယ်။
B+ Tree တစ်ခုမှာ အလွှာ ၃ ခု ပါဝင်ပါတယ်။
- Root Node: ရှာဖွေမှု စတင်ရာ အပေါ်ဆုံး Node ဖြစ်ပြီး RAM ထဲမှာ အမြဲတမ်း Cache လုပ်ထားလေ့ ရှိပါတယ်။
- Internal (Branch) Nodes: လမ်းကြောင်းညွှန်ပြပေးတဲ့ အလယ်အလွှာ Nodes တွေဖြစ်ပြီး Key Ranges နဲ့ အောက်အလွှာ Child Page Pointers တွေသာ ပါဝင်ပါတယ်။
- Leaf Nodes: အောက်ဆုံးအလွှာဖြစ်ပြီး စီစဉ်ထားတဲ့ Index Keys တွေနဲ့ အမှန်တကယ် Data ရှိရာ Heap Page Pointers တွေကို သိမ်းဆည်းထားပါတယ်။ Leaf Nodes အချင်းချင်းကို Doubly-Linked List နဲ့ ချိတ်ဆက်ထားတဲ့အတွက်
BETWEEN၊>၊<နဲ့ORDER BYလိုမျိုး Range Queries တွေကို အလွန်မြန်ဆန်စွာ ဆက်တိုက်ဖတ်ရှုနိုင်ပါတယ်။
Rows သန်း 100 ရှိတဲ့ Table တစ်ခုမှာတောင် B+ Tree ရဲ့ အမြင့်ဟာ 3 ကနေ 4 ဆင့်သာ ရှိတဲ့အတွက် မည်သည့် Row ကိုမဆို Disk Page 3–4 ကြိမ် ဖတ်ရုံနဲ့ (O(log N)) မီလီစက္ကန့်ပိုင်းအတွင်း ရှာဖွေပေးနိုင်ပါတယ်။
Composite Indexes and the Leftmost Prefix Rule
လက်တွေ့ Queries အများစုဟာ Column တစ်ခုတည်းကိုသာ စစ်ဆေးတာ မဟုတ်ဘဲ Columns အများအပြားကို တွဲဖက်စစ်ဆေးလေ့ ရှိပါတယ်။ ဒီလိုအခါမျိုးမှာ Column နှစ်ခု သို့မဟုတ် သုံးခုကို ပေါင်းစပ်တည်ဆောက်ထားတဲ့ Composite (Multi-Column) Index ကို သုံးရပါတယ်။
သတိထားရမယ့် အရေးကြီးဆုံး စည်းမျဉ်းကတော့ Leftmost Prefix Rule ဖြစ်ပါတယ်။ ဥပမာ (tenant_id, status, created_at) ဆိုတဲ့ အစဉ်အတိုင်း Index ဆောက်ထားမယ်ဆိုရင် Database က ပထမဆုံး tenant_id အလိုက် စီစဉ်ပြီး၊ တူညီတဲ့ tenant_id အတွင်းမှာမှ status အလိုက် ထပ်မံစီစဉ်ပေးပါတယ်။
WHERE tenant_id = 10သို့မဟုတ်WHERE tenant_id = 10 AND status = 'PAID'Query တွေမှာ ဒီ Index ကို အပြည့်အဝ သုံးနိုင်ပါတယ်။- ဒါပေမဲ့ အရှေ့ဆုံး Column ဖြစ်တဲ့
tenant_idမပါဘဲWHERE status = 'PAID'တစ်ခုတည်းနဲ့ ရှာမယ်ဆိုရင်တော့ Index ကို ထိရောက်စွာ မသုံးနိုင်တော့ဘဲ Full Table Scan သို့ ပြန်ရောက်သွားနိုင်ပါတယ်။
ဒါ့အပြင် Composite Index တည်ဆောက်တဲ့အခါ Column Ordering (Equality before Range) ကလည်း အလွန်အရေးကြီးပါတယ်။ အတိအကျ ညီမျှမှု စစ်ဆေးတဲ့ Columns (=, IN) တွေကို အရှေ့ဆုံးမှာ ထားရှိရမှာဖြစ်ပြီး၊ အပိုင်းအခြားနဲ့ ရှာဖွေတဲ့ Range Columns (>, <, BETWEEN) သို့မဟုတ် Sorting (ORDER BY created_at DESC) တွေကိုတော့ အနောက်မှာ ထားရှိရပါမယ်။ အကယ်၍ Range Column ကို အရှေ့မှာ ထားမိရင် သူ့နောက်က လိုက်လာတဲ့ Columns တွေအတွက် B+ Tree Ordering ပျက်ပြယ်သွားပြီး Index ရဲ့ ထက်ဝက်ခန့်သာ အလုပ်လုပ်နိုင်တော့မှာ ဖြစ်ပါတယ်။
Clustered vs Non-Clustered Index Architectures
Database Engine တစ်ခုနဲ့တစ်ခုကြားမှာ Primary Key နဲ့ Secondary Index တည်ဆောက်ပုံ အခြေခံကွာခြားချက်ကို နားလည်ထားဖို့ လိုအပ်ပါတယ်။
- MySQL (InnoDB) — Clustered Primary Index: InnoDB မှာ Table ရဲ့ Primary Key B+ Tree Leaf Nodes တွေဟာ Table Data Row အစစ်တွေ ကိုယ်တိုင်ပဲ ဖြစ်ပါတယ်။ ဒါကြောင့် Primary Key နဲ့ ရှာဖွေတဲ့အခါ Heap Lookup ထပ်လုပ်စရာမလိုဘဲ အလွန်မြန်ဆန်ပါတယ်။ ဒါပေမဲ့ Secondary Index တွေရဲ့ Leaf Nodes တွေမှာတော့ Physical Pointer အစား Primary Key တန်ဖိုးကို သိမ်းဆည်းထားတဲ့အတွက်၊ Secondary Index ကနေ ရှာရင် Primary Key B+ Tree ကို နောက်တစ်ကြိမ် ပြန်ဖြတ်သန်းရပါတယ်။ ဒါကြောင့် MySQL မှာ Primary Key ကို အရွယ်အစားကြီးမားပြီး အစီအစဉ်မကျတဲ့ Random UUIDv4 အစား Sequential UUIDv7 သို့မဟုတ်
BIGINTသုံးဖို့ အထူးအရေးကြီးပါတယ်။ - PostgreSQL — Heap-Organized Tables: PostgreSQL မှာတော့ Primary Key ရော Secondary Indexes အားလုံးပါ Non-Clustered B-Tree တွေ ဖြစ်ကြပြီး၊ အားလုံးက Heap File ထဲက Physical Tuple Identifier (
ctid) ကိုသာ တိုက်ရိုက် ညွှန်ပြပေးပါတယ်။
Covering Indexes and Partial Indexes in PostgreSQL
သာမန် Index Scan တစ်ခုမှာ Database ဟာ B+ Tree Leaf Node ဆီကနေ Pointer ရရှိပြီးတဲ့နောက် ကျန်တဲ့ Columns တွေကို ရယူဖို့ မူလ Table Heap ဆီသို့ နောက်တစ်ကြိမ် ပြန်သွားဖတ်ရပါတယ် (Heap Fetch သို့မဟုတ် MySQL မှာ Bookmark Lookup)။
အကယ်၍ Query က တောင်းဆိုတဲ့ Columns အားလုံးကို Index ထဲမှာတင် INCLUDE နဲ့ ထည့်သွင်းပေးထားမယ်ဆိုရင် Table Heap ဆီ လုံးဝသွားစရာ မလိုတော့ဘဲ Index တစ်ခုတည်းကနေ အဖြေတိုက်ရိုက်ပေးနိုင်တဲ့ Index-Only Scan (Covering Index) ကို ရရှိနိုင်ပါတယ်။ အလားတူပဲ Table တစ်ခုလုံးကို Index မဆောက်ဘဲ မကြာခဏ ရှာဖွေလေ့ရှိတဲ့ အစိတ်အပိုင်းလေးကိုသာ WHERE Condition နဲ့ ရွေးချယ်တည်ဆောက်တဲ့ Partial Index ဟာလည်း Disk Space နဲ့ RAM ကို အဆပေါင်းများစွာ သက်သာစေပါတယ်။
-- 1. Covering Index: Enables Index-Only Scan without touching the table heap
CREATE INDEX idx_orders_user_covering
ON orders (user_id, created_at DESC)
INCLUDE (status, total_amount);
-- 2. Partial Index: Indexes only active pending orders (tiny index footprint)
CREATE INDEX idx_orders_pending_only
ON orders (created_at)
WHERE status = 'PENDING' AND deleted_at IS NULL;
Choosing the Right Index Type
B-Tree အပြင် Data အမျိုးအစားနဲ့ Query ပုံစံအလိုက် ရွေးချယ်သင့်တဲ့ Specialized Index အမျိုးအစားတွေကို အောက်ပါဇယားမှာ နှိုင်းယှဉ်ကြည့်နိုင်ပါတယ်။
| Index Type | Internal Structure | Supported Operators | Storage Footprint | Best Engineering Fit |
|---|---|---|---|---|
| B-Tree | Balanced B+ Tree | =, <, >, BETWEEN, LIKE 'abc%' | Moderate | Default for IDs, timestamps, strings, sorting |
| Hash | Hash buckets | = (Exact equality only) | Small to Moderate | Long URL strings or UUID exact lookups |
| GIN | Generalized Inverted Index | @>, ?, Full-text search | Large (slower writes) | JSONB documents, arrays, full-text search |
| GiST | Balanced search tree | Geometric overlap, nearest-neighbor | Moderate | PostGIS geospatial coordinates, range types |
| BRIN | Block Range Index (min/max) | <, >, = on ordered data | Extremely Tiny (<1% of B-Tree) | Append-only time-series logs and audit tables |
The Hidden Cost: Write Amplification and Index Bloat
Index တွေက Read Queries တွေကို မြန်ဆန်စေပေမဲ့ Column တိုင်းကို မျက်စိမှိတ် Index လိုက်ဆောက်ခြင်း (Over-Indexing) ဟာ Write Performance ကို ပြင်းထန်စွာ ထိခိုက်စေပါတယ်။
- Write Amplification: Table ထဲကို
INSERT၊UPDATEသို့မဟုတ်DELETEတစ်ကြိမ် လုပ်တိုင်း Table Heap ကိုသာမက အဲဒီ Table ပေါ်မှာ ဆောက်ထားတဲ့ Index အားလုံးရဲ့ B+ Tree Nodes တွေကိုပါ လိုက်လံပြင်ဆင်ရပါတယ်။ Index10ခု ရှိရင် Write တစ်ကြိမ်ဟာ Disk Operations အဆပေါင်းများစွာ ပိုလုပ်ရသလို Page Splits တွေကြောင့် Latency တက်လာပါတယ်။ - Buffer Pool Pollution: Database ဟာ မကြာခဏ သုံးတဲ့ Index Pages တွေကို RAM (
shared_buffers) ထဲမှာ သိမ်းဆည်းထားရပါတယ်။ မသုံးတဲ့ Unused Indexes တွေ များနေရင် တန်ဖိုးရှိတဲ့ RAM ပမာဏကို အလဟဿ နေရာယူထားမိတတ်ပါတယ်။ - Why Indexes Get Ignored: Column တစ်ခုပေါ်မှာ Function အုပ်ပြီး ရှာဖွေမိရင် (ဥပမာ
WHERE LOWER(email) = 'a@b.com') ရိုးရိုး Index ကို သုံးလို့ မရတော့ပါဘူး။ ဒီလိုအခြေအနေအတွက် Expression Index (CREATE INDEX ON users (LOWER(email))) ကို သီးခြားတည်ဆောက်ပေးရပါမယ်။ - Zero-Downtime Index Creation: Production ပေါ်က Rows သန်းချီရှိတဲ့ Table တစ်ခုမှာ ရိုးရိုး
CREATE INDEXRun လိုက်ရင် Table တစ်ခုလုံးကို Write Lock ချထားတဲ့အတွက် API Requests တွေ ရပ်တန့်သွားနိုင်ပါတယ်။ ဒါကြောင့် Production မှာ အမြဲတမ်းCREATE INDEX CONCURRENTLYကိုသာ အသုံးပြုပြီး Table Lock မချဘဲ နောက်ကွယ်ကနေ တည်ဆောက်ရသလို၊pg_stat_user_indexesView ကနေတစ်ဆင့် လုံးဝ အသုံးမပြုတော့တဲ့ Unused Indexes (idx_scan = 0) တွေကိုလည်း ပုံမှန် စစ်ဆေးဖယ်ရှားပေးရပါမယ်။
Final Thoughts
Database Indexing ဆိုတာ Read Latency နဲ့ Write Overhead ကြားမှာ အကောင်းဆုံး မျှခြေကို ရှာဖွေရတဲ့ အင်ဂျင်နီယာ ပညာရပ်ဖြစ်ပါတယ်။ B+ Tree ရဲ့ အလုပ်လုပ်ပုံနဲ့ Leftmost Prefix Rule ကို နားလည်ထားပြီး၊ လက်တွေ့ Query Patterns တွေအပေါ် မူတည်ကာ Composite၊ Covering နဲ့ Partial Indexes တို့ကို စနစ်တကျ ရွေးချယ်တည်ဆောက်ခြင်းအားဖြင့် Rows သန်းပေါင်းများစွာရှိတဲ့ Production Database ကို မီလီစက္ကန့်ပိုင်းအတွင်း သွက်လက်စွာ မောင်းနှင်နိုင်မှာ ဖြစ်ပါတယ်။