Below is a N1QL query to retrieve data without ORDER BY, which give a response time of around 16ms:
SELECT epoch FROM sales WHERE companyId=2 LIMIT 3;
{
"requestID": "8883934f-dff0-4097-8361-8b870cc6c9cb",
"signature": {
"epoch": "json"
},
"results": [
{
"epoch": 1.441273895e+09
},
{
"epoch": 1.441272745e+09
},
{
"epoch": 1.441271542e+09
}
],
"status": "success",
"metrics": {
"elapsedTime": "16.93128ms",
"executionTime": "16.781124ms",
"resultCount": 3,
"resultSize": 98
}
}
The N1QL query to retrieve data with ORDER BY, gives a response time of around 800ms.
SELECT epoch FROM sales WHERE companyId=2 ORDER BY epoch DESC LIMIT 3;
{
“requestID”: “f70ab2c2-4ba8-45c5-a4fa-e0fae6d07dc3”,
“signature”: {
“epoch”: “json”
},
“results”: [
{
“epoch”: 1.441280963e+09
},
{
“epoch”: 1.441280933e+09
},
{
“epoch”: 1.441280898e+09
}
],
“status”: “success”,
“metrics”: {
“elapsedTime”: “795.476822ms”,
“executionTime”: “795.393153ms”,
“resultCount”: 3,
“resultSize”: 144,
“sortCount”: 10460
}
}
Is it possible to retrieve documents from a bucket in sorted order based on their Document Id without using ORDER BY? If not, how can we improve the performance using ORDER BY?