【问题标题】:Can't find matching index with CouchDB and sort找不到与 CouchDB 匹配的索引并排序
【发布时间】:2020-04-24 12:44:54
【问题描述】:

我有以下疑问:

{
   "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 服务器(不同步),如果这可以帮助...

【问题讨论】:

  • 我的猜测是,排序中的第一个元素必须在选择器中并使用使用索引的运算符(正则表达式不使用)
  • @AlexisCôté 我试图将类型放在选择器的首位,但仍然收到警告。我还在排序中添加了类型,也没有帮助......

标签: couchdb pouchdb couchdb-mango


【解决方案1】:

您之前创建过索引吗?

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

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

【讨论】:

    【解决方案2】:

    附带说明,请记住,在大型数据库中,您查询的性能后果将非常严重。运算符 $in 和 $regex 将在内存中处理整个数据库,因为它不使用任何索引 - 也不能,至少在最新的 CouchDB 版本:3.0.2 中。

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

    【讨论】:

      猜你喜欢
      • 2014-03-24
      • 2019-01-14
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多