Single Field Indexes
Exercise
0. เตรียมระบบ (ถ้ายังไม่ได้ทำ)
สร้าง mongodb cluster บน mongoDB Atlas และเชื่อมต่อกับ Mongo Cluster
1. ทดสอบการค้นหาข้อมูลโดยไม่ใช้ Index
- จาก database sample_mflix ให้เลือก users collection มาใช้ทดสอบ
- ในช่อง Query ให้ใส่คำสั่งดังนี้ และกดปุ่ม Find
- ตรวจสอบผลลัพธ์ที่ได้
- กดปุ่ม Explain และสังเกตค่าที่ได้
- Plan ที่ได้คือ COLLSCAN หมายความว่า mongodb ไล่เทียบเช็คค่าตามเงื่อนไขทุก document ใน collection
- 185 documents examined คือ mongodb ไล่เช็คทุก document ใน collection
- 1 documents returned คือ mongodb ได้เจอ 1 document ที่ตรงกับเงื่อนไข
- Execution Time คือเวลาที่ใช้ในการค้นหา
2. ทดสอบการค้นหาด้วยการใช้ Single Field Indexes
- จาก database sample_mflix ให้เลือก users collection มาใช้ทดสอบ
- ในช่อง Query ให้ใส่คำสั่งดังนี้ และกดปุ่ม Find
{ email :"sean_bean@gameofthron.es" }
- ตรวจสอบผลลัพธ์ที่ได้
- กดปุ่ม Explain และสังเกตค่าที่ได้
- Plan ที่ได้คือ IXSCAN -> FETCH หมายความว่า mongodb นำเงื่อนไขที่ตรงกับ index มาเทียบก่อน และส่ง document ที่ตรงออกไปจาก collection
- จาก database sample_mflix ให้เลือก comments collection มาใช้ทดสอบ
- ในช่อง Query ให้ใส่คำสั่งดังนี้ และกดปุ่ม Find
{email: "mercedes_tyler@fakegmail.com" }
- ตรวจสอบผลลัพธ์ที่ได้
- กดปุ่ม Explain และสังเกตค่าการทำงานต่อไปนี้
- Plan
- Document examined
- Document returned
- Execution Time
- กดปิดหน้าต่าง explain และกลับมาที่หน้าจอเดิม
- กด tab Indexes และสังเกตว่ามี index อะไรบ้าง
- กดปุ่ม Create Index และสร้าง index ใหม่ โดยใช้ field email และเลือกเป็น ascending (1)
- กลับมาที่ tab documents ให้ทำการเลือก Query ใหม่อีกครั้ง
{email: "mercedes_tyler@fakegmail.com" }
- กดปุ่ม find และตรวจสอบผลลัพธ์ที่ได้
- กดปุ่ม Explain และสังเกตค่าการทำงานต่อไปนี้
- Plan
- Document examined
- Document returned
- Execution Time
- กลับไปที่ tab indexes และลบ index email ออก