是否可以每次请求多次查询MongoDB? [英] Is it OK to query a MongoDB multiple times per request?

查看:856
本文介绍了是否可以每次请求多次查询MongoDB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自RDBMS背景,我总是在尽可能努力地使用一个查询,假设它的高效,这意味着它是昂贵的每一个请求你对数据库。当涉及到MongoDB时,看起来这是不可能的,因为你不能连接表。



我知道它不应该是关系,还是推它的目的,如博客,论坛和事情,我会发现一个RDBMS更容易接近。



有一些挂起我试图了解MongoDB或NoSQL的效率。如果我想得到所有的帖子与某些用户相关(如果他们被分组)...使用MySQL我可能做一些连接,并得到它。



在MongoDB中,假设我需要的集合是单独的,使用大的$是有效的:['user1','user2','user3','user4',...]



这种方法在一段时间后会变慢吗?如果我包括1000个用户?
如果我需要得到与用户X,Y,Z相关的帖子的列表,它将使用MongoDB做到效率和/或快速:




  • 获取用户数组

  • 获取用户数组中的帖子



< 2查询一个请求。这是在NoSQL的坏习惯吗?

解决方案

b

我对以下情况进行了一些性能测试:



收藏集中有〜2,400万个文档
$
查找1基于一个键的百万个文档(已索引)

使用.NET的CSharp驱动程序



结果:

一次查询1,单线程:109sogle
一次查询1个,多线程:48sought
查询100K一次使用$ in,单线程= 20sholar
一次使用$ in,multi threaded = 9sought查询100K



因此,使用大型$ in(限制为最大查询大小)时,效果明显更好。



更新:
(查询多线程):



一次查询10次(100000次批次)= 8.8s

一次查询100(10000批)= 4.32s

一次查询1000(1000批)= 4.31s

一次查询10000(100批) = 8.4s

一次查询100000(10批)= 9s(根据上面的原始结果)



用于批量处理$ in子句中的值的数量与循环次数的比值


Coming from an RDBMS background, I was always under the impression "Try as hard as you can to use one query, assuming it's efficient," meaning that it's costly for every request you make to the database. When it comes to MongoDB, it seems like this might not be possible because you can't join tables.

I understand that it's not supposed to be relational, but they're also pushing it for purposes like blogs, forums, and things I'd find an RDBMS easier to approach with.

There are some hang ups I've had trying to understand the efficiency of MongoDB or NoSQL in general. If I wanted to get all "posts" related to certain users (as if they were grouped)... using MySQL I'd probably do some joins and get it with that.

In MongoDB, assuming I need the collections separate, would it be efficient to use a large $in: ['user1', 'user2', 'user3', 'user4', ...] ?

Does that method get slow after a while? If I include 1000 users? And if I needed to get that list of posts related to users X,Y,Z, would it be efficient and/or fast using MongoDB to do:

  • Get users array
  • Get Posts IN users array

2 queries for one request. Is that bad practice in NoSQL?

解决方案

To answer the Q about $in....

I did some performance tests with the following scenario:

~24 million docs in a collection
Lookup 1 million of those documents based on a key (indexed)
Using CSharp driver from .NET

Results:
Querying 1 at a time, single threaded : 109s
Querying 1 at a time, multi threaded : 48s
Querying 100K at a time using $in, single threaded=20s
Querying 100K at a time using $in, multi threaded=9s

So noticeably better performance using a large $in (restricted to max query size).

Update: Following on from comments below about how $in performs with different chunk sizes (queries multi-threaded):

Querying 10 at a time (100000 batches) = 8.8s
Querying 100 at a time (10000 batches) = 4.32s
Querying 1000 at a time (1000 batches) = 4.31s
Querying 10000 at a time (100 batches) = 8.4s
Querying 100000 at a time (10 batches) = 9s (per original results above)

So there does look to be a sweet-spot for how many values to batch up in to an $in clause vs. the number of round trips

这篇关于是否可以每次请求多次查询MongoDB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆