About the Indexes API

Warning: Wix Data APIs currently require the site's code editor to be enabled manually. For this reason, we recommend against using these APIs for developing Wix CLI apps and self-hosted apps.

The Indexes API enables you to create indexes for data collections, to make querying data faster.

An index is a map of a collection's data, organized according to fields that you select. An index increases the speed of queries by providing a query with a shortcut to the information it is looking for. Instead of iterating through every data item in the collection, a query can use the index to skip to potentially relevant items. The larger a collection is, the more time this saves.

With the Indexes API, you can:

  • Create regular indexes to accelerate query speed.
  • Create unique indexes to enforce unique values.
  • Access and drop existing indexes.

To learn about the differences between regular and unique indexes, see Index Types.

Before you begin

It's important to note the following points before starting to code:

  • A collection can have up to 3 regular indexes (for up to 3 fields per index). It may also have 1 unique index (for 1 field).
  • The value contained in an indexed field can't be larger than 982 bytes, including the collection name. For example, in a collection named "Products", the maximum length of each value in the indexed fields is 974 bytes.
  • When an index creation fails, the failed index still occupies a slot. To remove the failed index and free up the slot for a new index, call Drop Index.
  • Case-insensitive filters, such as $startsWith, aren't accelerated by an index.

Terminology

  • Regular index: An index that increases query speed. Up to 3 fields can be selected for a regular index.
  • Unique index: An index that increases query speed and enforces unique values for 1 field.
Did this help?

Setup

@wix/data

To use the Indexes API, install the @wix/data package.

Install the package

To install the package, follow the instructions below based on your development environment.

Wix sites and Blocks apps

When developing with:

Copy

or

Copy

Wix apps and Headless

When developing CLI or self-hosted Wix apps, as well as headless apps or sites, run the following command:

Copy

or

Copy

Import the package

To import the package in your code:

Copy
Did this help?

Index Types

The Indexes API allows you to create 2 kinds of indexes:

  • A regular index accelerates queries for up to 3 fields in a collection.
  • A unique index enforces unique values for 1 field in a collection and also functions as a regular index for that field.

Regular indexes

You can create up to 3 regular indexes for each collection. Most regular indexes can be created for up to 3 fields, with each field sorted either in ascending or descending order.

For example, consider a database containing information about many different products. Each data item contains productName, category, and price fields.

An index for the productName field in ascending order accelerates queries based on the productName field. Instead of searching through all of the data entries until one is found with the desired name, the index enables the query to jump based on alphabetical ordering.

An index for the category and price fields speeds up queries that include these fields. A general query for all products in a particular category, ordered by price, doesn't need to iterate through the entire collection to find all items in the specified category. The results of that search will already be ordered by price and thus will not need to be sorted again. For a large collection, this can save a significant amount of time.

Unique indexes

A unique index enforces unique values for an indexed field. You can create 1 unique index for each collection, in addition to its regular indexes.

A unique index can only have 1 field. For that field, it functions like a regular index. For example, a unique index for the productName field in a product database enables faster queries based on that field.

In addition, a unique index enforces unique values for the indexed field in all data items across the collection. So if you created a unique index for the productName field, you will not be able to create more than one item with any given productName value.

Available indexes

Basic website plans and most premium plans support 4 indexes, 3 regular and 1 unique. Certain tier premium plans support 15 indexes, 5 regular (up to 3 fields), 2 unique, and 8 additional regular fields (1 field). If a Wix user needs more indexes than their current plan offers, they can upgrade a site.

Did this help?

Indexes and Query Speed

Every time you query a data collection, you are initiating filtering and sorting operations that take time to carry out because they iterate through every item in the collection. For a small collection, the time it takes to execute a query and receive its results might be negligible. But as the quantity of data in a collection increases, each query takes longer to process, and this can impact the performance of your app.

An index is a map of the collection's data, organized according to fields that you select. An index increases the speed of queries by providing a query with a shortcut to the information it is looking for. Instead of iterating through every data item in the collection, a query can use the index to skip to potentially relevant items. The larger a collection is, the more time this saves.

The best fields to use for indexes are those with a wide range of values. Indexes of fields with a narrow range of values, such as booleans, don't speed up queries significantly.

Did this help?