နားလည်ထားရမယ့် အချက်
Schema diagram ဆိုတာ database ရဲ့ မြေပုံတစ်ခုပါ — table အတွက် box၊ primary key တစ်ခုစီအတွက် မျဉ်း၊ foreign key ဘယ်ကို ညွှန်းလဲပြသတဲ့ မြားတွေပါဝင်ပါတယ်။ တစ်ကြည့်ချင်း ဖတ်တတ်ဖို့ core skill တစ်ခုပါ။
အောက်က learning platform ရဲ့ diagram မှာ Users, Courses, Lessons, Enrollments, Progress, Bookmarks တွေပါဝင်ပြီး foreign key တိုင်းကို ညွှန်းတဲ့ table ဆီ မြားနဲ့ ပြထားပါတယ်။
- "Course တစ်ခုရဲ့ lesson အားလုံးကို အစီအစဉ်အတိုင်း ရယူ" -> Lesson.course_id ပေါ်မှာ index (order နဲ့ sort ပါ) လိုအပ်ပါတယ်။
- "Course တစ်ခုလုံးအတွက် user တစ်ဦးရဲ့ progress ရယူ" -> Progress.user_id ပေါ်မှာ index (သို့) course နဲ့ ပေါင်းထားတဲ့ index လိုအပ်ပါတယ်။
- "URL slug နဲ့ course ရှာ" -> primary key တစ်ခုတည်းမက Course.slug ပေါ်မှာ unique index လိုအပ်ပါတယ်။
- "User တစ်ဦးရဲ့ bookmark ရယူ" -> Bookmark.user_id ပေါ်မှာ index လိုအပ်ပါတယ်၊ မဟုတ်ရင် lookup တိုင်း table တစ်ခုလုံး scan လုပ်ရပါလိမ့်မယ်။
ဘုံကျတဲ့ ကျော့ကွင်း
Schema တစ်ခုဟာ perfectly normalized ဖြစ်နိုင်ပေမယ့် abstract အနေနဲ့ ဒီဇိုင်းဆွဲပြီး screen/API endpoint အစစ်တွေ မှီခိုနေတဲ့ pattern တွေနဲ့ တစ်ခါမှ မစစ်ဆေးရင် performance ညံ့နိုင်ပါတယ်။
LEARNING PLATFORM SCHEMA
------------------------
USERS COURSES
+------------+ +-------------+
| PK id | | PK id |
| email | | slug |
| name | | title |
+------------+ +-------------+
| \ |
| \ |
| \ v
| \ LESSONS
| \ +-------------+
| \ | PK id |
| \--------->| FK course_id|
| | order |
| +-------------+
v ^
ENROLLMENTS |
+---------------+ |
| PK id | |
| FK user_id | |
| FK course_id | PROGRESS
+---------------+ +---------------+
| PK id |
BOOKMARKS | FK user_id |
+---------------+ | FK lesson_id |
| PK id | +---------------+
| FK user_id |
| FK lesson_id |
+---------------+လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်
Diagram ကနေ စတင်ပါ၊ ဒါပေမယ့် access pattern တွေကနေ အဆုံးသတ်ပါ။ Learning platform ရဲ့ screen တွေလိုအပ်တဲ့ query အတိအကျကို ချရေးပါ — course page, dashboard, course-detail page, saved-items page။
Pattern တစ်ခုစီက schema ဖြေရမယ့် test အသေးလေးတစ်ခုပါ။ Index မပါဘဲ filter လုပ်ရင် data များလာတာနဲ့အမျှ ပိုဆိုးလာမယ့် table scan ဖြစ်ပါတယ် — ဒါတွေက hypothetical မဟုတ်ဘဲ production code က request တိုင်းမှာ run မယ့် filter အတိအကျတွေပါ။
အောက်က code ဥပမာက အဲဒီ test ကို function အနေနဲ့ encode လုပ်ထားပါတယ်: candidate schema နဲ့ access pattern list ကို ယူပြီး ဘယ်ဟာက ထိရောက်စွာ ဖြေနိုင်ပြီးသားလဲ၊ ဘယ်ဟာက index လိုအပ်လဲ၊ ဘယ်ဟာက schema ပြောင်းရမလဲ report လုပ်ပေးပါတယ်။
အတူတူ စမ်းရေးကြည့်မယ်
function checkAccessPatterns(accessPatterns, schema) {
return accessPatterns.map((pattern) => {
const entitySchema = schema[pattern.entity];
if (!entitySchema) {
return { ...pattern, verdict: "schema change needed (entity does not exist)" };
}
if (!entitySchema.fields.includes(pattern.filterField)) {
return { ...pattern, verdict: "schema change needed (field missing)" };
}
if (!entitySchema.indexes.includes(pattern.filterField)) {
return { ...pattern, verdict: "needs an index" };
}
return { ...pattern, verdict: "efficient" };
});
}
const accessPatterns = [
{ name: "Get all lessons for a course", entity: "lessons", filterField: "course_id" },
{ name: "Get a user's progress", entity: "progress", filterField: "user_id" },
{ name: "Find a course by slug", entity: "courses", filterField: "slug" },
{ name: "Get a user's bookmarks", entity: "bookmarks", filterField: "user_id" }
];
const schema = {
lessons: { fields: ["id", "course_id", "title", "order"], indexes: ["id"] },
progress: { fields: ["id", "user_id", "lesson_id", "completed_at"], indexes: ["id", "user_id"] },
courses: { fields: ["id", "slug", "title"], indexes: ["id"] },
bookmarks: { fields: ["id", "user_id", "lesson_id"], indexes: ["id"] }
};
const results = checkAccessPatterns(accessPatterns, schema);
for (const r of results) {
console.log(r.name + " -> " + r.verdict);
}Sample schema အပေါ် စစ်ကြည့်တဲ့အခါ access pattern ၄ ခုထဲက ၃ ခုက "needs an index" လို့ ပြန်လာပါတယ် (course_id နဲ့ lesson ရှာတာ၊ slug နဲ့ course ရှာတာ၊ user_id နဲ့ bookmark ရှာတာ တို့မှာ field အဲဒီပေါ် index မရှိပါ)၊ "Get a user's progress" ကတော့ Progress ပေါ်မှာ user_id ကို index လုပ်ပြီးသားမို့ "efficient" လို့ ပြန်လာပါတယ်။ ဒီ mix ကို တမင်ရွေးထားတာပါ — checker က ကွက်လပ်တွေကို တကယ်ဖော်ထုတ်နိုင်တာကို ပြသဖို့ပါ၊ အားလုံးကောင်းတယ်လို့ ပြောနေတာ မဟုတ်ပါဘူး။၅ မိနစ် စမ်းကြည့်
AccessPatterns array ထဲကို { name: "Find a lesson by its slug within a course", entity: "lessons", filterField: "slug" } ဆိုတဲ့ pattern အသစ် ထည့်ပြီး code ကို ပြန် run ကြည့်ပါ။ ဘယ် verdict ပြန်လာလဲ၊ schema object ကို ဘယ်လိုပြောင်းရင် ဖြေရှင်းနိုင်မလဲ။
သတိလေးတစ်ချက်
Schema diagram ကို query အစစ်တွေနဲ့ စစ်ဆေးရမယ့် အစပြု မြေပုံသာမက design ပြီးသားအဖြစ် သဘောထားခြင်းပါ။
Schema ကို မှန်ကန်စွာ normalize လုပ်ပေမယ့် app ရဲ့ access pattern အစစ်တွေကို တစ်ခါမှ စာရင်းမပြုစုတာက production ကနှေးမှသာ index ကွက်လပ်တွေကို ပေါ်လာစေတတ်ပါတယ်။
MDN: Structuring related data — How Databases Work