Skip to content

Commit

Permalink
ds mongo OPTIMIZE speed up queries with indices
Browse files Browse the repository at this point in the history
  • Loading branch information
Humblesaw authored and michalvasko committed Jul 26, 2024
1 parent 6635f3c commit 18d15ad
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/plugins/ds_mongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -2978,8 +2978,9 @@ srpds_mongo_install(const struct lys_module *mod, sr_datastore_t ds, const char
mongo_plg_conn_data_t *pdata = (mongo_plg_conn_data_t *)plg_data;
sr_error_info_t *err_info = NULL;
bson_error_t error;
bson_t *bson_query = NULL, *bson_index_key1 = NULL;
mongoc_index_model_t *im = NULL;
uint32_t i, idx_cnt = 3;
bson_t *bson_query = NULL, *bson_index_keys[idx_cnt];
mongoc_index_model_t *im[idx_cnt];
char *process_user = NULL, *process_group = NULL;
struct timespec spec = {0};

Expand All @@ -2989,10 +2990,16 @@ srpds_mongo_install(const struct lys_module *mod, sr_datastore_t ds, const char
goto cleanup;
}

/* create indices on prev for quicker data retrieval while managing the userordered lists */
bson_index_key1 = BCON_NEW("prev", BCON_INT32(1));
im = mongoc_index_model_new(bson_index_key1, NULL /* opts */);
if (!mongoc_collection_create_indexes_with_opts(mdata.module, &im, 1, NULL /* opts */, NULL /* reply */, &error)) {
/* create a compound index on prev and path for load_next(),
* compound index on order and path_no_pred for srpds_shift_uo_list_recursively()
* and index on path_modif for quicker loading */
bson_index_keys[0] = BCON_NEW("prev", BCON_INT32(1), "path_no_pred", BCON_INT32(1));
bson_index_keys[1] = BCON_NEW("order", BCON_INT32(1), "path_no_pred", BCON_INT32(1));
bson_index_keys[2] = BCON_NEW("path_modif", BCON_INT32(1));
for (i = 0; i < idx_cnt; ++i) {
im[i] = mongoc_index_model_new(bson_index_keys[i], NULL /* opts */);
}
if (!mongoc_collection_create_indexes_with_opts(mdata.module, im, idx_cnt, NULL /* opts */, NULL /* reply */, &error)) {
ERRINFO(&err_info, plugin_name, SR_ERR_OPERATION_FAILED, "mongoc_collection_create_indexes_with_opts", error.message)
goto cleanup;
}
Expand Down Expand Up @@ -3038,8 +3045,10 @@ srpds_mongo_install(const struct lys_module *mod, sr_datastore_t ds, const char
cleanup:
free(process_user);
free(process_group);
mongoc_index_model_destroy(im);
bson_destroy(bson_index_key1);
for (i = 0; i < idx_cnt; ++i) {
mongoc_index_model_destroy(im[i]);
bson_destroy(bson_index_keys[i]);
}
bson_destroy(bson_query);
srpds_data_destroy(pdata, &mdata);
return err_info;
Expand Down

0 comments on commit 18d15ad

Please sign in to comment.