Search code examples
sqlcouchbasesql++

is it ok to replace count(*) with count(1) in N1ql


There is scenario where i am counting all records in n1ql using

Select count(*) from bucket where type='xyz' and column1='abc'.

Which taking around 25 sec but if i replace it with count(1) it takes 19sec. My question can we use count(1) instead of count(*). I believe it only counts only one column total counts rather than counting all. Let me know if this is not the case in n1ql.


Solution

  • If you are using CB 5.0 and above COUNT(*) and COUNT(1) are treated same.

    The following should perform better.

    CREATE INDEX ix1 ON bucket(column1) WHERE type = "xyz";
    SELECT COUNT(1) AS cnt
    FROM bucket
    WHERE type='xyz' AND column1='abc';