Should I use redundancy or a simple query on a lar

2019-07-08 10:12发布

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
    ...

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

usersCollection
    user1
    user2
    user3
    ...

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")

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?

1条回答
女痞
2楼-- · 2019-07-08 10:47

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. 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.

  2. Realtime Database does not scale automatically.

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.

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);
查看更多
登录 后发表回答