n1ql主扫描性能

时间:2018-11-26 04:06:21

标签: 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 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(数字)和日期组成。例如频道: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 

3 个答案:

答案 0 :(得分:2)

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

通过讨论如何为分组依据创建索引来查看本文: https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/groupby-aggregate-performance.html

答案 1 :(得分:2)

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 ;

也可以结帐https://blog.couchbase.com/understanding-index-grouping-aggregation-couchbase-n1ql-query/

答案 2 :(得分:1)

我认为问题出在索引扫描上,因为您怀疑问题的标题。您可以尝试几个选项来提高查询性能。

  1. 过滤器[如'channels:%:2015-05-%']将强制扫描 整个索引以生成组,然后为 聚合。这可能是花费大部分时间的地方,因此解决此问题是关键。您是否可以重新设计文档密钥 以提高选择性,即通过放置“日期”部分 在“用户ID”之前?如果可以的话,它应该运行得更快 将其更改为[例如'channels:2016-05-%']
  2. 如果您使用的是Couchbase v6.0,则可以在设置中启用Couchbase Analytics服务。 https://docs.couchbase.com/server/6.0/analytics/primer-beer.html。 Couchbase Analytics也使用 SQL ++ 。用于Couchbase Analytics的N1QL。 这意味着您可以使用相同的查询,并将其指向Google Analytics(分析) 服务。它旨在帮助需要访问 大量文档,利用并行处理 算法。

选项1是解决此问题的最有效方法,并且 SQL ++ Couchbase Analytics 应该可以为您带来重大改进,而无需进行任何更改。