What's the reason that result of N1QL SELECT COUNT(*) is wrong?

Hi, everybody

I executed N1QL statement with n1ql_query function(python sdk) in my python program, and got a problem.

detail as follows:

My index create statement :
CREATE INDEX idx_leaderboard_score ON leaderboard_dev_test(leaderboard_id,score_val,update_counter,timestamp) WHERE (doc_type = “RANKING_SCORE”)

After updated db data, I used n1ql_query function immediately, and I printed out logs, as follows:

================= without count : SELECT * FROM leaderboard_dev_test WHERE doc_type=“RANKING_SCORE” AND leaderboard_id = “PVP5” AND timestamp >=0 AND timestamp <= 1505369092.27 AND (score_val < 1 OR (score_val=1 AND update_counter < 61))

================= with count : SELECT COUNT(*) FROM leaderboard_dev_test WHERE doc_type=“RANKING_SCORE” AND leaderboard_id = “PVP5” AND timestamp >=0 AND timestamp <= 1505369092.27 AND (score_val < 1 OR (score_val=1 AND update_counter < 61))

================= without count result len: 0, with count result : [{u’$1’: 1}]

It shows the results of two SQL statements are not match(0 is expected), the only different between two SQL Statements is one use count(*) and other not.

what’s the reason about it?

PS.
After updated db data, I used sleep function to sleep several seconds, then did same thing as above, the results got right. is it reason that the index data is updating after data updated, and during index data updating, select count(*) statement maybe use dirty index data and returns wrong result ?

Environment:
couchbase version : 4.5.1-2844 Community Edition (build-2844)
python sdk version : 2.2.1

Thanks for your attention.

Index is little behind and it need time to catchup. If you need to read the data you updated use scan_consistency option.

1 Like