I’m trying to build an index to target the following query:
SELECT
DISTINCT 'Mission::' || submission.missionId as missionIds
FROM brandbassador_dev submission
WHERE submission.docType="Submission"
AND (status = "request" OR offer IS NOT MISSING)
LIMIT 10
I’ve built the following index:
CREATE INDEX `idx_submission` ON `brandbassador_dev`(status, offer, missionId)
WHERE docType="Submission" AND ((status ="request") OR (offer IS NOT MISSING))
but it does not appear to target my query. Even if I use USE INDEX to force the index to be used, it defaults to the primary index. Am I missing something here? I’m using Couchbase Community DP (4.6). Thanks!
CREATE INDEX `idx_submission_status` ON `brandbassador_dev`(status, missionId)
WHERE docType="Submission" AND (status ="request");
and
CREATE INDEX `idx_submission_offer` ON `brandbassador_dev`(offer, missionId)
WHERE docType="Submission" AND (offer IS NOT MISSING);
and run the following N1QL
SELECT
submission.missionId as missionIds
FROM brandbassador_dev submission
WHERE submission.docType = "Submission"
AND submission.status = "request"
UNION
SELECT
submission.missionId as missionIds
FROM brandbassador_dev submission
WHERE submission.docType = "Submission"
AND submission.offer IS NOT MISSING
LIMIT 10
Thanks for the prompt reply! Maybe it’s best if I present the whole use case:
SELECT
DISTINCT 'Mission::' || submission.missionId as missionIds,
SUM(CASE WHEN submission.status = 'request' OR submission.offer IS NOT MISSING THEN 1 ELSE 0 END) AS requests,
SUM(CASE WHEN submission.status = 'pending' THEN 1 ELSE 0 END) AS pending,
FROM brandbassador_dev submission
WHERE submission.docType="Submission"
AND (status IN ["request", "pending"] OR offer IS NOT MISSING)
LIMIT 10
This is the query I wanted to run; am I still able to do it with UNION?
I have a different query now, but the problem is the same. I have this query:
SELECT RAW submission.docId
FROM brandbassador_dev submission
WHERE submission.docType="Submission"
AND submission.network IS NOT MISSING
AND submission.status IN ["approved", "evaluating", "pending"]
AND submission.response IS NOT MISSING
I’ve tried to create variations of an index that also covers docId, but the query is not targeted, even with USE INDEX:
CREATE INDEX idx_submission_network_dev ON brandbassador_dev(docId, status) WHERE docType="Submission" AND network IS NOT MISSING AND response IS NOT MISSING
CREATE INDEX idx_submission_network_dev ON brandbassador_dev(docId, status, network, response) WHERE docType="Submission" AND network IS NOT MISSING AND response IS NOT MISSING
The index you’ve provided did work, but I didn’t understand what you meant by “(i.e leading index key needs to be present in predicate)”. Could you give me an example?
Why did this index not work?
CREATE INDEX idx_submission_network_dev ON brandbassador_dev(status, network, response, docId) WHERE docType="Submission" AND network IS NOT MISSING AND response IS NOT MISSING
In post 11 the index is different. The leading key is docId and no predicate on the docId. That is why index is not selected.
FYI: Index uses b-tree, you can only scan b-tree when leading key is present.
The following index will select and works. I have removed the extra conditions in index where clause whcih unnessary and added more index keys make query covered.