Couchbase n1ql主扫描性能

Couchbase n1ql主扫描性能,couchbase,n1ql,Couchbase,N1ql,我的文件如下: { "date": "1970-02-19", “uid”: 2345 “profile": [ "Profile Text, Profile Text, Profile Text, Profile Text, Profile Text", "Profile Text, Profile Text, Profile Text, Profile Text, Profile Text", "Profile Text, Profile Tex

我的文件如下:

{
  "date": "1970-02-19",
  “uid”: 2345
  “profile": [
    "Profile Text, Profile Text, Profile Text, Profile Text, Profile Text",
    "Profile Text,  Profile Text,  Profile Text,  Profile Text,  Profile Text",
    "Profile Text,  Profile Text,  Profile Text,  Profile Text,  Profile Text"
  ],
  “channel_a”: {
    "reach": 915157,
     "likes": 6.39,
     "shares": 8.15,
     "followergrowth": 6.89
  },
  “channel_b”: {
    "reach": 894888,
    "response": 8.64,
    "influence": 7.03,
    "reject": 5.09
  },
  “channel_c” {
    "reach": 396938
  }
}
文档密钥由文档类型、用户id(数字)和日期组成。e、 g.频道:9999:2015-12-31。我想创建一个查询,返回给定日历月内特定频道参与率最高的前10名用户列表。标准可能因渠道和要求而异

上面的查询计划使用主索引扫描,几乎需要一分钟才能完成。合格的数据集大约有1.3K行,但这可能会增加到5K行。Couchbase上是否有任何机制可供我使用,以获得更好的性能?此外,随着数据量的增长,我正在寻找一种可扩展的解决方案

select 
    s. uid,
    sum(s.channel_c.reach) channel_c_Reach,
    sum(s.channel_b.reach) channel_b_Reach,
    sum(s.channel_a.likes) channel_a_Likes
FROM channels s
where meta().id like ‘channels:%:2016-05-%’
group by s.uid
ORDER BY sum(s.channel_a.likes) DESC
LIMIT 10 

如果您的查询使用的是主索引,那么有很多地方可以改进。关键是创建几个索引来支持您的查询

查看这篇文章,讨论如何为group BY创建索引:


另外,我认为问题在于索引扫描,正如您在问题标题中所猜测的那样。您可以尝试几个选项来提高查询性能。

  • 过滤器[如'channels:%:2015-05-%']将强制扫描 整个索引生成组,然后获取组的数据 聚合。这可能是花费大部分时间的地方,所以解决这个问题是关键。您有重新设计文档密钥的选项吗 增加选择性,即通过添加“日期”组件 在“用户id”之前?如果可以的话,它应该运行得更快 将其更改为[如“频道:2016-05-%”
  • 如果您使用的是Couchbase v6.0,则可以在设置中启用Couchbase Analytics服务。  . Couchbase Analytics使用SQL++,又名。N1QL用于Couchbase分析。 这意味着您可以使用相同的查询,并将其指向分析 服务它旨在帮助处理需要访问数据库的查询 大量文档,利用并行处理 算法
  • 选项1将是解决此问题的最有效方法,SQL++Couchbase Analytics将在不做任何更改的情况下为您带来显著的改进

    CREATE INDEX ix1 ON channels(uid, date, channel_a.likes, channel_c.reach, channel_b.reach)
    WHERE meta().id like "channels:%";
    SELECT
        s.uid,
        sum(s.channel_c.reach) channel_c_Reach,
        sum(s.channel_b.reach) channel_b_Reach,
        sum(s.channel_a.likes) channel_a_Likes
    FROM channels s
    WHERE meta(s).id like "channels:%" AND s.uid IS NOT NULL AND s.date LIKE "2016-05-%"
    group by s.uid
    ORDER BY sum(s.channel_a.likes) DESC
    LIMIT 10 ;