SELECT MAX([ instance.expiryTime, { ‘page’:p1[0].page.pageId, ‘instance’:instance.pageId , ‘_ID’: META(instance).id, ‘_CAS’:META(instance).cas }])[1].*
FROM bucket AS instance
LET p1 = (
SELECT *
FROM bucket page
WHERE page.type = ‘page’
AND page.pageId IN [“testing-xdcr”,“xdcr-23”])
WHERE instance.isStacked = FALSE
AND instance.pageId IN [ p1[0].page.pageId ]
AND instance.type = ‘pagedef’
AND ((instance.effectiveTime <= 1617261825616
AND 1617261825616 <= instance.expiryTime)
OR (instance.effectiveTime <= 1617261825616
AND (instance.expiryTime IS NULL
OR instance.expiryTime IS MISSING)))
GROUP BY p1[0].page.pageId
This is working fine and returning with correct data, but as you see the p1 will be an array of page objects, it is only picking the zeroth element, as I am adding p1[0]. I want it to consider all objects of the page and further map it each with instance.pageId .
WITH pages AS ( SELECT page.pageId FROM bucket AS page
WHERE page.type = "page"
AND page.pageId IN ["testing-xdcr","xdcr-23"])
SELECT MAX([ instance.expiryTime, { instance.pageId , "_ID": META(instance).id, "_CAS":META(instance).cas }])[1].*
FROM bucket AS instance JOIN pages AS p ON instance.pageId = p.pageId
WHERE instance.isStacked = FALSE
AND instance.type = "pagedef"
AND ( (instance.effectiveTime <= 1617261825616 AND 1617261825616 <= instance.expiryTime)
OR (instance.effectiveTime <= 1617261825616 AND (instance.expiryTime IS NULL
OR instance.expiryTime IS MISSING)))
GROUP BY instance.pageId;
It is still subquery (materialized). The results will be ARRAY. If you need to every instance and look every element of pages. You need to use JOIN.
If using EE you can use HASH JOIN.
As you are looking only pageId form pages and no other info
WITH pages AS ( SELECT RAW page.pageId FROM bucket AS page
WHERE page.type = "page"
AND page.pageId IN ["testing-xdcr","xdcr-23"])
SELECT MAX([ instance.expiryTime, { instance.pageId , "_ID": META(instance).id, "_CAS":META(instance).cas }])[1].*
FROM bucket AS instance
WHERE instance.isStacked = FALSE
AND instance.type = "pagedef"
AND instance.pageId IN pages
AND ( (instance.effectiveTime <= 1617261825616 AND 1617261825616 <= instance.expiryTime)
OR (instance.effectiveTime <= 1617261825616 AND (instance.expiryTime IS NULL
OR instance.expiryTime IS MISSING)))
GROUP BY instance.pageId;
CREATE INDEX ix1 ON bucket(pageId) WHERE type = "page";
CREATE INDEX ix2 ON bucket(isStacked, pageId, effectiveTime,expiryTime, META().cas ) WHERE type = "pagedef";