ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
စာအုပ်ကြီးတစ်အုပ်ထဲမှာ "foreign key" ဆိုတဲ့ စကားလုံး ပါတဲ့ စာမျက်နှာတွေကို ရှာရမယ်ဆိုပါစို့။ စာမျက်နှာ ၁ ကနေ အဆုံးထိ တစ်ရွက်ချင်း လှန်ဖတ်လို့ ရပေမယ့် အချိန်ကြာပါတယ်။ စာအုပ်နောက်ဆုံးက index စာမျက်နှာမှာ စကားလုံးကို အက္ခရာစဉ်အတိုင်း စီထားပြီး စာမျက်နှာနံပါတ်တွေ ရေးထားလို့ စက္ကန့်ပိုင်းအတွင်း တန်းရောက်ပါတယ်။
Database index လည်း အတူတူပါပဲ။ Column value တွေကို စီထားတဲ့ သီးခြား structure ဖြစ်ပြီး value တစ်ခုစီက table ထဲက row ဘယ်နေရာမှာ ရှိလဲ ညွှန်ပြပါတယ်။ အပေးအယူကတော့ စာအုပ်ထဲ စာမျက်နှာအသစ်ထည့်တိုင်း index စာမျက်နှာကိုပါ ပြင်ရသလို row အသစ်ထည့်တိုင်း index ကိုပါ update လုပ်ရတာပါ။
Problem → Why → Solution
ပြဿနာ: data များလာမှ နှေးလာတဲ့ page
Online shop တစ်ခုရဲ့ "ကျွန်ုပ်ရဲ့ order များ" page က customer တစ်ယောက်ရဲ့ နောက်ဆုံး order ၁၀ ခုကို ပြပါတယ်။ Launch လုပ်ခါစမှာ order ရာဂဏန်းပဲ ရှိလို့ မြန်ပါတယ်။ တစ်နှစ်ကြာလို့ order သန်းချီ ဖြစ်လာတဲ့အခါ page တစ်ခါဖွင့်တိုင်း စက္ကန့်အတော်ကြာလာပြီး database CPU တက်နေပါတယ်။ Query ကို တစ်လုံးမှ မပြင်ရသေးဘဲ ဒီလို ဖြစ်လာတာပါ။
EXPLAIN နဲ့ ကြည့်လိုက်ရင် အကြောင်းရင်း ပေါ်လာပါတယ်။ PostgreSQL က order အားလုံးကို အစကနေ အဆုံးထိ ဖတ်ပြီး (Seq Scan) customer 42 မဟုတ်တဲ့ row တွေကို လွှင့်ပစ်၊ ကျန်တာကို sort လုပ်ပြီးမှ ၁၀ ခု ယူနေတာပါ။
-- Runs on every visit to "My orders"
SELECT id, total, created_at
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 10;
-- Plan: Limit -> Sort -> Seq Scan on orders (reads the whole table)ဘာကြောင့် data နဲ့အတူ နှေးလာတာလဲ
Index မရှိရင် PostgreSQL မှာ ရွေးစရာ တစ်ခုပဲ ရှိပါတယ်: row တိုင်းကို ဖတ်ပြီး condition ကို စစ်တာပါ။ အလုပ်ပမာဏက ကိုယ်လိုချင်တဲ့ row ၁၀ ခုနဲ့ မဆိုင်ဘဲ table အရွယ်အစားနဲ့ အချိုးကျ တက်လာပါတယ်။ Index က row ၁၀ ခုဆီ တန်းသွားခွင့် ပေးလို့ table ကြီးလာရင်တောင် အလုပ်ပမာဏ သိပ်မပြောင်းပါဘူး။
Core concept: index ကို query ပုံစံအတိုင်း ဆောက်ပါ
PostgreSQL ရဲ့ default index type က B-tree ပါ။ Value တွေကို sort လုပ်ထားတဲ့ tree ဖြစ်လို့ =၊ <၊ >၊ BETWEEN၊ ORDER BY နဲ့ 'abc%' လို prefix ရှာတာ (C collation ဒါမှမဟုတ် text_pattern_ops လိုတတ်ပါတယ်) တွေကို ကူညီနိုင်ပါတယ်။ Query တစ်ခု run တိုင်း planner က row ဘယ်နှစ်ကြောင်း ပြန်ရမလဲ ခန့်မှန်းပြီး Seq Scan၊ Index Scan၊ Bitmap Scan ထဲက အသက်သာဆုံး plan ကို ရွေးပါတယ်။ Index ရှိနေတိုင်း သုံးမယ်လို့ အာမမခံပါဘူး။
၁။ ရွေးချယ်မှု (selectivity) က အရေးကြီးတယ်
Row ၂၀၀၀၀ ထဲက ၄၀ ကြောင်းကိုပဲ ရွေးတဲ့ customer_id = 42 အတွက် index က အများကြီး ကူညီပါတယ်။ Row အားလုံးနီးပါး ပြန်ရမယ့် total > 5 အတွက်တော့ index ကနေ တစ်ကြောင်းချင်း ခုန်ဖတ်တာထက် table ကို တစ်ခါတည်း ဆက်တိုက်ဖတ်တာ ပိုသက်သာလို့ planner က Seq Scan ကို ရွေးပါတယ်။
၂။ Composite index မှာ column အစီအစဉ် အရေးကြီးတယ်
(customer_id, created_at DESC) index က customer_id အလိုက် အရင်စီပြီး customer တစ်ယောက်ထဲမှာ created_at အသစ်ဆုံးကနေ စီထားပါတယ်။ ဒါကြောင့် "customer 42 ရဲ့ နောက်ဆုံး ၁၀ ခု" က index ထဲမှာ ကပ်လျက် ရှိနေပြီး Sort step မလိုတော့ပါဘူး။ ပုံမှန်စည်းမျဉ်းကတော့ = နဲ့ စစ်တဲ့ column ကို ရှေ့မှာ၊ range ဒါမှမဟုတ် sort column ကို နောက်မှာ ထားတာပါ။
၃။ Index က column value ကိုပဲ သိတယ်
created_at ပေါ်မှာ index ရှိပေမယ့် WHERE created_at::date = '2026-01-05' ဆိုရင် index ထဲမှာ cast ပြီးသား date value မရှိလို့ မသုံးနိုင်ပါဘူး။ Column ကို မပတ်ဘဲ range condition (>= နဲ့ <) နဲ့ ရေးပါ။ ဒါမှမဟုတ် lower(email) လို expression ပေါ်မှာ expression index ဆောက်ပါ။
၄။ Index တိုင်းမှာ ကုန်ကျစရိတ် ရှိတယ်
INSERT၊ UPDATE၊ DELETE တစ်ခါလုပ်တိုင်း သက်ဆိုင်ရာ index အားလုံးကို update လုပ်ရပြီး disk နေရာလည်း ယူပါတယ်။ Read များတဲ့ query တွေအတွက် index ထည့်ပြီး ဘယ်သူမှ မသုံးတဲ့ index တွေကို ဖယ်ရှားပါ။
| Plan node | အဓိပ္ပာယ် |
|---|---|
| Seq Scan | Table ကို အစကနေ အဆုံးထိ ဖတ်ပြီး Filter နဲ့ စစ်တယ် |
| Index Scan | Index ထဲမှာ ရှာပြီး ကိုက်တဲ့ row တွေကို table ကနေ ယူတယ် |
| Bitmap Heap Scan | Index ကနေ row တည်နေရာတွေ စုပြီး table page တွေကို အစဉ်လိုက် ဖတ်တယ် |
| Index Only Scan | လိုတဲ့ column အားလုံး index ထဲမှာ ရှိလို့ table ကို သိပ်မထိရဘူး |
- နှေးတဲ့ query ကို EXPLAIN နဲ့ ကြည့်
- = column တွေကို index ရှေ့မှာ ထား
- Range/sort column ကို နောက်မှာ ထား
- EXPLAIN ကို ပြန် run ပြီး စစ်
Index ကို column တစ်ခုချင်းစီအတွက် မဟုတ်ဘဲ မကြာခဏ run တဲ့ query တစ်ခုအတွက် design လုပ်ပါ။
အခြေခံ ပုံစံ
CREATE [UNIQUE] INDEX index_name
ON table_name (equality_col, range_or_sort_col [DESC]);
CREATE INDEX index_name ON table_name (lower(text_col)); -- expression
CREATE INDEX index_name ON table_name (col) WHERE flag; -- partial
EXPLAIN [ANALYZE] SELECT ... ; -- show the chosen plan
DROP INDEX index_name;Equality column ကို အရင်၊ range ဒါမှမဟုတ် sort column ကို နောက်မှ ထားပါ။ Expression index က function ပတ်ထားတဲ့ condition အတွက်၊ partial index က row အစိတ်အပိုင်းတစ်ခုကိုပဲ မကြာခဏ ရှာတဲ့အခါအတွက် ဖြစ်ပါတယ်။ EXPLAIN က ခန့်မှန်း plan ကို ပြပြီး EXPLAIN ANALYZE က query ကို တကယ် run ပြီး အချိန်အမှန်ကို ပြပါတယ်။
"ကျွန်ုပ်ရဲ့ order များ" query ကို မြန်အောင်လုပ်ခြင်း
ဒီ example မှာ customer ၅၀၀ အတွက် order ၂၀၀၀၀ ရှိတဲ့ table ကို သုံးပြီး ANALYZE နဲ့ statistics ပြင်ဆင်ထားပါတယ်။ Index မထည့်ခင်နဲ့ ထည့်ပြီးနောက် plan နှစ်ခုကို နှိုင်းယှဉ်ပါမယ်။ Cost ဂဏန်းတွေက planner ရဲ့ ခန့်မှန်းချက်ဖြစ်ပြီး data၊ statistics၊ PostgreSQL version အလိုက် ပြောင်းလို့ output ကို နမူနာအနေနဲ့သာ ကြည့်ပါ။ အရေးကြီးတာက plan ရဲ့ ပုံသဏ္ဌာန်ပါ: Sort + Seq Scan ကနေ Index Scan တစ်ခုတည်း ဖြစ်သွားတာပါ။
-- 1. How does PostgreSQL run the query today?
EXPLAIN
SELECT id, total, created_at
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 10;
-- 2. Add an index shaped like the query
CREATE INDEX idx_orders_customer_created
ON orders (customer_id, created_at DESC);
-- 3. Ask again
EXPLAIN
SELECT id, total, created_at
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 10;Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Ask for the plan
EXPLAINEXPLAIN က query ကို မ run ဘဲ planner ရွေးမယ့် plan ကိုပဲ ပြပါတယ်။ ပထမ plan မှာ Seq Scan on orders နဲ့ Filter: (customer_id = 42) ကို တွေ့ရပါတယ်။
- The query being tuned
WHERE customer_id = 42Equality condition ဖြစ်လို့ index ရဲ့ ပထမ column ဖြစ်သင့်ပါတယ်။ Index ပြီးနောက် plan ထဲမှာ Filter အစား Index Cond အဖြစ် ပေါ်လာပါတယ်။
- Index shaped like the query
ON orders (customer_id, created_at DESC);Customer တစ်ယောက်ချင်းစီရဲ့ order တွေက index ထဲမှာ အသစ်ဆုံးကနေ စီပြီးသားဖြစ်လို့ PostgreSQL က ပထမ ၁၀ ခုကို ဖတ်ပြီး ရပ်လိုက်ရုံပါပဲ။ ဒုတိယ plan မှာ Sort node ပျောက်သွားတာ ဒီအကြောင်းကြောင့်ပါ။
- LIMIT stops early
LIMIT 10;Index ကနေ အစဉ်လိုက် ဖတ်နိုင်တဲ့အခါ LIMIT က ၁၀ ကြောင်းရတာနဲ့ ရပ်ပါတယ်။ Index မရှိရင်တော့ ကိုက်တဲ့ row အားလုံးကို sort ပြီးမှ ၁၀ ခု ဖြတ်ယူရပါတယ်။
-- plan before the index (cost numbers are estimates and will differ)
Limit (cost=398.86..398.89 rows=10 width=21)
-> Sort (cost=398.86..398.96 rows=40 width=21)
Sort Key: created_at DESC
-> Seq Scan on orders (cost=0.00..398.00 rows=40 width=21)
Filter: (customer_id = 42)
-- plan after the index
Limit (cost=0.29..37.45 rows=10 width=21)
-> Index Scan using idx_orders_customer_created on orders
(cost=0.29..148.93 rows=40 width=21)
Index Cond: (customer_id = 42)Index ရှိလျက်နဲ့ မသုံးတဲ့ အခြေအနေ
အောက်က snippet မှာ created_at ပေါ် index တစ်ခု ဆောက်ပြီး query နှစ်ခုကို နှိုင်းယှဉ်ပါတယ်။ ပထမ query က column ကို ::date နဲ့ cast လုပ်လို့ Seq Scan ဖြစ်ပြီး ဒုတိယ query က အဓိပ္ပာယ်တူ range ကို bare column ပေါ်မှာ ရေးလို့ Index Scan ရပါတယ်။ Comment ထဲက plan တွေက PGlite (PostgreSQL 18) မှာ run ကြည့်ထားတဲ့ ရလဒ်ကို အတိုချုံ့ထားတာပါ။
CREATE INDEX idx_orders_created ON orders (created_at);
-- Function on the column: the index cannot help
EXPLAIN SELECT id FROM orders WHERE created_at::date = '2026-01-05';
-- Seq Scan on orders Filter: ((created_at)::date = '2026-01-05'::date)
-- Same day as a range on the bare column
EXPLAIN SELECT id FROM orders
WHERE created_at >= '2026-01-05' AND created_at < '2026-01-06';
-- Index Scan using idx_orders_created on ordersLeftmost column စည်းမျဉ်းနဲ့ PostgreSQL 18
(customer_id, created_at) index က customer_id ပါတဲ့ query တွေအတွက် အကောင်းဆုံး အလုပ်လုပ်ပါတယ်။ PostgreSQL 18 မှာ B-tree skip scan ပါလာလို့ ပထမ column မပါတဲ့ query အတွက်လည်း တခါတလေ သုံးနိုင်ပါတယ်။ ဒါပေမယ့် ပထမ column မှာ မတူတဲ့ value နည်းတဲ့အခါမှ ထိရောက်တာမို့ အဲဒါကို အားမကိုးဘဲ EXPLAIN နဲ့ စစ်ပါ။ MySQL နဲ့ SQL Server မှာလည်း composite index column အစီအစဉ် စည်းမျဉ်း အတူတူနီးပါး ရှိပါတယ်။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
Production မှာ index ကို ခန့်မှန်းပြီး မထည့်ပါဘူး။ pg_stat_statements extension ဒါမှမဟုတ် slow query log ကနေ အချိန်အများဆုံးယူတဲ့ query တွေကို ရှာ၊ EXPLAIN (ANALYZE, BUFFERS) နဲ့ plan ကို ကြည့်ပြီးမှ index ဆုံးဖြတ်ပါတယ်။ ဒီ lesson မှာ မြင်ခဲ့သလို PostgreSQL က foreign key column တွေကို index အလိုလို မဆောက်ပေးပါဘူး (primary key နဲ့ UNIQUE ကိုတော့ ဆောက်ပေးပါတယ်)။ ဒါကြောင့် orders.customer_id လို join column တွေမှာ index ထည့်ရတာ အရမ်းအဖြစ်များပါတယ်။
အလုပ်လုပ်နေတဲ့ ကြီးမားတဲ့ table ပေါ်မှာ ရိုးရိုး CREATE INDEX က build မပြီးမချင်း write တွေကို တားထားပါတယ်။ ဒါကြောင့် team တွေက CREATE INDEX CONCURRENTLY ကို သုံးလေ့ရှိပါတယ်။ ပိုကြာပေမယ့် write တွေကို မပိတ်ပါဘူး၊ transaction block ထဲမှာ run လို့ မရပါဘူး။ pg_stat_user_indexes ထဲမှာ idx_scan = 0 ဖြစ်နေတဲ့ index တွေကို ပြန်စစ်ပြီး မလိုတော့တာကို ဖယ်ရှားကြပါတယ်။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
Composite index column အစီအစဉ်ကို query မကြည့်ဘဲ ရွေးတာ
(created_at, customer_id) လို့ ပြောင်းပြန် ဆောက်လိုက်ရင် customer 42 ရဲ့ row တွေက index တစ်ခုလုံးမှာ ပြန့်ကျဲနေလို့ "customer တစ်ယောက်ရဲ့ နောက်ဆုံး ၁၀ ခု" ကို ထိထိရောက်ရောက် မရှာနိုင်ပါဘူး။ = condition column ကို ရှေ့မှာ ထားပါ။
Column တိုင်းမှာ index ထည့်တာ
Index ငါးခုရှိတဲ့ table မှာ INSERT တစ်ခုက table နဲ့ index ငါးခု၊ စုစုပေါင်း structure ခြောက်ခုကို ရေးရပါတယ်။ Read မမြန်လာဘဲ write နှေးလာပြီး disk နေရာ ကုန်ပါတယ်။ တကယ် run တဲ့ query ရှိမှ index ထည့်ပါ။
Index column ကို function နဲ့ ပတ်တာ
WHERE lower(email) = ... ဒါမှမဟုတ် WHERE created_at::date = ... လို ရေးရင် column ပေါ်က ရိုးရိုး index ကို မသုံးနိုင်ပါဘူး။ Range condition နဲ့ ပြန်ရေးပါ၊ ဒါမှမဟုတ် အဲဒီ expression အတိအကျ ပေါ်မှာ index ဆောက်ပါ။
Table သေးသေးမှာ Seq Scan မြင်ပြီး index ပျက်နေတယ် ထင်တာ
Row အနည်းငယ်ပဲ ရှိတဲ့ table မှာ page တစ်ခုနှစ်ခုကို တစ်ခါတည်း ဖတ်တာက index ကို ဖြတ်သွားတာထက် သက်သာလို့ planner က Seq Scan ကို ရွေးတာ မှန်ပါတယ်။ Production အရွယ်အစားနီးပါး data နဲ့ ANALYZE လုပ်ပြီးမှ plan ကို ဆုံးဖြတ်ပါ။
Mini challenge
Email ကို စာလုံးအကြီးအသေး မခွဲဘဲ unique ဖြစ်အောင်
users table မှာ 'susu@mail.com' ရှိပြီးသားပါ။ 'SuSu@Mail.com' ဆိုတဲ့ email နဲ့ နောက်ထပ် account ဖွင့်လို့ မရအောင် index တစ်ခု ဆောက်ပြီး login lookup ကလည်း အဲဒီ index ကို သုံးနိုင်အောင် ရေးပါ။
- lower(email) ပေါ်မှာ UNIQUE expression index တစ်ခု ဆောက်ပါ
- DO block ထဲမှာ duplicate insert ကို စမ်းပြီး unique_violation ကို ဖမ်းပါ
- WHERE lower(email) = lower('...') နဲ့ user ကို ရှာပါ
- pg_indexes ကနေ users table ရဲ့ index တွေကို စာရင်းထုတ်ပါ
-- users(id, email, full_name) already has two rows.
-- TODO 1: one account per email, ignoring letter case
-- TODO 2: prove a case-only duplicate is rejected without stopping the script
DO $$
BEGIN
-- TODO: insert 'SuSu@Mail.com'
EXCEPTION WHEN unique_violation THEN
RAISE NOTICE 'duplicate rejected';
END $$;
-- TODO 3: find the user for 'KOKO@mail.com' in a way the index can serve
-- TODO 4: list index names and definitions for the users tableရလဒ်: NOTICE: duplicate rejected ပေါ်ပြီး lookup က id 2၊ Ko Ko ကို ပြန်ပေးပါတယ်။ နောက်ဆုံး query က users_email_lower_key နဲ့ users_pkey ဆိုတဲ့ index နှစ်ခုကို ပြပါတယ်။
အရိပ်အမြွက် ကြည့်ရန်
Index ထဲက expression နဲ့ WHERE ထဲက expression က အတိအကျ တူမှ index ကို သုံးနိုင်ပါတယ်။ ရိုးရိုး UNIQUE (email) ဆိုရင် 'SuSu@Mail.com' နဲ့ 'susu@mail.com' ကို မတူဘူးလို့ ယူဆလို့ duplicate ကို တားမပေးပါဘူး။
အဖြေ ကြည့်ရန်
-- 1. One account per email, ignoring letter case
CREATE UNIQUE INDEX users_email_lower_key ON users (lower(email));
-- 2. A duplicate that differs only in case must be rejected
DO $$
BEGIN
INSERT INTO users (email, full_name) VALUES ('SuSu@Mail.com', 'Su Su 2');
RAISE NOTICE 'duplicate was accepted';
EXCEPTION WHEN unique_violation THEN
RAISE NOTICE 'duplicate rejected';
END $$;
-- 3. Login lookup written so the expression index can serve it
SELECT id, full_name FROM users WHERE lower(email) = lower('KOKO@mail.com');
-- 4. Confirm what exists
SELECT indexname, indexdef
FROM pg_indexes
WHERE tablename = 'users'
ORDER BY indexname;Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Index က row တွေဆီ တန်းရောက်စေပေမယ့် write တိုင်းကို ပိုနှေးစေပြီး disk နေရာ ယူပါတယ်
- Composite index မှာ = column တွေကို ရှေ့၊ range/sort column ကို နောက်မှာ ထားပါ
- Column ကို function နဲ့ ပတ်ရင် ရိုးရိုး index မသုံးနိုင်လို့ range condition ဒါမှမဟုတ် expression index သုံးပါ
- EXPLAIN ရဲ့ cost ဂဏန်းတွေက ခန့်မှန်းချက်ပါ၊ plan ပုံသဏ္ဌာန်ကို ကြည့်ပြီး EXPLAIN ANALYZE နဲ့ အတည်ပြုပါ
- PostgreSQL က foreign key column တွေကို index အလိုလို မဆောက်ပေးပါ
PostgreSQL Indexes — PostgreSQL
PostgreSQL Indexes — PostgreSQL