加快MongoDB查询响应| Discord Bot
我认为这不是一个问题,但是随着我使用的数据库继续增长,我注意到我程序的响应越来越慢(这很有意义,这是有意义的,更多的东西可以返回) 。
现在我的问题是我如何能够加快响应的表现,因为截至目前,要做以下几点需要花费很多时间: 我想对查询搜索进行排序并返回前10个结果,目前,我的代码看起来像以下
alldocuments = self.bot.db.nameofdb.find()
alldocuments = list(map(lambda x: x["userId"], alldocuments ))
_documentCount = [(i, alldocuments.count(i)) for i in set(alldocuments)]
_documentCount.sort(key = lambda x: x[1], reverse=True)
docCount = list(filter(lambda x: ctx.guild.get_member(int(x[0])), _documentCount))
i = 0
while i < 10:
if not ctx.guild.get_member(int(docCount[i][0])):
i+=1
continue
print(docCount[i][1]})
i += 1
内容
:数据库由数千个条目组成:
Entry Ex. 1:
{
_id: 62338b6994b3773415bd7efc
uid: "849551661"
gacha_type: 100
item_id: ""
count: "1"
time: "2021-12-25 17:42:58"
name: "Magic Guide"
lang: "en-us"
item_type: "Weapon"
rank_type: "3"
id: "1640423160000228061"
userId: 738362958253522976
}
Entry Ex. 2
{
_id: 62338b6994b3773415bd7efe
uid: "849551661"
gacha_type: 100
item_id: ""
count: "1"
time: "2021-12-25 17:42:58"
name: "Noelle"
lang: "en-us"
item_type: "Character"
rank_type: "4"
id: "1640423160000227861"
userId: 738362958253522976
}
现在我的数据库目前存在,其中超过100K的条目,我想按用户ID对其进行对,以“用户ID”的条目最多的顺序进行排序。
示例结果应该是按照以下方式的方式:
1. 25k - userId
2. 15k - userId
3. 10k - userId
4. 8k - userId
5. 5k - userId
6. 1k - userId
7. 200 - userId
8. 100 - userId
9. 50 - userId
10. 25 - userId
在其中只有25岁以下条目的任何人都会被忽略。
I didn't think this would be an issue, but as the database I am using continues to grow, I've noticed that the responses from my program are getting slower and slower (which makes sense, more stuff to look through to return).
Now my question is how would I be able to speed up the performance of my response because it takes quite some time to do the following as of right now:
I want to sort and return to the top 10 results from a query search, currently, my code looks something along the lines of:
alldocuments = self.bot.db.nameofdb.find()
alldocuments = list(map(lambda x: x["userId"], alldocuments ))
_documentCount = [(i, alldocuments.count(i)) for i in set(alldocuments)]
_documentCount.sort(key = lambda x: x[1], reverse=True)
docCount = list(filter(lambda x: ctx.guild.get_member(int(x[0])), _documentCount))
i = 0
while i < 10:
if not ctx.guild.get_member(int(docCount[i][0])):
i+=1
continue
print(docCount[i][1]})
i += 1
Some information:
Database consists of thousands of entries as follow:
Entry Ex. 1:
{
_id: 62338b6994b3773415bd7efc
uid: "849551661"
gacha_type: 100
item_id: ""
count: "1"
time: "2021-12-25 17:42:58"
name: "Magic Guide"
lang: "en-us"
item_type: "Weapon"
rank_type: "3"
id: "1640423160000228061"
userId: 738362958253522976
}
Entry Ex. 2
{
_id: 62338b6994b3773415bd7efe
uid: "849551661"
gacha_type: 100
item_id: ""
count: "1"
time: "2021-12-25 17:42:58"
name: "Noelle"
lang: "en-us"
item_type: "Character"
rank_type: "4"
id: "1640423160000227861"
userId: 738362958253522976
}
Now my database currently exists with over 100k of these entries, and I want to sort them by userId in the order of which "userId" has the greatest amount of entries.
A sample result should be something along the lines of:
1. 25k - userId
2. 15k - userId
3. 10k - userId
4. 8k - userId
5. 5k - userId
6. 1k - userId
7. 200 - userId
8. 100 - userId
9. 50 - userId
10. 25 - userId
where anyone with under 25 entries would just be ignored.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有适当的信息,给出答案有点困难(即可能还有2-3个样本输入文档和样本结果)。但是,可能是这个:
It's a bit difficult to give an answer without proper information (i.e. maybe 2-3 more sample input documents and sample result). However, could be this one:
使用MongoDB,您可以直接包含您在查询中应用的那些过滤器。这应该通过加载较少的数据来影响您的运行时。
Using MongoDB, you're able to directly include those filters that you are applying into your query. This should impact your runtime by loading fewer data.