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

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

ใช้ Query Explain

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

คุณสามารถใช้ Query Explain ผ่านคอนโซล Firebase หรือใช้ ไลบรารีไคลเอ็นต์เซิร์ฟเวอร์ Firestore

คอนโซล
  1. ในคอนโซล Firebase ให้ไปที่ ฐานข้อมูลและการจัดเก็บข้อมูล > Firestore > ข้อมูล > เครื่องมือแก้ไขการค้นหา

  2. เรียกใช้การค้นหาที่คุณต้องการข้อมูลการดำเนินการ
  3. คลิกแท็บ Query Explain เพื่อดูเอาต์พุตการวิเคราะห์การค้นหา
Node.js (ผู้ดูแลระบบ)
import { field } from '@google-cloud/firestore/pipelines';

const q = db.pipeline()
        .collection('/users')
        .sort(field('status').ascending())
        .limit(100);
let results;
try {
    results = await q.execute({
        explainOptions: { mode: 'analyze', outputFormat: 'text' }
    });
} catch (error) {
    console.log(error);
}
const metrics = results?.explainStats?.text;

console.log(metrics);
Java (ผู้ดูแลระบบ)
Pipeline q = db.pipeline()
        .collection("/users")
        .sort(field("status").ascending())
        .limit(100);

PipelineExecuteOptions pipelineOpts = new PipelineExecuteOptions().withExplainOptions(
        new ExplainOptions().withExecutionMode(ExplainOptions.ExecutionMode.ANALYZE)
);
Pipeline.Snapshot result = q.execute(pipelineOpts).get();

String metrics = null;
if (result.getExplainStats() != null) {
    metrics = result.getExplainStats().getText();
    System.out.println(metrics);
}

โหมด Explain

คุณสามารถเรียกใช้การค้นหาด้วย Query Explain ในโหมดต่างๆ ได้ ทั้งนี้ขึ้นอยู่กับสิ่งที่คุณต้องการดีบัก

  • analyze: วางแผนและเรียกใช้การค้นหา แสดงข้อมูลเครื่องมือวางแผน สถิติและเมตริกการดำเนินการรันไทม์ รวมถึงผลลัพธ์ปกติที่การค้นหาแสดง

  • explain: วางแผนการค้นหา แต่ไม่เรียกใช้ แสดงข้อมูลเครื่องมือวางแผน แต่ไม่แสดงสถิติ เมตริก หรือผลลัพธ์รันไทม์ โหมดนี้มีประโยชน์สำหรับการดีบักลักษณะการทำงานของการค้นหาโดยไม่ต้องเรียกใช้การดำเนินการที่มีค่าใช้จ่ายสูง

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

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

db.pipeline().collection('/users').sort(field("status").ascending()).limit(100)

สถิติสรุป

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

Execution:
 results returned: 2
 request peak memory usage: 20.25 KiB (20,736 B)
 data bytes read: 148 B
 entity row scanned: 2

Billing:
 read units: 1

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

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

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

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

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

Tree:
• Compute
|  $out_1: map_set($record_1, "__name__", $__name___1, "__key__", unset)
|  is query result: true
|
|  Execution:
|   records returned: 2
|   latency: 5.96 ms (local <1 ms)
|
└── • Compute
    |  $__name___1: map_get($record_1, "__key__")
    |
    |  Execution:
    |   records returned: 2
    |   latency: 5.88 ms (local <1 ms)
    |
    └── • MajorSort
        |  fields: [$v_1 ASC]
        |  output: [$record_1]
        |  limit: 100
        |
        |  Execution:
        |   records returned: 2
        |   latency: 5.86 ms (local <1 ms)
        |   peak memory usage: 20.25 KiB (20,736 B)
        |
        └── • Compute
            |  $v_1: map_get($record_1, "status")
            |
            |  Execution:
            |   records returned: 2
            |   latency: 5.23 ms (local <1 ms)
            |
            └── • TableScan
                   source: /users
                   order: UNDEFINED
                   properties: *
                   row range: (-∞..+∞)
                   output record: $record_1
                   variables: [$record_1]

                   Execution:
                    records returned: 2
                    latency: 4.68 ms
                    records scanned: 2
                    data bytes read: 148 B

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