使用 whereArrayContains 进行搜索的效率 [英] Efficiency of searching using whereArrayContains

查看:11
本文介绍了使用 whereArrayContains 进行搜索的效率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇使用此代码在集合中搜索文档的效率.随着集合中文档数量的增加和数组中项目数量的增加,这种搜索是否会变得非常低效?有没有更好的方法来做到这一点,或者我可以对数据库进行架构更改以更好地优化它吗?有没有什么地方可以找到 Firestore 文档的这些函数的时间复杂度?

I am curious as to the efficiency of searching for documents in a collection using this code. As the number of documents in the collection grows and the number of items in the array grows will this search become very inefficient? Is there a better way of doing this or is there a schema change I can make to the database to better optimize this? Is there somewhere I can find the time complexity of these functions for the firestore documentation maybe?

Query query = db.collection("groups").whereArrayContains("members", userid);


我最初想尝试将组 ID 存储在用户下,以便仅获取当前用户的组,但遇到了问题,并且从未找到使用多个 ID 来查询设置 FireStoreRecyclerOptions 的解决方案.

I originally wanted to try storing the group ids under the user so as to only grab the groups for that current user, but ran into issues and never found a solution for setting a FireStoreRecyclerOptions using multiple ids to query by.

示例:

for(String groupid : list) {
    Query query = db.collection("test-groups").document(groupid);

    FirestoreRecyclerOptions<GroupResponse> response = new FirestoreRecyclerOptions.Builder<GroupResponse>()
            .setQuery(query, GroupResponse.class)
            .build();
}

有没有办法向 FirestoreRecyclerOptions 添加多个查询?

Is there a way to add multiple queries to the FirestoreRecyclerOptions?

推荐答案

随着集合中文档数量的增加和数组中项目数量的增加,这种搜索是否会变得非常低效?

As the number of documents in the collection grows and the number of items in the array grows will this search become very inefficient?

问题不在于搜索会变得非常低效,问题在于文档有限制.因此,当涉及到可以放入文档的数据量时,存在一些限制.根据关于使用和限制的官方文档:

The problem isn't the fact that the search will become very inefficient, the problem is that the documents have limits. So there are some limits when it comes to how much data you can put into a document. According to the official documentation regarding usage and limits:

文档的最大大小:1 MiB(1,048,576 字节)

Maximum size for a document: 1 MiB (1,048,576 bytes)

如您所见,单个文档中的数据总量限制为 1 MiB.当我们谈论存储文本时,您可以存储很多内容,但随着数组变大,请注意此限制.

As you can see, you are limited to 1 MiB total of data in a single document. When we are talking about storing text, you can store pretty much but as your array gets bigger, be careful about this limitation.

如果您在数组中存储大量数据并且这些数组应该由很多用户更新,那么您需要注意另一个限制.因此,您在每个文档上每秒只能写入 1 次.因此,如果您遇到许多用户都试图同时将数据写入/更新到相同文档的情况,您可能会开始看到其中一些写入失败.所以,也要注意这个限制.

If you are storing a large amount of data in arrays and those arrays should be updated by lots of users, there is another limitation that you need to take care of. So you are limited to 1 write per second on every document. So if you have a situation in which a lot of users al all trying to write/update data to the same documents all at once, you might start to see some of these writes to fail. So, be careful about this limitation too.

您可能已经注意到,Cloud Firestore 中的查询速度非常快,这是因为 Firestore 会自动为您文档中的任何字段创建索引.

As you probably noticed, queries in Cloud Firestore are very fast and this is because Firestore automatically creates an index for any fields you have in your document.

如果您认为将根据父对象包含集合的特定成员来查询父对象,请使用映射而不是数组.

If you think that you'll be querying for a parent based on their containing a specific member of a collection, then use maps and not arrays.

有很多帖子说数组在 Cloud Firestore 上不能很好地工作,因为当您的数据可以被多个客户端更改时,很容易混淆,因为您不知道发生了什么以及在哪个字段上.如果我正在使用地图并且用户想要编辑多个不同的字段,即使是完全相同的字段,我们通常都知道发生了什么.在数组中,情况有所不同.试着想想如果用户想要编辑索引 0 处的值会发生什么,其他一些用户想要删除索引 0 处的值,你最终会得到非常不同的结果,为什么不,数组越界异常.所以带数组的 Firestore 操作有点不同.因此,您无法在特定索引处执行诸如插入、更新或删除之类的操作.但是如果不关心将元素存储到数组中的确切顺序,那么您应该使用数组.Firestore 几天前添加了一些功能来添加或删除特定元素,但前提是不关心它们的确切位置.请参阅此处官方文档.

There many posts out there that say that arrays don't work well on Cloud Firestore because when you have data that can be altered by multiple clients, it's very easy to get confused because you cannot know what is happening and on which field. If I'm using a map and users want to edit several different fields, even the exact same field, we generally know what is happening. In arrays, things are different. Try to think what might happen if a user wants to edit a value at index 0, some other user wants to delete the value at index 0 you'll end up having a very different results and why not, array out of bounds exceptions. So Firestore actions with arrays are a little bit different. So you cannot perform actions like, insert, update or delete at a specific index. But if don't care about the exact order that you store element into an array, then you should use arrays. Firestore added a few days ago some features to add or remove specific elements but only if don't care about their exact position. See here official documentation.

总而言之,只有在需要将数据一起显示时才将数据放在同一个文档中.另外,不要把它们弄得这么大,这样你就需要下载比实际需要更多的数据.当您想要搜索该数据的各个字段或希望您的数据有增长空间时,将数据放入集合中.如果您想根据该数据搜索您的父对象,请将您的数据保留为地图字段.如果您有通常用作标志的项目,请继续使用数组.

In conclusion, put data in the same document only if you need it to display it together. Also, don't make them so big so you'll need to download more data than you actually need. To put data in a collection when you want to search for individual fields of that data or if you want your data to have room to grow. Leave your data as a map field if you want to search your parent object based on that data. And if you got items that you generally use as flags, go ahead with arrays.

另外,不要担心 Firestore 中的慢查询.

这篇关于使用 whereArrayContains 进行搜索的效率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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