Thuta Learning
How Databases Work
AdvancedData & Databasesbeginner

Schema Diagram နှင့် Access Pattern အပေါ်အခြေခံ Design

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

  • Schema Diagram နှင့် Access Pattern အပေါ်အခြေခံ Design concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • Diagram/table ကို ဖတ်ပြီး data model/schema/architecture ဘယ်လို ပုံသဏ္ဌာန်ရှိသလဲ ခြေရာခံနိုင်ရန်
  • ကိုယ့် project အတွက် database concept/system ကို ဘယ်လို အသုံးချသင့်သလဲ ရှင်းပြနိုင်ရန်

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

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 ညံ့နိုင်ပါတယ်။

text
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 လုပ်ပေးပါတယ်။

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

javascript
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);
}
You should see
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 dataHow Databases Work

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

  • Schema diagram ကို query အစစ်တွေနဲ့ စစ်ဆေးရမယ့် အစပြု မြေပုံသာမက design ပြီးသားအဖြစ် သဘောထားခြင်းပါ။
  • Schema ကို မှန်ကန်စွာ normalize လုပ်ပေမယ့် app ရဲ့ access pattern အစစ်တွေကို တစ်ခါမှ စာရင်းမပြုစုတာက production ကနှေးမှသာ index ကွက်လပ်တွေကို ပေါ်လာစေတတ်ပါတယ်။
  • ဒီ course က database concept/landscape ကို framework-neutral level မှာသာ သင်ပေးပါတယ် — SQL syntax, PostgreSQL, MongoDB, Redis ကို နက်နက်ရှိုင်းရှိုင်း လေ့လာချင်ရင် SQL, PostgreSQL, MongoDB, Redis tutorial တွေဆီ ဆက်သွားပါ။

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

AccessPatterns array ထဲကို { name: "Find a lesson by its slug within a course", entity: "lessons", filterField: "slug" } ဆိုတဲ့ pattern အသစ် ထည့်ပြီး code ကို ပြန် run ကြည့်ပါ။ ဘယ် verdict ပြန်လာလဲ၊ schema object ကို ဘယ်လိုပြောင်းရင် ဖြေရှင်းနိုင်မလဲ။

You'll know it worked when: 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 က ကွက်လပ်တွေကို တကယ်ဖော်ထုတ်နိုင်တာကို ပြသဖို့ပါ၊ အားလုံးကောင်းတယ်လို့ ပြောနေတာ မဟုတ်ပါဘူး။

Schema Diagram နှင့် Access Pattern အပေါ်အခြေခံ Design | Thuta Learning