Multi-collection indexes

Overview

A multi-collection index is an index which includes documents from more than one collection.

Indexes which cover multiple collections may be less performant than those which cover a single index. If possible, it’s a better practice to organize your collections and queries so that multi-collection indexes are not necessary.

Example

For demonstration purposes, let’s create two collections named fruit and flowers.

Copied!
The Shell version of this example is not currently available.
Query metrics:
  •    bytesIn:  135

  •   bytesOut:   35

  • computeOps:    1

  •    readOps:    0

  •   writeOps:    2

  •  readBytes:  822

  • writeBytes:  656

  •  queryTime: 46ms

  •    retries:    0

The following example adds some documents to the fruit collection:

Copied!
The Shell version of this example is not currently available.
Query metrics:
  •    bytesIn:   399

  •   bytesOut:    35

  • computeOps:     1

  •    readOps:     0

  •   writeOps:     3

  •  readBytes:    42

  • writeBytes: 1,006

  •  queryTime:  47ms

  •    retries:     0

Now let’s add some documents to the flowers collection:

Copied!
The Shell version of this example is not currently available.
Query metrics:
  •    bytesIn:  405

  •   bytesOut:   36

  • computeOps:    1

  •    readOps:    0

  •   writeOps:    3

  •  readBytes:   42

  • writeBytes:  997

  •  queryTime: 34ms

  •    retries:    0

To make these two collections searchable by their color field, we can create an index which specifies both collections in the source field and color in the terms field.

Copied!
The Shell version of this example is not currently available.
Query metrics:
  •    bytesIn:   179

  •   bytesOut:   408

  • computeOps:     1

  •    readOps:     0

  •   writeOps:     2

  •  readBytes: 1,316

  • writeBytes: 1,343

  •  queryTime:  64ms

  •    retries:     0

The following example searches the index fruit_and_flowers_search_by_color for documents with the string red in the color field.

Copied!
The Shell version of this example is not currently available.
Query metrics:
  •    bytesIn:  151

  •   bytesOut:  160

  • computeOps:    1

  •    readOps:    0

  •   writeOps:    0

  •  readBytes:    0

  • writeBytes:    0

  •  queryTime: 86ms

  •    retries:    0

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!