Thanks Prasad,
For your detail:
-
My database is just simple database running on only one node Centos 6.8 (dedicated server for couchbase, 4 cores, 8GB RAM, right now free 1GB ram) with exactly 261334 records (select count(*) from default), each record about 3KB
-
I also found out the query is only very slow with DESC condition when ordering, this is the completed_request:
2.1) With DESC: 19s
"completed_requests": {
"ClientContextID": "f69c6ba7-9c68-44a6-adb6-ea6dd92b729c",
"ElapsedTime": "19.88647505s",
"ErrorCount": 0,
"PhaseCounts": {
"Fetch": 249307,
"IndexScan": 249307,
"Sort": 249308
},
"PhaseOperators": {
"Fetch": 1,
"IndexScan": 1,
"Sort": 1
},
"RequestId": "9f30aea7-ae8e-46f5-9822-cf27351493dc",
"ResultCount": 1,
"ResultSize": 3107,
"ServiceTime": "19.886412997s",
"State": "completed",
"Statement": "select * from default where ngayDangTin > 20140801 order by ngayDangTin desc limit 1",
"Time": "2016-09-03 10:31:57.184899229 +0700 ICT"
}
2.2) Without DESC: very fast 31.12ms
My N1QL: “select * from default where ngayDangTin > 20140801 order by ngayDangTin limit 2”, but I dont’ know why completed_request does not capture it.
- EXPLAIN of query:
3.1) With DESC
[
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan",
"index": "ngayDangTin_idx",
"index_id": "683705dc9f6b6507",
"keyspace": "default",
"namespace": "default",
"spans": [
{
"Range": {
"Inclusion": 0,
"Low": [
"20140801"
]
}
}
],
"using": "gsi"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Fetch",
"keyspace": "default",
"namespace": "default"
},
{
"#operator": "Filter",
"condition": "(20140801 < (`default`.`ngayDangTin`))"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "self",
"star": true
}
]
}
]
}
}
]
},
{
"#operator": "Order",
"limit": "2",
"sort_terms": [
{
"desc": true,
"expr": "(`default`.`ngayDangTin`)"
}
]
},
{
"#operator": "Limit",
"expr": "2"
},
{
"#operator": "FinalProject"
}
]
},
"text": "select * from default where ngayDangTin > 20140801 order by ngayDangTin DESC limit 2"
}
]
3.2) Without DESC:
[
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan",
"index": "ngayDangTin_idx",
"index_id": "683705dc9f6b6507",
"keyspace": "default",
"limit": "2",
"namespace": "default",
"spans": [
{
"Range": {
"Inclusion": 0,
"Low": [
"20140801"
]
}
}
],
"using": "gsi"
},
{
"#operator": "Parallel",
"maxParallelism": 1,
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Fetch",
"keyspace": "default",
"namespace": "default"
},
{
"#operator": "Filter",
"condition": "(20140801 < (`default`.`ngayDangTin`))"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "self",
"star": true
}
]
}
]
}
}
]
},
{
"#operator": "Limit",
"expr": "2"
},
{
"#operator": "FinalProject"
}
]
},
"text": "select * from default where ngayDangTin > 20140801 order by ngayDangTin limit 2"
}
]