How to create custom MongoDB indexes
- I am trying to use ST2 REST API to get executions.
- The request is as follows:
curl -s \
-H 'Content-Type: application/json'
-H "St2-Api-Key: ${ST2_API_KEY}"\
-k 'localhost:80/api/v1/executions?offset=0&limit=100&parent=null'
-
This request takes about 5 to 8 seconds in our environment.
-
I checked the mongoDB's execution plan and found that the sorting process took a long time.
-
I created a following index to speed up the sorting process.
db.action_execution_d_b.createIndex({ parent: 1, start_timestamp: -1, 'action.ref': 1 }, { background: true })
- The index has improved request speed. However, the created index is automatically deleted.
2024-11-15T03:22:50.069+0900 I ACCESS [conn965898] Successfully authenticated as principal st2admin on st2 from client xxx.xxx.xxx.xxx:50468
2024-11-15T03:22:50.103+0900 I COMMAND [conn965898] CMD: dropIndexes st2.action_execution_d_b
- Then I have two questions
- Is it a specification that user-created index is deleted?
- Is there a way to prevent user-created index from being automatically deleted?
The registration function will automatically clean up the mongo index, and you can add additional index passes in the code file below st2common/st2common/models/db/action.py
Thank you for your response.
I added an index to st2common/models/db/execution.py and the index was created the next day and has been maintained to date! It seems that ST2 periodically manages the index automatically, but is it possible to reflect changes in the files under models/db/ at any given time?
I remember that the index will be rebuilt when the service is restarted. You can check the source code for this