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

SQL Indexes and Query Performance

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

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

  • CREATE INDEX ရေးရန်
  • Composite index order ကိုနားလည်ရန်
  • Index trade-offs ကိုရှင်းပြရန်

ရိုးရိုးလေး ရှင်းပြမယ်

Index သည် table တစ်ခုလုံး scan မလုပ်ဘဲ row ကိုမြန်မြန်ရှာနိုင်စေသော data structure ဖြစ်သည်။ WHERE, JOIN နှင့် ORDER BY တွင်မကြာခဏသုံးသော selective column များအတွက်သင့်တော်သည်။ Index များလွန်းလျှင် write နှင့် storage cost တက်သည်။

sql
CREATE INDEX idx_orders_customer_created
ON orders (customer_id, created_at DESC);

EXPLAIN
SELECT id, total, created_at
FROM orders
WHERE customer_id = 42
ORDER BY created_at DESC
LIMIT 10;
You should see
Index Scan using idx_orders_customer_created on orders

လက်တွေ့စမ်းကြည့်ရန်

users table တွင် unique email index တစ်ခုဖန်တီးပြီး duplicate email insert ဖြစ်သောအခါဘာဖြစ်သည်ကိုစမ်းပါ။

PostgreSQL IndexesPostgreSQL

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

  • Column တိုင်းကို index လုပ်ခြင်း
  • Composite index column အစီအစဉ်ကို query pattern မစဉ်းစားဘဲရွေးခြင်း

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

users table တွင် unique email index တစ်ခုဖန်တီးပြီး duplicate email insert ဖြစ်သောအခါဘာဖြစ်သည်ကိုစမ်းပါ။

You'll know it worked when: Index Scan using idx_orders_customer_created on orders

SQL Indexes and Query Performance | Thuta Learning