2

I need the total no. of records available for the condition to display like - "Page 1 of 'Total Records'" (I'm displaying data in a grid). According to Couchbase forums, sort count metrics should give me the total records but it gives me zero. Any help is highly appreciated. using the .NET SDK.

N1QL query used:

        select  Title
        from `CMS`
        where LastUpdatedOn > 0 and IsLatestVersion = true   
        order by LastUpdatedOn DESC LIMIT 2

Index used:

    CREATE INDEX `CMS_idx_LastUpdatedOnDesc_IsLatestVersion` ON `CMS`(LastUpdatedOn DESC, IsLatestVersion);

1 Answer 1

3

The query ORDER BY clause follows index key order. So query uses Index order and avoids sort and provide the results quickly. When that happens sortCount in metrics will be missing (which will be zero 0). As it uses index order it terminates index scan once it satisfies query. So the total qualified records are unknown. You need to issue explicit count query to know total results.

Even if query does explicit sort with LIMIT + OFFSET (<= 8192), It will use TOP n sort. In that case also sortCount may not reflect right vlaue.

The following index can perform better. The query recognizes the IsLatestVersion equality predicate and still uses index order. This avoids more index scan pages (similar like btree)

CREATE INDEX `CMS_idx_LastUpdatedOnDesc_IsLatestVersion` ON 
  `CMS`(IsLatestVersion, LastUpdatedOn DESC, Title); 

SELECT  Title
FROM `CMS`
WHERE LastUpdatedOn > 0 and IsLatestVersion = true   
ORDER BY LastUpdatedOn DESC 
LIMIT 2

https://blog.couchbase.com/offset-keyset-pagination-n1ql-query-couchbase/

3
  • Thanks very much, mate, the answer was useful. I have one more question, I'm facing a performance issue after using OR condition in a query. What should be the index for the below query? SELECT Count(1) FROM CMS WHERE LastUpdatedOn > 0 AND (EditedBy = 'me' OR IsLatestVersion = true)
    – Kannan M
    Aug 26, 2020 at 10:40
  • Thank you, that link was very useful. I tried the below small query to check if I get sortcount (just for learning), but sort count is still zero even for this small query, Am I missing something? I would be happy if I'm able to see the sortcount for any small query. SELECT Title FROM CMS` WHERE LastUpdatedOn > 0 and IsLatestVersion = true`
    – Kannan M
    Aug 26, 2020 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.