วิเคราะห์การดำเนินการค้นหาด้วย Query Explain

หน้านี้อธิบายวิธีดึงข้อมูลการดำเนินการค้นหาเมื่อคุณเรียกใช้การค้นหา

ใช้ Query Explain

คุณสามารถใช้ Query Explain เพื่อทำความเข้าใจวิธีดำเนินการกับการค้นหา ซึ่งจะให้รายละเอียดที่คุณใช้เพื่อเพิ่มประสิทธิภาพการค้นหาได้

คุณสามารถใช้ Query Explain ผ่านคอนโซล Google Cloud หรือคำสั่ง explain

คอนโซล

เรียกใช้การค้นหาในเครื่องมือแก้ไขการค้นหา แล้วเปิดแท็บคำอธิบาย

  1. ในคอนโซล Google Cloud ให้ไปที่หน้าฐานข้อมูล

    ไปที่ฐานข้อมูล

  2. เลือกฐานข้อมูล Cloud Firestore จากรายการฐานข้อมูล คอนโซล Google Cloud จะเปิด Firestore Explorer สำหรับฐานข้อมูลนั้น
  3. ป้อนคำค้นหาในเครื่องมือแก้ไขการค้นหา แล้วคลิกเรียกใช้
  4. คลิกแท็บคำอธิบายเพื่อดูเอาต์พุตการวิเคราะห์การค้นหา

    แท็บ Query Explain ในคอนโซล
MongoDB API

Query Explain ใน MongoDB API ได้รับการรองรับผ่านคำสั่ง explain ซึ่งคุณสามารถใช้ในเครื่องมือต่างๆ เช่น Mongo Shell และ Compass

คำสั่ง explain รองรับคำสั่ง aggregate, find, distinct และ count เช่น

db.collection.explain('executionStats').find(...)

นอกจากนี้ คุณยังใช้วิธี explain() ได้ด้วย เช่น

db.collection.find({QUERY}).explain('executionStats')
ข้อจำกัด
โปรดทราบข้อจำกัดและความแตกต่างต่อไปนี้
  • Query Explain ไม่รองรับคำสั่งที่แสดงเคอร์เซอร์ ตัวอย่างเช่น ระบบไม่รองรับการเรียกใช้ explain โดยการเรียกใช้คำสั่งต่อไปนี้โดยตรง

    db.collection.aggregate(..., explain: true)
  • Query Explain รองรับเฉพาะคำสั่ง find, aggregate, count, distinct, update, delete และ findAndModify เท่านั้น

  • Query Explain รองรับโหมดการพูดรายละเอียด executionStats, allPlansExecution และ queryPlanner

    • queryPlanner: แสดงผลเฉพาะแผนการดำเนินการโดยไม่เรียกใช้การค้นหา
    • executionStats และ allPlansExecution: แสดงผลแผนการดำเนินการพร้อมกับสถิติการเรียกเก็บเงิน หน่วยความจำ และการดำเนินการ

    หากไม่ได้ระบุโหมดการพูดรายละเอียด Shell จะใช้ค่าเริ่มต้นเป็น queryPlanner หากต้องการดูสถิติการดำเนินการทั้งหมด คุณต้องระบุโหมดการพูดรายละเอียด executionStats หรือ allPlansExecution

การวิเคราะห์

เอาต์พุตของ Query Explain ประกอบด้วย 2 องค์ประกอบหลัก ได้แก่ สถิติสรุปและแผนผังการดำเนินการ ลองดูตัวอย่างคำค้นหานี้

db.orders.aggregate(
 [
   { "$match": { "user_id": 1234 } },
   { "$sort": { "date_placed": 1 } }
 ]
)

สถิติสรุป

ด้านบนของเอาต์พุตที่อธิบายจะมีสรุปสถิติการดำเนินการ ใช้สถิติเหล่านี้เพื่อพิจารณาว่าการค้นหามีเวลาในการตอบสนองหรือค่าใช้จ่ายสูงหรือไม่ นอกจากนี้ยังมีสถิติหน่วยความจำซึ่งช่วยให้คุณทราบว่าการค้นหาใกล้ถึงขีดจำกัดหน่วยความจำมากน้อยเพียงใด

Execution:
 results returned: 35
 query id: 7e7b37ea1a259d79
 request peak memory usage: 45.56 KiB (46,656 B)
 data bytes read: 24.58 KiB (25,175 B)
 entity row scanned: 265

Billing:
 read units: 7

แผนผังการดำเนินการ

แผนผังการดำเนินการจะอธิบายการดำเนินการค้นหาเป็นชุดโหนด โหนดด้านล่าง (โหนดปลายสุด) จะดึงข้อมูลจากเลเยอร์พื้นที่เก็บข้อมูลซึ่งจะข้ามขึ้นไปตามโครงสร้างเพื่อสร้างการตอบกลับการค้นหา

โปรดดูรายละเอียดเกี่ยวกับโหนดการดำเนินการแต่ละโหนดที่หัวข้อข้อมูลอ้างอิงเกี่ยวกับการดำเนินการ

ดูรายละเอียดเกี่ยวกับวิธีใช้ข้อมูลนี้เพื่อเพิ่มประสิทธิภาพการค้นหาได้ที่เพิ่มประสิทธิภาพการดำเนินการค้นหา

ต่อไปนี้เป็นตัวอย่างของแผนผังการดำเนินการ

Execution:
 results returned: 35
 query id: 7e7b37ea1a259d79
 request peak memory usage: 45.56 KiB (46,656 B)
 data bytes read: 24.58 KiB (25,175 B)
 entity row scanned: 265

Billing:
 read units: 7

Tree:
• Compute
|  $out_1: map_set($record_1, "__id__", $__id___1, "__key__", unset)
|  is query result: true
|
|  Execution:
|   records returned: 35
|   latency: 204.87 ms (local 7.64 ms)
|
└── • Compute
    |  $__id___1: _id($__key___2)
    |
    |  Execution:
    |   records returned: 35
    |   latency: 197.23 ms (local 2.04 ms)
    |
    └── • MajorSort
        |  fields: [$v_5 ASC]
        |  output: [$__key___2, $record_1]
        |
        |  Execution:
        |   records returned: 35
        |   latency: 195.20 ms (local 28.42 ms)
        |   peak memory usage: 45.56 KiB (46,656 B)
        |
        └── • Compute
            |  $v_5: offset($v_4, 0L)
            |
            |  Execution:
            |   records returned: 35
            |   latency: 166.78 ms (local 14.84 ms)
            |
            └── • Compute
                |  $v_4: sortPaths(array($date_placed_1), [date_placed ASC])
                |
                |  Execution:
                |   records returned: 35
                |   latency: 151.94 ms (local 5.43 ms)
                |
                └── • TableScan
                       source: **/orders
                       order: STABLE
                       filter: $eq($user_id_1, 1,234)
                       output bindings: {$__key___2=row().__key__, $date_placed_1=row().date_placed, $record_1=row[* - { __create_time__, __update_time__ }](), $user_id_1=row().user_id}
                       output: [$__key___2, $date_placed_1, $record_1]

                       Execution:
                        records returned: 35
                        latency: 146.50 ms
                        data bytes returned: 3.25 KiB (3,325 B)
                        post-filtered rows: 230
                        records scanned: 265
                        data bytes read: 24.58 KiB (25,175 B)

ขั้นตอนถัดไป