နားလည်ထားရမယ့် အချက်
Index က table အားလုံးမဖတ်ဘဲ row ကိုရှာနိုင်စေသော်လည်း insert/update/delete တိုင်း index ကိုပါပြင်ရပါတယ်။ Primary key နှင့် unique constraint များက index ဖန်တီးပေးပေမယ့် foreign key referencing column ကို PostgreSQL ကအလိုအလျောက် index မလုပ်ပါဘူး။ Composite index column order ကို query predicates နဲ့ sort order အလိုက်ရွေးရပါတယ်။
လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
Published tutorial feed က `is_published` true rows ကို date/id descending ဖြင့်ဖတ်လို့ partial composite index သုံးမယ်။ Lessons lookup အတွက် foreign key column နှင့် lesson number ကို index လုပ်ပါ။ Index တစ်ခုထည့်တိုင်း EXPLAIN နှင့် write overhead ဖြင့်အတည်ပြုရပါတယ်။
အတူတူ စမ်းရေးကြည့်မယ်
CREATE INDEX lessons_tutorial_number_idx
ON app.lessons (tutorial_id, lesson_number);
CREATE INDEX tutorials_published_feed_idx
ON app.tutorials (published_at DESC, tutorial_id DESC)
WHERE is_published = true;
SELECT indexname, indexdef
FROM pg_indexes
WHERE schemaname = 'app';Lesson lookup နှင့် published feed အတွက် workload-specific indexes နှစ်ခုရမည်။၅ မိနစ် စမ်းကြည့်
User email lookup၊ enrollment by user နှင့် incomplete progress query တို့အတွက် index candidates ရေးပြီး column order ကိုရှင်းပါ။
သတိလေးတစ်ချက်
Column တိုင်း index ထည့်ခြင်းက write နှေး၊ disk ကြီးနှင့် planner choice ရှုပ်စေပါတယ်။
PostgreSQL — Indexes — PostgreSQL Global Development Group