App Indexes
Indexing provides a way to significantly speed up various types of queries in apps. JourneyApps allows indexes to be defined for the app database in the data model.
Backend API Indexes
By default, indexes only affect offline queries in the app. To use indexes for OnlineDB or CloudCode queries or the backend API, the cloud_indexes
feature flag must be enabled in the app's Settings in OXIDE.
Indexes specifically help for cases where the query filters out a large percentage of the results. For, example, if there are 20 000 asset objects on a device, and a query only returns 5 results, indexing can speed up the query significantly. On the other hand, if the query returns 10 000 results (before skipping and limiting), indexing will not really improve the performance.
Indexes may also help for sorting results more efficiently.
Performance trade-off
Specifying unnecessary indexes can affect the performance of the app negatively. Indexes generally make writes slower, and use more storage.
Example Data Model
In this example, indexes are used on the belongs-to relationship between asset
and building
, and also on the make
and model
for when they are queried together.
Example queries
Debugging
During development, to test whether a query is using an index or not, you can use the explain()
function with JSON.stringify()
to display the result in a dialog in the app.
Here is an example:
The results of the above dialog will include the following results when running in a browser:
type
: ‘index’ or ‘full scan’scanned
: number of objects returned by the index before further filteringresults
: number of results returned after filteringranges
: which index is used, along with its boundsduration
: duration of the query in milliseconds
For example, for the above query:
On native containers on Android, iOS and Windows, SQLite is used, and the explain()
output returns the underlying SQLite query and query plan instead.
Reference Documentation
For more technical information on specifying indexes, please refer to the index reference documentation.
Last updated