Thuta Learning
ရှာဖွေရန်
AdvancedData & Databasesbeginner

EXPLAIN ANALYZE ဖြင့် Query Plan ဖတ်ခြင်း

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • EXPLAIN ANALYZE ဖြင့် Query Plan ဖတ်ခြင်း concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • နမူနာ SQL/command ကို ကိုယ်တိုင် run ပြီး output စစ်နိုင်ရန်
  • Tutorial Platform project နှင့် production scenario တွင် အသုံးချနိုင်ရန်

နားလည်ထားရမယ့် အချက်

`EXPLAIN` က query မrun ဘဲ estimated plan ပြပြီး `EXPLAIN ANALYZE` က query ကိုတကယ် run ကာ actual metrics ထည့်ပေးပါတယ်။ Cost က milliseconds မဟုတ်ပါ။ Estimated rows နဲ့ actual rows အလွန်ကွာရင် statistics သို့ data correlation ပြဿနာဖြစ်နိုင်ပါတယ်။ `BUFFERS` က cache/disk page activity ကိုမြင်စေပါတယ်။

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

Published feed query ကို index မတိုင်မီ/ပြီးနောက် plan နှိုင်းမယ်။ Production write query ကို `EXPLAIN ANALYZE` လုပ်ရင် တကယ်ပြောင်းလဲမှာဖြစ်လို့ read-only transaction သို့ staging data ပေါ်မှာသာစမ်းပါ။

အတူတူ စမ်းရေးကြည့်မယ်

sql
EXPLAIN (ANALYZE, BUFFERS, VERBOSE)
SELECT tutorial_id, title, published_at
FROM app.tutorials
WHERE is_published = true
ORDER BY published_at DESC, tutorial_id DESC
LIMIT 10;
You should see
Scan type၊ row estimate/actual နှင့် buffer usage ပါသော execution plan ရမည်။

၅ မိနစ် စမ်းကြည့်

Index တစ်ခု drop/create မလုပ်ခင်နှင့်လုပ်ပြီးနောက် plan ကိုကူးပြီး ဘာပြောင်းသလဲသုံးချက်ရေးပါ။

သတိလေးတစ်ချက်

Table သေးသေးမှာ sequential scan ရွေးတာကို အမှားမယူဆပါနှင့်—index lookup ထက်တကယ်ဈေးသက်သာနိုင်ပါတယ်။

PostgreSQL — Using EXPLAINPostgreSQL Global Development Group

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Table သေးသေးမှာ sequential scan ရွေးတာကို အမှားမယူဆပါနှင့်—index lookup ထက်တကယ်ဈေးသက်သာနိုင်ပါတယ်။
  • နမူနာ code ကို production data ပေါ် တိုက်ရိုက်မစမ်းဘဲ local/test database နှင့် recoverable backup ပေါ်တွင် အရင်အတည်ပြုပါ။

လေ့ကျင့်ခန်း

Index တစ်ခု drop/create မလုပ်ခင်နှင့်လုပ်ပြီးနောက် plan ကိုကူးပြီး ဘာပြောင်းသလဲသုံးချက်ရေးပါ။

You'll know it worked when: Scan type၊ row estimate/actual နှင့် buffer usage ပါသော execution plan ရမည်။

EXPLAIN ANALYZE ဖြင့် Query Plan ဖတ်ခြင်း | Thuta Learning