Indexes
Aidbox index management is in draft stage. API will change.
Database indexes are essential for performance. In particular you will need indexes to speed up search requests.
Aidbox provides mechanisms to
manage indexes
suggest indexes
generate indexes automatically
Background
Aidbox uses PostgreSQL database for storage. Most of resource data is contained in resource column with jsonb type.
Consider simple example: active search parameter for Patient resource.
Let's try the search query
Use _explain to find out SQL query generated by this request
Possible response is
Corresponding SQL is
Here @>
is containment operator. It tests whether jsonb value on the right-hand side is contained in the jsonb value on the left-hand side.
Without indexes Postgres has to check this condition for every Patient resource stored in the database.
However, GIN indexes can speed up these kind of queries.
We can create GIN index for the resource
column
Now Postgres can use this index to make search much faster.
Functional indexes
Consider more complex example: name
search parameter for Patient
resource.
Request
Generates SQL like
Postgres' pg_trgm
module supports index searches for ILIKE
queries.
You can create functional index to speed up this query:
Index management
Managing indexes manually is quite tedious and error-prone.
Aidbox provides a mechanism to manage indexes automatically using Aidbox configuration project.
Consider this example
Here
box
is the Aidbox project entrypointrepositories
is an Aidbox servicepatient-repository
is an Aidbox repository. It configures resource behavior. Currently it can only add indexes and search parameters.my-index
is index definition
Use aidbox.index.v1/sync-indexes
RPC to update indexes.
This RPC runs workflow that creates indexes requested by your configuration. And removes indexes not requested.
Aidbox managed indexes start with aidbox_mng_idx
prefix. So your custom indexes which do not start with this prefix will not be affected by sync-indexes
RPC.
Auto-generated indexes
You can make index by your own with Index Suggestion API. However, Aidbox can be configured to make indexes for desired SearchParameters at start automatically.
Import aidbox.index.v1
in the example above and add :indexes
into patient-repository.
Add new symbol my-index1
with tag aidbox.index.v1/auto-index
to make index on start, based on Index Suggestion API for Patient.brthd SearchParameter.
After restart new index will be added.
How to make my index explicitly with SQL?
Use aidbox.index.v1/index
tag with :expression
and PostgreSQL index:type
fields:
After restart new index will be added:
As in previous section, actuall creation/deletion of indexes is triggered with aidbox.index.v1/sync-indexes
RPC.
Formal description of Zen Indexes:
Zen IndexesIndex suggestion
Aidbox provides two RPCs which can suggest you indexes
Suggest indexes for parameter
Use aidbox.index/suggest-index
RPC to get index suggestion for specific search parameter
Suggest indexes for query
Use aidbox.index/suggest-index-query
RPC to get index suggestions based on query
Last updated