mongodb-dba-workshop

Compound Indexes with Sort

Exercise

0. เตรียมระบบ (ถ้ายังไม่ได้ทำ)

สร้าง mongodb cluster บน mongoDB Atlas และเชื่อมต่อกับ Mongo Cluster

1. ทดสอบ Compound Index with sort

  1. จาก database sample_mflix ให้เลือก movies collection มาใช้ทดสอบ
  2. ในช่อง Query ให้ใส่รายละเอียดคำสั่งดังนี้ และกดปุ่ม Find
{ year: 2000 }

Option > Sort

{ "tomatoes.viewer.meter": -1 }
  1. ตรวจสอบผลลัพธ์ที่ได้ จะเป็นหนังปี 2000 ที่ sort ตาม tomatoes.viewer.meter จากมากไปน้อย
  2. กดปุ่ม Explain และสังเกตค่าการทำงานต่อไปนี้
  3. Plan (สังเกตว่า Sort in memory)
  4. Document examined
  5. Document returned
  6. Execution Time
  7. กดปิดหน้าต่าง explain และกลับมาที่หน้าจอเดิม
  8. กด tab Indexes และสังเกตว่ามี index อะไรบ้าง
  9. กดปุ่ม Create Index และสร้าง index ใหม่ โดยใช้ field year และ tomatoes.viewer.meter และเลือกเป็น ascending (1) และ descending (-1) ตามลำดับ
  10. กลับมาที่ tab documents ให้ทำการเลือก Query ใหม่อีกครั้ง
{ year: 2000 }

Option > Sort

{ "tomatoes.viewer.meter": -1 }
  1. กดปุ่ม find และตรวจสอบผลลัพธ์ที่ได้
  2. กดปุ่ม Explain และสังเกตค่าการทำงานต่อไปนี้
  3. Plan
  4. Document examined
  5. Document returned
  6. Execution Time
  7. สังเกตว่า Sort in memory จะหายไป และจะเป็นการใช้ index ที่สร้างขึ้นมาใหม่