Hi, I try to implement pagination in couchbase. But my query with indexes is slow. It takes almost 3 min to run query.
I want to get distinct threadId count which contains in displayTo. How can i create right index for that query ?
select distinct raw(threadId) from messages WHERE 10 IN displayTo
group by threadId, createdDate
order by createdDate desc
limit 20 OFFSET 0
Document
{
“threadId”: “1”,
“senderId”: 10,
“displayTo”: [
10,
20
],
“createdDate”: 1566589725671
}
{
“plan”: {
“#operator”: “Sequence”,
“~children”: [
{
“#operator”: “Sequence”,
“~children”: [
{
“#operator”: “IndexScan3”,
“index”: “idx_displayTo_senderId”,
“index_id”: “944f7efe923412ee”,
“index_projection”: {
“primary_key”: true
},
“keyspace”: “messages”,
“namespace”: “default”,
“spans”: [
{
“range”: [
{
“inclusion”: 0,
“low”: “null”
}
]
}
],
“using”: “gsi”
},
{
“#operator”: “Fetch”,
“keyspace”: “messages”,
“namespace”: “default”
},
{
“#operator”: “Parallel”,
“~child”: {
“#operator”: “Sequence”,
“~children”: [
{
“#operator”: “Filter”,
“condition”: “(1313 in (messages
.displayTo
))”
},
{
“#operator”: “InitialGroup”,
“aggregates”: ,
“group_keys”: [
“(messages
.threadId
)”,
“(messages
.createdDate
)”
]
}
]
}
},
{
“#operator”: “IntermediateGroup”,
“aggregates”: ,
“group_keys”: [
“(messages
.threadId
)”,
“(messages
.createdDate
)”
]
},
{
“#operator”: “FinalGroup”,
“aggregates”: ,
“group_keys”: [
“(messages
.threadId
)”,
“(messages
.createdDate
)”
]
},
{
“#operator”: “Parallel”,
“~child”: {
“#operator”: “Sequence”,
“~children”: [
{
“#operator”: “InitialProject”,
“distinct”: true,
“raw”: true,
“result_terms”: [
{
“expr”: “(messages
.threadId
)”
}
]
},
{
“#operator”: “Distinct”
}
]
}
},
{
“#operator”: “Distinct”
}
]
},
{
“#operator”: “Order”,
“limit”: “20”,
“sort_terms”: [
{
“desc”: true,
“expr”: “(messages
.createdDate
)”
}
]
},
{
“#operator”: “Limit”,
“expr”: “20”
},
{
“#operator”: “FinalProject”
}
]
},
“text”: “select distinct raw(threadId) from messages WHERE 10 IN displayTo \n group by threadId, createdDate\n order by createdDate desc\n limit 20 OFFSET 0”
}
Any help would be appreciated