1

我正在做一个项目,该项目需要我在 CouchDB 数据库中搜索近似匹配,然后对结果进行排序。我决定使用 Mango 查询系统,因为我事先不知道将使用哪个字段和排序组合。这部分的近似匹配不是问题,我创建的索引表现非常好。但是,当我对结果进行排序时,即使从 _explain 我可以看到它正在使用我的索引,整个事情也会变慢。

我在这些查询中尽可能明确,因为我发现它有助于 CouchDB 自动找到正确的索引。

这是我毫无问题地进行的普通查询的示例:

{
   "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
}

其中 arr_one 是我正在查看的数组,我试图在第一个元素上找到一个以 findOne 开头的字符串,在第二个元素上找到一个以 findTwo 开头的字符串。

我的索引的相关部分如下所示:

"fields": [
  "arr_one.0",
  "arr_one.0",
  "arr_one.1",
  "arr_one.1"
]

此查询也运行得非常快,并找到与上面相同的结果,但没有正确排序结果:

{
   "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
}

使用索引:

"fields": [
  "arr_one.0",
  "arr_one.0",
  "arr_one.1",
  "arr_one.1"
  "sort"
]

现在,这是在搜索和排序中起作用但需要很长时间才能完成的问题查询和索引:

{
   "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
}

使用索引:

"fields": [
  "sort,
  "arr_one.0",
  "arr_one.0",
  "arr_one.1",
  "arr_one.1"
]

任何试图弄清楚如何优化这一点的帮助将不胜感激。我愿意接受任何建议。

编辑:

我已经简化了问题,但仍然遇到同样的问题。我没有使用数组,而是只尝试使用单个值和单个范围。即使有索引,仍然会得到同样的慢查询。

{
   "selector": {
      "$and": [
         {
            "sort": {
               "$gt": null
            }
         },
         {
            "val": {
               "$gte": "findOne"
            }
         },
         {
            "val": {
               "$lt": "findOne\ufff0"
            }
         }
      ]
   },
   "sort": [
      {
         "sort": "asc"
      },
      {
         "val": "asc"
      }
   ],
   "limit": 25
}

带索引:

"fields": [
  "sort",
  "val",
  "val"
]
4

0 回答 0