【问题标题】:Speeding up Cloudant query for type text index加速 Cloudant 查询类型文本索引
【发布时间】:2017-11-30 13:48:30
【问题描述】:

我们有一个具有这种结构的表:

 {_id:15_0, createdAt: 1/1/1, task_id:[16_0, 17_0, 18_0], table:”details”, a:b, c: d, more}

我们使用

创建索引
 { 
   "index": {}, 
   "name": "paginationQueryIndex", 
   "type": "text" 
 }

它是自动创建的

 {
  "ddoc": "_design/28e8db44a5a0862xxx",
  "name": "paginationQueryIndex",
  "type": "text",
  "def": {
    "default_analyzer": "keyword",
    "default_field": { 
    },
    "selector": {    
    },
    "fields": [      
    ],
    "index_array_lengths": true
  }
 }

我们正在使用以下查询

{ 
  "selector": { 
   "createdAt": { "$gt": 0 }, 
   "task_id": { "$in": [ "18_0" ] }, 
   "table": "details" 
  }, 
  "sort": [ { "createdAt": "desc" } ], 
  "limit”: 20 
 }

第一次需要 700-800 毫秒,之后会减少到 500-600 毫秒

为什么第一次需要更长的时间?

有什么方法可以加快查询速度?

如果类型是“文本”,有什么方法可以为特定字段添加索引? (而不是索引这些记录中的所有字段)

【问题讨论】:

    标签: couchdb cloudant


    【解决方案1】:

    您可以尝试更明确地创建索引,定义您希望索引的每个字段的类型,例如:

    {
      "index": {
        "fields": [
          {
            "name": "createdAt",
            "type": "string"
          },
          {
            "name": "task_id",
            "type": "string"
          },
          {
            "name": "table",
            "type": "string"
          }
        ]
      },
      "name": "myindex",
      "type": "text"
    }
    

    那么你的查询变成:

    { 
      "selector": { 
        "createdAt": { "$gt": "1970/01/01" }, 
        "task_id": { "$in": [ "18_0" ] }, 
        "table": "details" 
      }, 
      "sort": [ { "createdAt": "desc" } ], 
      "limit": 20 
    }
    

    请注意,我使用了数据类型为字符串的字符串。

    如果您对性能感兴趣,请尝试从查询中一次删除一个子句,看看是否有一个子句导致了性能问题。您还可以查看查询的explanation,看看它是否正确使用了您的索引。

    创建显式文本查询索引的文档是here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      • 2011-04-24
      • 1970-01-01
      • 2010-12-07
      • 2015-08-31
      相关资源
      最近更新 更多