You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Firestore两种删除方案的操作次数及成本优化咨询

Firebase Firestore两种文档删除方案的计费操作次数分析

方案1

代码实现:

FirebaseFirestore firestore = FirebaseFirestore.instance;
final docRef = await firestore.collection('users').where('username', isEqualTo: username).limit(1).get();
final data = docRef.docs[0].reference;
await data.delete();

对应的计费操作次数:

  • 读操作:1次(执行get()查询时读取了1个匹配的文档)
  • 写操作:0次
  • 删除操作:1次(执行delete()删除目标文档)

方案2

代码实现:

FirebaseFirestore firestore = FirebaseFirestore.instance;
final docRef = await firestore.collection('users').doc(docID);
await docRef.delete();

对应的计费操作次数:

  • 读操作:0次(doc(docID)仅在本地生成文档引用,不会触发云端读请求)
  • 写操作:0次
  • 删除操作:1次(执行delete()删除目标文档)

优化建议

如果要尽可能减少计费操作次数,优先选择方案2。方案1需要先查询获取文档引用,额外产生了1次读操作的费用,而方案2直接通过已知的文档ID定位并删除,没有额外的读开销。

内容的提问来源于stack exchange,提问作者whatwhatwhat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 05:52:37