我是否应该在具有Firebase Cloud Firestore数据库的大型数据集上使用冗余或简单查询? [英] Should I use redundancy or a simple query on a large dataset with Firebase Cloud Firestore database?

查看:39
本文介绍了我是否应该在具有Firebase Cloud Firestore数据库的大型数据集上使用冗余或简单查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个collectionsItemCollection,其中包含大量的小itemDocs.每个itemDoc都有一个子集合,统计信息.每个itemDoc还具有一个所有者"字段,该字段指示哪个用户拥有itemDoc.

I have a collection, itemsCollection, which contains a very large amount of small itemDocs. Each itemDoc has a subcollection, statistics. Each itemDoc also has a field "owner" which indicates which user owns the itemDoc.

itemsCollection
    itemDoc1
        statistics
    itemDoc2
        statistics
    itemDoc3
        statistics
    itemDoc4
        statistics
    ...

我还有一个收藏集usersCollection,其中包含基本的用户信息.

I also have a collection, usersCollection, which contains basic user info.

usersCollection
    user1
    user2
    user3
    ...

由于每个itemDoc都属于一个特定用户,因此有必要向每个用户显示他们拥有哪些itemDocs.我一直在使用查询:

Since each itemDoc belongs to a specific user, it's necessary to display to each user which itemDocs they own. I have been using the query:

db.collection("itemsCollection").where("owner", "==", "user1")

我想知道这是否可以有效扩展,即每当itemsCollection成为数百万条记录时?如果不是,最好的解决方案是将每个itemDoc及其统计信息子集合复制为用户文档中的子集合,还是我应该做其他事情?

I am wondering if this will scale effectively, i.e. whenever itemsCollection gets to be millions of records? If not, is the best solution to duplicate each itemDoc and its statistics subcollection as a subcollection in the user document, or should I be doing something else?

推荐答案

正如Firebase产品经理Alex Dufter在 Firebase Dev Summit 2017 上的一天中所说,Firestore的灵感来自许多多年以来在Firebase实时数据库上的反馈方式.他们面临两种类型的问题:

As Alex Dufter, the product manager from Firebase, explained in one of days at Firebase Dev Summit 2017 that Firestore was inspired in many ways by the feed-back that they had on the Firebase Realtime Database over the years. They faced two types of issues:

  1. 数据建模和查询.Firebase Realtime Database无法查询多个属性,因为它通常涉及重复数据或客户端过滤,我们都已经知道这有些混乱.

  1. Data modelling and querying. Firebase Realtime Database cannot query over multiple properties because it ussaly involves duplication data or cliend-side filtering, which we all already know that is some kind of messy.

实时数据库不会自动扩展.

Realtime Database does not scale automatically.

有了这个新产品,他们说您现在可以构建一个应用程序并将其扩展到全球范围,而无需更改任何代码行.Cloud Firestore还是专门为移动和Web应用程序开发而构建的 NoSQL数据库.可以灵活地构建各种应用程序,并可以进行扩展以扩展到任何大小.

With this new product, they say that you can now build an app and grow it to planetary scale without changing a single line of code. Cloud Firestore is also a NoSQL database that was build specifically for mobile and web app development. It's flexible to build all kinds of apps and scalable to grow to any size.

因此,由于新数据库是在已知的基础上构建的,因此不再需要 重复数据 .因此,即使您的数据将增长到数百万条记录,您也不必担心使用该行代码,它 将自动缩放 .但是,您需要记住一件事,如果要使用多个条件,请不要忘记通过将索引添加到Firebase控制台中来使用索引.这是官方文档中的两个简单示例:

So because the new database was build knowing this iusses, duplication data is not nedeed anymore. So you will not have to worry about using that line of code, even if your data will grow to millions of records, it will scale automatically. But one thing you need to remember, if you will use multiple conditions, don't forget to use the indexes by simply adding them in the Firebase console. Here are two simple examples from the offical documentation:

citiesRef.whereEqualTo("state", "CO").whereEqualTo("name", "Denver");
citiesRef.whereEqualTo("state", "CA").whereLessThan("population", 1000000);

这篇关于我是否应该在具有Firebase Cloud Firestore数据库的大型数据集上使用冗余或简单查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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