0

我有以下查询:

{
   "selector":{
      "lastname":{
         "$regex":"(?i)[cç][oòóôõöø]"
      },
      "firstname":{
         "$gt":null
      },
      "type":"person",
      "owner":{
         "$in":["admin"]
      }
   },"sort":["lastname","firstname"]
}

并尝试了许多索引:

{
 "type": "json",
 "def": {
  "fields": [
   {
    "lastname": "asc"
   }
  ]
 }
}

{
 "type": "json",
 "def": {
  "fields": [
   {
    "lastname": "asc"
   },
   {
    "firstname": "asc"
   }
  ]
 }
}

{
 "type": "json",
 "def": {
  "fields": [
   {
    "lastname": "asc"
   },
   {
    "firstname": "asc"
   },
   {
    "type": "asc"
   },
   {
    "owner": "asc"
   }
  ]
 }
}

但没有一个奏效。仅供参考,我正在使用 CouchDB 2.1.0。

我也尝试添加"sort":["lastname","firstname","type","owner"]到查询中。仍然收到警告:no matching index found, create an index to optimize query time

我究竟做错了什么?

编辑:我将 PouchDB 直接用于我的 CouchDB 服务器(不同步),如果这可以帮助...

4

2 回答 2

0

您以前创建过索引吗?

this._DB.createIndex({
   index: { fields: ['lastname', 'firstname'] },
   ddoc: "my-index-design-doc"
})

并在您的 .find 中使用它而不是 with use_index: 'my-index-design-doc' ?

于 2017-11-28T11:41:43.653 回答
0

作为旁注,请记住,在大型数据库中,您查询的性能后果将是巨大的。运算符 $in 和 $regex 将在内存中处理整个数据库,因为它不使用任何索引——也不能,至少在最新的 CouchDB 版本:3.0.2 中。

另一方面,$eq, $gt — $gte, $lt — $lte 将索引而不处理所有数据。这就是它变得聪明的地方。

于 2020-04-24T04:44:54.557 回答