已切碎的欄位
本頁面說明如何查看及控管與 MongoDB 相容的 Firestore 中已切碎欄位的使用情形。這項功能適用於 Firestore Enterprise 版。
寫入文件時,Firestore 可能會判斷特定欄位應以碎片化格式儲存。透過僅讀取必要欄位 (而非完整文件),切碎欄位可最佳化查詢效能。
可從切碎欄位獲益的查詢
如果可用,系統會將已切碎欄位的讀取作業套用至下列查詢形狀:
匯總查詢:這類查詢只需要存取部分欄位,即可執行匯總作業。例如:
db.customers.aggregate( [ { $match: { "account_balance" : { $lt : 0 } } }, { $count: "total" } ] );或使用 group-by:
db.customers.aggregate([ { $match: { "account_balance" : { $lt : 0 } } }, { $group: { _id: "$market_segment", avg_balance: { $avg: "$account_balance" } } } ]);投影查詢:只傳回特定欄位子集的查詢。例如:
db.customers.find({}, { family_name: 1, given_name: 1, _id: 0 });篩選查詢:Firestore 查詢引擎判斷使用切碎欄位篩選文件有益時,就會篩選查詢。例如:
db.customers.find({ given_name: "Alice" });
查看已清除欄位的使用情況
您可以使用查詢說明,檢查查詢是否使用切碎欄位。查詢計畫中的 TableScan 節點包含 Storage 區段,其中有下列指標:
- 掃描形狀:
shredded_fields_only:查詢只會讀取經過切碎的欄位。shredded_fields_backjoin:查詢會從經過切碎的欄位讀取資料,並與原始文件聯結,以取得其他欄位。
- 使用的切碎欄位:讀取為切碎欄位的屬性名稱清單。
- 重新檢查計數:重新檢查計數器的地圖。重新檢查是指掃描撕碎的欄位時,會改為讀取原始完整文件。如果文件中的欄位值超過 8 KiB,就可能發生這種情況,因為這超過了切碎欄位儲存空間的大小上限。
輸出範例
...
└── • TableScan
source: **/customers
order: UNDEFINED
row range: (-∞..+∞)
filter: $lt($account_balance_1, 0)
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]
控管已切碎欄位的使用情形
根據預設,與 MongoDB 相容的 Firestore 會在可用時使用切碎欄位。您可以在查詢註解中使用 tableScanMethod 選項,控管這項行為。
tableScanMethod 支援的值:
shreddedFieldsEnabled(預設):使用已切碎的欄位 (如有)。shreddedFieldsDisabled:系統不會使用已撕毀的欄位。forceShreddedFields:如果無法透過掃描已切碎的欄位完成資料表掃描,則查詢會失敗。
尋找指令
使用 .comment() 方法指定 find 指令的選項:
db.customers.find(
{ account_balance: 0.0 }
).comment(
{
firestoreOptions: {
tableScanMethod: "shreddedFieldsDisabled"
}
}
);
匯總指令
在 aggregate 指令的選項參數中,使用 comment 選項:
db.customers.aggregate(
[
{ $match: { "account_balance" : { $lt : 0 } } },
{ $count: "total" }
],
{
comment: {
firestoreOptions: {
tableScanMethod: "shreddedFieldsDisabled"
}
}
}
);
查詢效能警告
如果系統偵測到使用效率不彰的切碎欄位,與 MongoDB 相容的 Firestore 可能會在查詢說明結果中發出效能警告。例如:
低選擇性查詢:如果查詢掃描經過切碎的欄位進行篩選,但篩除的文件很少,導致掃描效率不彰。
高重新檢查查詢:如果查詢經常回溯至完整文件讀取,可能會影響效能。您可以使用
$bsonSize等運算子,找出會觸發重新檢查的大值。
在這種情況下,建議使用查詢選項停用已切碎的欄位讀取作業。
限制
Firestore 只會嘗試清除頂層欄位。此外,每個集合可切碎的欄位數量也有限制。