I'm working on a project that requires me to search a CouchDB database for an approximate match and then sort the results. I've decided to use the Mango query system because I don't know ahead of time what field and sort combo will be used. The approximate matching part of this hasn't been an issue, the index I have created performs very well. However, when I sort the results the entire thing slows down even though from the _explain I can see it is using my index.
I'm being explicit as possible in these queries because I find that it helps CouchDB to automatically find the right index.
This is an example of a plain query I make with no problems:
{
"selector": {
"$and": [
{
"arr_one.0": {
"$gte": "findOne"
}
},
{
"arr_one.0": {
"$lt": "findOne\ufff0"
}
},
{
"arr_one.1": {
"$gte": "findTwo"
}
},
{
"arr_one.1": {
"$lt": "findTwo\ufff0"
}
}
]
},
"fields": ["_id"],
"limit": 25
}
where arr_one is the array I am looking through and I'm trying to find a string starting with findOne on the first element and a string starting with findTwo on the second element.
The relevant parts of my index look like this:
"fields": [
"arr_one.0",
"arr_one.0",
"arr_one.1",
"arr_one.1"
]
This query also works very fast and finds the same results as above, but does not sort the results correctly:
{
"selector": {
"$and": [
{
"arr_one.0": {
"$gte": "findOne"
}
},
{
"arr_one.0": {
"$lt": "findOne\ufff0"
}
},
{
"arr_one.1": {
"$gte": "findOne"
}
},
{
"arr_one.1": {
"$lt": "findOne\ufff0"
}
},
{
"sort": {
"$gt": null
}
}
]
},
"sort": [
{
"arr_one.0": "asc"
},
{
"arr_one.1": "asc"
},
{
"sort": "asc"
}
],
"fields": ["_id"],
"limit": 25
}
With the index:
"fields": [
"arr_one.0",
"arr_one.0",
"arr_one.1",
"arr_one.1"
"sort"
]
Now, this is the problem query and index which works in searching and sorting but takes a long time to complete:
{
"selector": {
"$and": [
{
"sort": {
"$gt": null
}
},
{
"arr_one.0": {
"$gte": "findOne"
}
},
{
"arr_one.0": {
"$lt": "findOne\ufff0"
}
},
{
"arr_one.1": {
"$gte": "findOne"
}
},
{
"arr_one.1": {
"$lt": "findOne\ufff0"
}
}
]
},
"sort": [
{
"sort": "asc"
},
{
"arr_one.0": "asc"
},
{
"arr_one.1": "asc"
}
],
"fields": ["_id"],
"limit": 25
}
With the index:
"fields": [
"sort,
"arr_one.0",
"arr_one.0",
"arr_one.1",
"arr_one.1"
]
Any help trying to figure out how to optimize this would be appreciated. I'm open to any suggestions.
EDIT:
I've simplified the problem and am still getting the same issue. Instead of using an array, I'm only trying it with a single value and a single range. Still getting the same slow query even with an index.
{
"selector": {
"$and": [
{
"sort": {
"$gt": null
}
},
{
"val": {
"$gte": "findOne"
}
},
{
"val": {
"$lt": "findOne\ufff0"
}
}
]
},
"sort": [
{
"sort": "asc"
},
{
"val": "asc"
}
],
"limit": 25
}
With index:
"fields": [
"sort",
"val",
"val"
]