
Database Tuning and Query Optimization
A deep dive into production database performance tuning. Master EXPLAIN ANALYZE query plans, keyset cursor pagination, PgBouncer connection pooling, and PostgreSQL MVCC autovacuum maintenance.
Beyond Basic Indexing: Holistic Database Tuning
Database နှေးကွေးတိုင်း Index တစ်ခု ထပ်ဆောက်လိုက်ရုံနဲ့ ပြဿနာအားလုံး မပြေလည်နိုင်ပါဘူး။ Production အဆင့်မှာ Index တွေ အပြည့်အစုံ ရှိနေပါလျက်နဲ့ Query Planner က Index မသုံးဘဲ Table Scan ရွေးချယ်သွားတာ၊ OFFSET 50000 လို Pagination Query တွေကြောင့် CPU မြင့်တက်သွားတာ၊ ဒါမှမဟုတ် Serverless Functions ရာပေါင်းများစွာက Database Connections တွေ တစ်ပြိုင်နက် ဆွဲယူလိုက်လို့ Database ပြိုလဲသွားတာမျိုးတွေ မကြာခဏ ကြုံတွေ့ရလေ့ ရှိပါတယ်။
တကယ့် Production Database Performance Tuning မှာ အဓိက ထောင့်တိုင် ၃ ခု ပါဝင်ပါတယ်။
- Query Execution Optimization: Query Planner ရဲ့ အလုပ်လုပ်ပုံကို နားလည်ပြီး Disk I/O အနည်းဆုံးဖြစ်အောင် SQL ရေးသားခြင်း။
- Connection Lifecycle Management: Connection Pooling နဲ့ Concurrency Overhead ကို ထိန်းချုပ်ခြင်း။
- Storage & Memory Hygiene: Buffer Cache Hit Ratio နဲ့ MVCC Dead Tuples (Autovacuum) တို့ကို စနစ်တကျ ထိန်းသိမ်းခြင်းတို့ ဖြစ်ပါတယ်။
Reading Query Execution Plans with EXPLAIN (ANALYZE, BUFFERS)
နှေးကွေးနေတဲ့ SQL Query တစ်ခုကို ပြင်ဆင်ဖို့အတွက် မှန်းဆမနေဘဲ Database Engine က အဲဒီ Query ကို အမှန်တကယ် ဘယ်လို တွက်ချက်သွားလဲဆိုတာ EXPLAIN (ANALYZE, BUFFERS) နဲ့ စစ်ဆေးကြည့်ရပါမယ်။
- Estimated Rows vs Actual Rows: Planner က ခန့်မှန်းထားတဲ့ Row အရေအတွက်နဲ့ တကယ်ထွက်လာတဲ့ Row အရေအတွက် အဆမတန် ကွာဟနေရင် Table Statistics အဟောင်းဖြစ်နေတာကြောင့် မှားယွင်းတဲ့ Join Algorithm (ဥပမာ
Hash JoinအစားNested Loop) ကို ရွေးချယ်သွားတတ်ပါတယ်။ shared hitvsread:BUFFERSOutput မှာshared hitဆိုတာ RAM (shared_buffers) ပေါ်ကနေ တိုက်ရိုက်ရယူနိုင်ခဲ့တဲ့8KBPages အရေအတွက်ဖြစ်ပြီး၊readကတော့ Disk ပေါ်ကနေ အနှေးဆွဲတင်ခဲ့ရတဲ့ Pages အရေအတွက် ဖြစ်ပါတယ်။Rows Removed by Filter: Index ကနေ Rows သိန်းချီ ဆွဲထုတ်ပြီးမှ Memory ပေါ်ရောက်မှ ပြန်စစ်ထုတ်ပစ်နေရတယ်ဆိုရင် Composite သို့မဟုတ် Covering Index လိုအပ်နေပြီဆိုတဲ့ လက္ခဏာ ဖြစ်ပါတယ်။
ဒါ့အပြင် Database က Tables နှစ်ခုကို ချိတ်ဆက်တဲ့အခါ ရွေးချယ်လေ့ရှိတဲ့ Join Algorithms ၃ မျိုး ရဲ့ သဘာဝကိုလည်း နားလည်ထားရပါမယ်။
- Nested Loop Join: Table အသေးတစ်ခုရဲ့ Row တစ်ခုချင်းစီအတွက် ဒုတိယ Table ရဲ့ Index ကို သွားရှာတဲ့ ပုံစံဖြစ်ပြီး၊ Row အရေအတွက် နည်းပါးတဲ့ OLTP Queries တွေအတွက် အမြန်ဆုံး ဖြစ်ပါတယ်။
- Hash Join: Table တစ်ခုကို Memory (
work_mem) ထဲမှာ Hash Table အဖြစ် တည်ဆောက်ပြီး ဒုတိယ Table နဲ့ တိုက်ဆိုင်စစ်ဆေးတဲ့ ပုံစံဖြစ်ကာ အရွယ်အစားကြီးမားပြီး မစီစဉ်ထားတဲ့ Data Sets တွေအတွက် အသင့်တော်ဆုံး ဖြစ်ပါတယ်။ အကယ်၍work_memမလောက်ငရင် Disk ပေါ် ယာယီဖိုင် (Temp Files) ထုတ်ရေးရတဲ့အတွက် ရုတ်တရက် နှေးကွေးသွားတတ်ပါတယ်။ - Merge Join: Join Key အလိုက် အစီအစဉ်တကျ စီပြီးသား (
Sorted) Tables နှစ်ခုကို တစ်ပြိုင်နက်တည်း ပေါင်းစပ်ပေးတဲ့ နည်းလမ်း ဖြစ်ပါတယ်။
Replacing Offset Pagination with Keyset (Cursor) Pagination
API တွေမှာ အတွေ့ရအများဆုံး Query Anti-Pattern တစ်ခုကတော့ LIMIT 20 OFFSET 100000 သုံးပြီး စာမျက်နှာ ခွဲခြင်း (Offset Pagination) ဖြစ်ပါတယ်။ OFFSET 100000 ပေးလိုက်တဲ့အခါ Database ဟာ အရှေ့က Rows 100,000 လုံးကို Disk/Index ပေါ်ကနေ တစ်ခုချင်းစီ ဆွဲထုတ်ရေတွက်ပြီး အမှိုက်ပုံးထဲ ပစ်ထည့်ကာ၊ 100,001 ကနေ 100,020 အထိ 20 ခုကိုသာ ပြန်ပေးပါတယ်။ စာမျက်နှာ နက်ရှိုင်းလာလေလေ Query က ပိုနှေးလာလေလေ ဖြစ်ပါတယ်။
ဒါကို ဖြေရှင်းဖို့ နောက်ဆုံးမြင်ခဲ့ရတဲ့ Row ရဲ့ (created_at, id) ကို အမှတ်အသား (Cursor) အဖြစ် သုံးပြီး B+ Tree Index ပေါ်မှာ တိုက်ရိုက် ခုန်ကျော်ရှာဖွေတဲ့ Keyset (Seek/Cursor) Pagination ကို သုံးရပါမယ်။
-- Anti-Pattern: Scans and discards 100,000 rows (O(N) degradation)
SELECT id, title, created_at FROM articles
ORDER BY created_at DESC, id DESC
LIMIT 20 OFFSET 100000;
-- Optimized Keyset Pagination: Direct B+ Tree seek in < 1ms (O(log N))
SELECT id, title, created_at FROM articles
WHERE (created_at, id) < ('2026-10-15T10:00:00Z', 'a9f3-8812')
ORDER BY created_at DESC, id DESC
LIMIT 20;
Connection Management and Pooling (PgBouncer)
PostgreSQL မှာ Client Connection အသစ်တစ်ခု ချိတ်ဆက်တိုင်း OS အဆင့်မှာ သီးခြား Process တစ်ခု (fork()) ဖန်တီးပေးရပြီး Connection တစ်ခုချင်းစီဟာ RAM 5MB ကနေ 10MB အထိ သုံးစွဲပါတယ်။ အကယ်၍ Auto-scaling Backend Instances သို့မဟုတ် Serverless Functions တွေက Connections 500 တစ်ပြိုင်နက် ဖွင့်လိုက်မယ်ဆိုရင် Database CPU ဟာ Query မတွက်ချက်နိုင်တော့ဘဲ OS Process Context Switching နဲ့ Lock Contention တို့ကြောင့် စွမ်းဆောင်ရည် ထိုးကျသွားပါတယ်။
ဒါကြောင့် Application နဲ့ Database ကြားမှာ PgBouncer လိုမျိုး Lightweight Connection Pooler ကို Transaction Pooling Mode နဲ့ ခံထားရပါမယ်။
Transaction Pooling Mode မှာ Application Connections ထောင်ပေါင်းများစွာကို လက်ခံထားနိုင်ပေမဲ့၊ အမှန်တကယ် SQL Transaction Run နေတဲ့ မီလီစက္ကန့်ပိုင်းအတွင်းမှာသာ Database ရဲ့ Physical Connection (30–50 ခန့်) ကို ခေတ္တငှားရမ်းပေးပြီး Transaction ပြီးတာနဲ့ အခြား Request အတွက် ချက်ချင်း ပြန်လွှဲပေးပါတယ်။
PostgreSQL Server တစ်လုံးအတွက် အကောင်းဆုံး Active Physical Connections အရေအတွက်ကို တွက်ချက်တဲ့ စံနှုန်းကတော့ Connections = (CPU Cores * 2) + Effective Spindle Count ဖြစ်ပါတယ်။ ဥပမာ 8-Core CPU ရှိတဲ့ Database Server တစ်လုံးမှာ တစ်ပြိုင်နက်တည်း အမြန်ဆုံး အလုပ်လုပ်နိုင်တဲ့ Connection အရေအတွက်ဟာ 20 ကနေ 30 ဝန်းကျင်သာ ရှိပါတယ်။ အဲဒီပမာဏထက် ပိုများတဲ့ Requests တွေကို Database ထဲ တိုက်ရိုက်ပေးဝင်မယ့်အစား PgBouncer Queue မှာ စနစ်တကျ တန်းစီစေခြင်းက စုစုပေါင်း Query Throughput ကို အမြင့်ဆုံး ဖြစ်စေပါတယ်။
Database Tuning Strategies Comparison
Production Database တစ်ခုမှာ ကဏ္ဍအလိုက် အသုံးပြုရမယ့် Optimization နည်းလမ်းတွေကို အောက်ပါဇယားမှာ နှိုင်းယှဉ်ကြည့်နိုင်ပါတယ်။
| Tuning Layer | Common Anti-Pattern | Production Bottleneck | High-Performance Solution |
|---|---|---|---|
| Pagination | OFFSET 100000 LIMIT 20 | Linear CPU & I/O degradation | Keyset cursor pagination (WHERE id < last_id) |
| Column Fetching | SELECT * with large TEXT/JSONB | Network & TOAST decompression overhead | Explicit column projection (SELECT id, name) |
| Bulk Writes | 1,000 separate INSERT statements | 1,000 network round-trips & WAL fsyncs | Multi-row batch INSERT or COPY stream |
| Connections | Direct DB connections per serverless call | FATAL: too many clients, CPU thrashing | PgBouncer in Transaction Pooling mode |
| Transactions | Holding transaction open during HTTP call | Row lock contention & idle-in-transaction | Move external API calls outside DB transaction |
MVCC Dead Tuples and Autovacuum Tuning
PostgreSQL ရဲ့ MVCC (Multi-Version Concurrency Control) စနစ်မှာ Row တစ်ခုကို UPDATE သို့မဟုတ် DELETE လုပ်တဲ့အခါ မူလ Row အဟောင်းကို Disk ပေါ်ကနေ ချက်ချင်း ဖျက်မပစ်ဘဲ Dead Tuple အဖြစ် အမှတ်အသားပြုပြီး Row အသစ်တစ်ခု ထပ်ရေးပါတယ်။ ဒါမှသာ တစ်ပြိုင်နက်တည်း ဖတ်နေတဲ့ အခြား Transactions တွေက Lock စောင့်စရာမလိုဘဲ ဆက်လက်ဖတ်ရှုနိုင်မှာ ဖြစ်ပါတယ်။
ဒါပေမဲ့ Write/Update အလွန်များတဲ့ Tables တွေမှာ Dead Tuples တွေ စုပုံလာပြီး Table Bloat မဖြစ်ပေါ်စေဖို့ Background Autovacuum Process က ပုံမှန် ရှင်းလင်းပေးရပါတယ်။ Rows သန်းချီရှိတဲ့ Hot Tables တွေမှာ Default autovacuum_vacuum_scale_factor (20%) ဟာ ပမာဏ ကြီးမားလွန်းတဲ့အတွက် 1% သို့မဟုတ် 2% (0.01–0.02) သို့ လျှော့ချပေးထားမှသာ မကြာခဏ သန့်ရှင်းရေးလုပ်ပေးပြီး Query Performance အမြဲတမ်း သွက်လက်နေမှာ ဖြစ်ပါတယ်။
export async function updateOrderTotal(orderId: string, itemId: string) {
// 1. Fetch slow external pricing BEFORE opening the DB transaction
const liveExchangeRate = await fxService.getCurrentRate('USD', 'MMK')
// 2. Keep the ACID transaction under a few milliseconds to avoid lock contention
return db.$transaction(async (tx) => {
const item = await tx.orderItem.findUniqueOrThrow({ where: { id: itemId } })
const updatedTotal = item.priceUsd * liveExchangeRate
return tx.order.update({
where: { id: orderId },
data: { totalMmk: updatedTotal },
})
})
}
အထက်ပါ ဥပမာအတိုင်း Database Transaction တစ်ခုဖွင့်ထားစဉ်အတွင်း ပြင်ပ HTTP API ခေါ်ယူခြင်း သို့မဟုတ် နှေးကွေးတဲ့ တွက်ချက်မှုတွေ လုံးဝမပြုလုပ်ဘဲ ပြင်ပမှာ အရင်တွက်ချက်ပြီးမှသာ Transaction ကို မီလီစက္ကန့်ပိုင်းအတွင်း အမြန်ဆုံး ဖွင့်/ပိတ် ပြုလုပ်ခြင်းက Row Lock Contention နဲ့ idle in transaction ပြဿနာတွေကို အထိရောက်ဆုံး ကာကွယ်ပေးပါတယ်။ ဒါ့အပြင် PostgreSQL ရဲ့ statement_timeout နဲ့ idle_in_transaction_session_timeout တန်ဖိုးတွေကိုလည်း သတ်မှတ်ပေးထားခြင်းအားဖြင့် မျှော်လင့်မထားတဲ့ Stuck Queries တွေက Connection Pool တစ်ခုလုံးကို ပိတ်ဆို့ထားခြင်းမှ အလိုအလျောက် ကာကွယ်နိုင်ပါတယ်။
Final Thoughts
Database Performance Tuning ဆိုတာ SQL Query တစ်ကြောင်းချင်းစီကို ပြင်ဆင်ရုံသာမက Query Planner၊ Indexing Strategy၊ Connection Pooling နဲ့ Storage Engine ရဲ့ MVCC သဘာဝတို့ကို ဟန်ချက်ညီအောင် ပေါင်းစပ်စီမံခန့်ခွဲခြင်း ဖြစ်ပါတယ်။ EXPLAIN (ANALYZE, BUFFERS) နဲ့ ပိတ်ဆို့မှုအမှန်ကို ရှာဖွေပြီး၊ Keyset Pagination၊ Batch Operations နဲ့ PgBouncer Connection Pooling တို့ကို လက်တွေ့အသုံးချခြင်းအားဖြင့် Hardware အဆင့်မြှင့်စရာ မလိုဘဲ Database ရဲ့ Throughput ကို အဆပေါင်းများစွာ မြှင့်တင်ပေးနိုင်မှာ ဖြစ်ပါတယ်။