使用碎片化字段优化的查询

本页介绍了如何在 Firestore 中查看和控制碎片化字段的使用情况。此功能在 Firestore 企业版中提供。

写入文档时,Firestore 可能会确定应以碎片化格式存储特定字段。通过仅读取所需字段(而非整个文档),碎片化字段可优化查询性能。

可从切分字段中受益的查询

对碎片化字段的读取适用于以下查询形状(如果适用):

  • 聚合查询:仅需访问部分字段即可进行聚合操作的查询。例如:

    db.pipeline()
      .collection("/customers")
      .where(lessThan("account_balance", 0))
      .aggregate(
        countAll().as("total"),
      )
    

    或使用分组依据:

    db.pipeline()
      .collection("/customers")
      .where(lessThan("account_balance", 0))
      .aggregate({
        accumulators: [
          field('account_balance').average().as('avg_account_balance')
        ],
        groups: [field('market_segment')]
      })
    
  • 投影查询:仅返回特定字段子集的查询。例如:

    db.pipeline()
      .collection("/customers")
      .select("family_name", "given_name")
      .limit(10)
    
  • 过滤查询:Firestore 查询引擎确定使用碎片化字段进行文档过滤是有益的查询。例如:

    db.pipeline()
      .collection("/customers")
      .where(equal("given_name", "alice"))
    

查看粉碎字段的使用情况

您可以使用查询解释功能来检查查询是否使用了碎片化字段。查询计划中的 TableScan 节点包含一个 Storage 部分,其中包含以下指标:

  • 扫描形状
    • shredded_fields_only:查询仅从碎片化字段读取数据。
    • shredded_fields_backjoin:查询从碎片化字段读取数据,并与原始文档联接以获取其他字段。
  • 使用的切分字段:作为切分字段读取的字段名称列表。
  • 重新检查次数:重新检查的计数器映射。重新检查是指在扫描碎片化字段时,回退到从原始完整文档中读取。如果文档中的字段值超过 8 KiB,即超过了碎片化字段存储空间的大小,则可能会发生这种情况。

输出示例

...
└── • TableScan
        source: /customers
        order: UNDEFINED
        row range: (-∞..+∞)
        filter: ($account_balance_1 < 0L)
        output bindings: {$account_balance_1=account_balance, $market_segment_1=market_segment}
        variables: [$account_balance_1, $market_segment_1]

        Execution:
         records returned: 1,374
         latency: 26.58 ms
         post-filtered rows: 13,626
         records scanned: 15,000
         data bytes read: 23.73 MiB (24,887,141 B)

        Storage:
         scan shape: shredded_fields_only
         shredded fields used: [account_balance, market_segment]

控制碎片化字段的使用

默认情况下,Firestore 会使用已分片的字段(如果可用)。您可以使用 table_scan_method 查询选项控制此行为。

table_scan_method 选项支持的值:

  • shredded_fields_enabled(默认):使用碎片化字段(如有)。
  • shredded_fields_disabled:不使用碎片化字段。
  • force_shredded_fields:如果无法通过扫描碎片化字段来满足表扫描,则使查询失败。

示例

var opts = new PipelineExecuteOptions()
    .with("table_scan_method", "shredded_fields_disabled");

var snapshot = db.pipeline()
    .collection("/customers")
    .where(equal("given_name", "alice"))
    .execute(opts)
    .get();

查询性能警告

如果检测到切分字段的使用效率低下,Firestore 可能会在查询解释结果中发出性能警告。例如:

  • 选择性较低的查询:当查询扫描碎片化字段以进行过滤,但过滤掉的文档太少,导致扫描效率低下时,就会出现这种情况。

  • 高重新检查查询:指查询频繁回退到完整文档读取,这可能会影响性能。您可以使用 storage_size 等函数来识别触发重新检查的大值。

在这些情况下,请考虑使用查询选项停用碎片化字段读取。

限制

Firestore 仅粉碎顶级字段。它还限制了每个合集组可粉碎的字段数量。