firestore: storage size calculations

Storage Size Calculations

This page describes the storage size of documents, document names, fields, and index entries in Cloud Firestore.

You can learn about the costs of this storage at  Cloud Firestore Pricing.

String size

String sizes are calculated as the number of UTF-8 encoded bytes + 1.

The following are stored as strings:

  • Document names
  • Collection IDs
  • Field names
  • String field values

For example:

  • The ID of the Task collection uses 4 bytes + 1 byte, for a total of 5 bytes.
  • The name of the description field uses 11 bytes + 1 byte, for a total of 12 bytes.

Document name size

The size of a document name is the sum of:

  • The full string size of the document name (integer IDs are 8 bytes each)
  • 16 additional bytes

For a document in a collection called Task with a numeric ID:

 
  
Task id:5730082031140864

The size is 5 + 8 + 16 = 29 bytes:

  • 5 bytes for the Task collection ID
  • 8 bytes for the numeric ID
  • 16 additional bytes

For a document in a collection called Task with a string ID:

 
  
Task name:my_task_id

The size is 5 + 11 + 16 = 32 bytes:

  • 5 bytes for the Task collection ID
  • 11 bytes for the my_task_id string ID
  • 16 additional bytes

Field size

The size of a field is the sum of:

  • The string size of the field name
  • The field value's size

The following table shows the size of field values by type.

Type Size
Array The sum of the sizes of its values
Boolean 1 byte
Bytes Byte length
Date and time 8 bytes
Floating-point number 8 bytes
Geographical point 16 bytes
Integer 8 bytes
Map The size of the map, calculated the same way as document size
Null 1 byte
Reference The document name size
Text string Number of UTF-8 encoded bytes + 1

For example, a boolean field named done would use 6 bytes:

  • 5 bytes for the done field name
  • 1 byte for the boolean value

Document size

The size of a document is the sum of:

This example is for a document in a Task collection with a numeric ID:

 
  
Task id:5730082031140864 - "type": "Personal" - "done": false - "priority": 1 - "description": "Learn Cloud Firestore"

The total size of the fields is 70 bytes:

Field name and value Field size in bytes
"type": "Personal" 14
5 for the field name + 9 for the field's string value
"done": false 6
5 for the field name + 1 for the field's boolean value
"priority": 1 17
9 for the field name + 8 for the field's integer value
"description": "Learn Cloud Firestore" 33
12 for the field name + 21 for the field's string value

So the document size is 29 + 70 + 32 = 131 bytes:

  • 29 bytes for the document name
  • 70 bytes for the fields
  • 32 additional bytes

Index entry size

The size of an index entry is the sum of:

  • The document name size of the indexed document
  • The sum of the indexed field sizes
  • The size of the indexed document's collection ID if the index is an automatic index (does not apply to composite indexes)
  • 32 additional bytes

This is the same pattern used for a document's size, except instead of including all of the document's fields, only those fields required by the index are stored. Also, the size of an index entry will never exceed 1500 bytes.

For example, using the same document as above:

 
  
Task id:5730082031140864 - "type": "Personal" - "done": false - "priority": 1 - "description": "Learn Cloud Firestore"

If done is an indexed field, the automatic index entry for the single field done index consists of the document name, the done field name and value, the Task collection ID, and 32 bytes for an index entry. The total size of this index entry is 72 bytes:

  • 29 bytes for the document name
  • 6 bytes for the done field name and boolean value
  • 5 bytes for the Task collection ID
  • 32 additional bytes

By default, Cloud Firestore automatically predefines two single-field indexes for each field of each document, one in ascending order and one in descending order. So this document would have a 72-byte index entry in the single-field done index in ascending order, and it would have a 72-byte index entry in the single-field done index in descending order.

Using the same document, consider a composite index that uses the done and priority fields (both ascending). The total size of the index entry in this index is 84 bytes:

  • 29 bytes for the document name
  • 6 bytes for the done field name and boolean value
  • 17 bytes for the priority field name and integer value
  • 32 additional bytes

猜你喜欢

转载自blog.csdn.net/leonqiu/article/details/79428134