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:
To learn about the differences between regular and unique indexes, see Index Types.
It's important to note the following points before starting to code:
$startsWith
, aren't accelerated by an index.To use the Indexes API, install the @wix/data
package.
To install the package, follow the instructions below based on your development environment.
When developing with:
or
When developing CLI or self-hosted Wix apps, as well as headless apps or sites, run the following command:
or
To import the package in your code:
The Indexes API allows you to create 2 kinds of 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.
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.
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.
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.