Hello CouchBase Community,
The N1QL query is taking more response time when we add a specific where clause.
For your understanding, we have two sets of documents (pages and instances). There is one - many relationship between a page and instances. The pageId will be common for a given page and its instances.
The WHERE clause mostly revolves around the request time in milliseconds. For each instance document, there is an effective time and expiry time as shown below
page
{
id: 1
pageId: β/page1β
}
inst
{
id: 12,
pageId: β/page1β,
effectiveTime:β1617778597591β,
expiryTime:β1617778597596β
}
inst
{
id: 34,
pageId: β/page1β,
effectiveTime:β1617778597561β,
expiryTime:β1617778597569β
}
When a specific page ID is requested, the where clause will make sure to return the instance document falling with the specified request time. If there is no document falling within the range, it will be 404
WHERE instance.pageId = β/page1β AND
(instance.effectiveTime <= 1617778597593 AND 1617778597593 <= instance.expiryTime)
Until this point, we have a very good response time.
However, we have a new requirement where for a particular set of pages, if βallowPastInstance = trueβ is set at the page document level, then we need to fetch the latest expired instance
page
{
id: 1
pageId: β/page1β
allowPastInstance: true
}
inst
{
id: 12,
pageId: β/page1β,
effectiveTime:β1617778597591β,
expiryTime:β1617778597596β
}
inst
{
id: 34,
pageId: β/page1β,
effectiveTime:β1617778597561β,
expiryTime:β1617778597569β
}
We tried to add an extra where clause along with the existing one as below
WHERE instance.pageId = β/page1β AND
((instance.effectiveTime <= 1617778597599 AND 1617778597599 <= instance.expiryTime) OR (page.allowPastInstance = true AND instance.effectiveTime <= 1617778597599))
Adding the new where clause increased the response time drastically, we also observed that, if this query is executed against a huge number of documents, the response time is going high.
Any help or suggestions to improve the response time would be appreciated.
Note: We tried to create the indexes as per query workbench advice, but it didnt help much.
Thanks,
Kannan S M