我的文档如下
{
"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
答案 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是解决此问题的最有效方法,并且 SQL ++ Couchbase Analytics 应该可以为您带来重大改进,而无需进行任何更改。