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

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

ใช้ Query Explain

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

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

คอนโซล

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

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

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

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

    แท็บอธิบายคำค้นหาในคอนโซล
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: แสดงผลแผนการดำเนินการพร้อมกับการเรียกเก็บเงิน หน่วยความจำ และสถิติการดำเนินการ

    หากไม่ได้ระบุโหมดรายละเอียดไว้ เชลล์จะใช้ 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)

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