I have a database with >10K records and use couchbase-lite-core opensource to manage.
The database has one of the indices with information: index name: “rec_duration”, on field: “duration”
When ORDER_BY duration (either DESC or ASC), here is the c4query_explain() and query performance result:
SELECT fl_result(fl_root(_doc.body)) FROM kv_default AS _doc WHERE (_doc.key LIKE 'rec:%') AND (_doc.flags & 1 = 0) ORDER BY fl_value(_doc.body, 'duration') DESC LIMIT MAX(0, 8) OFFSET MAX(0, 0)
10|0|0| SCAN TABLE kv_default AS _doc USING INDEX rec_duration
[\"SELECT\",{\"WHAT\":[[\".\"]],\"WHERE\":[\"LIKE\", [\".\",\"_id\"], \"rec:%\"],\"OFFSET\":0,\"LIMIT\":8,\"ORDER_BY\":[[\"DESC\",[\".duration\"]]]}]
{QueryEnum#4} Created on {Query#3} with 8 rows (3922 bytes) in 1.812ms
SELECT fl_result(fl_root(_doc.body)) FROM kv_default AS _doc WHERE (_doc.key LIKE 'rec:%') AND (_doc.flags & 1 = 0) ORDER BY fl_value(_doc.body, 'duration') LIMIT MAX(0, 8) OFFSET MAX(0, 0)
10|0|0| SCAN TABLE kv_default AS _doc USING INDEX rec_duration
[\"SELECT\",{\"WHAT\":[[\".\"]],\"WHERE\":[\"LIKE\", [\".\",\"_id\"], \"rec:%\"],\"OFFSET\":0,\"LIMIT\":8,\"ORDER_BY\":[[\".duration\"]]}]
{QueryEnum#6} Created on {Query#5} with 8 rows (3724 bytes) in 3540.810ms
You can see that DESC order_by takes only 1.812ms while default (ASC) order_by takes 3540.810ms.
I also read about sqlite indexing, they can search from both direction. I’m not sure about couchbase lite.
I’ve read same topics in the forum, but it seems they have problems with DESC order_by (opposite to my case) Is there any bug in my application?
Anw, I haven’t found any solution yet. Can anyone here explain? Or is there something wrong with my query statement?
Thanks a lot!