Compound Indexes Optimization with ESR
Exercise
0. เตรียมระบบ (ถ้ายังไม่ได้ทำ)
สร้าง mongodb cluster บน mongoDB Atlas และเชื่อมต่อกับ Mongo Cluster
1. ทดสอบ Compound Index with sort
- จาก database sample_mflix ให้เลือก movies collection มาใช้ทดสอบ
- ในช่อง Query ให้ใส่รายละเอียดคำสั่งดังนี้ และกดปุ่ม Find
{
rated: "G",
released: { $gte: ISODate("2000-01-01T00:00:00Z") }
}
Option > Sort
- ตรวจสอบผลลัพธ์ที่ได้ จะเป็นหนังที่มีการจัดอันดับเป็น G และมีการเข้าฉายตั้งแต่ปี 2000 ขึ้นไป และเรียงตามชื่อหนัง
- กดปุ่ม Explain และสังเกตค่าการทำงานต่อไปนี้
- Plan (สังเกตว่า Sort in memory และ Collscan)
- Document examined
- Document returned
- Execution Time
- กดปิดหน้าต่าง explain และกลับมาที่หน้าจอเดิม
- กด tab Indexes และสังเกตว่ามี index อะไรบ้าง
- กดปุ่ม Create Index และสร้าง index ใหม่ โดยใช้ field rated และ released และ title
- กลับมาที่ tab documents ให้ทำการเลือก Query ใหม่อีกครั้ง
{
rated: "G",
released: { $gte: ISODate("2000-01-01T00:00:00Z") }
}
Option > Sort
- กดปุ่ม find และตรวจสอบผลลัพธ์ที่ได้
- กดปุ่ม Explain และสังเกตค่าการทำงานต่อไปนี้
- Plan (สังเกตว่า Sort in memory ยังอยู่)
- Document examined
- Document returned
- Execution Time
- กด tab Indexes
- กดปุ่ม Create Index และสร้าง index ใหม่ โดยเราจะทำการเปลี่ยนการเรียงลำดับของ index ใหม่ที่สร้างขึ้นมา โดยให้เรียงตาม field rated และ title และ released ตามเทคนิคของ ESR (Equality, Sort, Range)
- กลับมาที่ tab documents ให้ทำการเลือก Query ใหม่อีกครั้ง
{
rated: "G",
released: { $gte: ISODate("2000-01-01T00:00:00Z") }
}
Option > Sort
- กดปุ่ม find และตรวจสอบผลลัพธ์ที่ได้
- กดปุ่ม Explain และสังเกตค่าการทำงานต่อไปนี้
- Plan (สังเกตว่า Sort in memory หายไปแล้ว)
- Document examined
- Document returned
- Execution Time