From 252a3f67f0e092f6dccf2d739d9c194489204359 Mon Sep 17 00:00:00 2001 From: Erwan Guyader Date: Wed, 5 Apr 2023 10:35:19 +0200 Subject: [PATCH] fix: Prevent fallback to existing jobs index (#2644) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The in-progress jobs query for the export feature relies on the presence of a very specific index to be fast on cozies with a lot of `io.cozy.jobs` documents. To make sure the index presence detection goes well, we forced a sort on the `worker` attribute. However, since `cozy-stack` uses an index on `worker` and `state` only, CouchDB fallbacks to this index instead of detecting the index we want does not exist and runs the slow `$or` query in-memory. See https://github.com/apache/couchdb/issues/4511 for more details on this issue. To prevent this fallback we will sort on a second attribute not present in the existing `cozy-stack` index: `message.slug`. ``` ### 🐛 Bug Fixes * Prevent fallback on sub-optimal index for in-progress jobs query. ``` --- src/ducks/export/helpers.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ducks/export/helpers.js b/src/ducks/export/helpers.js index e2608262ea..c37b2c1872 100644 --- a/src/ducks/export/helpers.js +++ b/src/ducks/export/helpers.js @@ -18,8 +18,8 @@ export const isExportJobInProgress = async client => { }, $or: [{ state: 'queued' }, { state: 'running' }] }) - .indexFields(['worker']) - .sortBy([{ worker: 'asc' }]) // XXX: forces CouchDB to require an index for the query + .indexFields(['worker', 'message.slug']) + .sortBy([{ worker: 'asc' }, { 'message.slug': 'asc' }]) // XXX: forces CouchDB to require an index for the query .limitBy(1) )