Firestore NoSql数据库结构(作为n元树) [英] Firestore NoSql Database Structure (as a n-ary tree)
问题描述
我正在开发一个应用程序,它将能够过滤您搜索的用户(性别,年龄,位置等)。我想让它更快并且更便宜,所以我决定将数据存储为n元树,就像这样:
I am developing an app that will offer the possibility to filter the users you search (gender, age, location, ...). I want to make this fast and as cheaper as possible, so I have decided to store my data as a n-ary tree, just something like this:
----- Users -----
Root Node: Gender (male or female)
-male
--country (nodes: USA, UK, Spain, Germany, France, Italy, ...)
---age (nodes: 18, 19, 20, 21, 22, ..., 60)
->user (leaf nodes)
-female
--country (nodes: USA, UK, Spain, Germany, France, Italy, ...)
---age (nodes: 18, 19, 20, 21, 22, ..., 60)
->user (leaf nodes)
我认为像这样构造数据库会减少数据量,因为我不必在最后的文档中将性别,国家和年龄存储为字段...但是我不知道如果这在NoSql世界中是不好的做法,还是会对性能(速度)产生负面影响。
I think that structuring the database like this will reduce the amount of data, as I don't have to store the gender, country, and age as field in the last documents... But I don't know if this is a bad practices in the NoSql world, or if this will negativily affect the performance (velocity).
关于这是否是一个好结构的任何想法吗?还是我应该将所有这些属性存储在每个用户文档中,并使用 where查询不满意?谢谢。
Any ideas about if this is a good structure? Or should I store all this properties in every user document and use the "where" query clausure? Thank you.
推荐答案
在您的方案中,您希望过滤3个属性(性别,国家和年龄),我认为可以如果提供了所有三个属性,则可以正常工作。
In your scenario you want to filter on 3 attributes (gender, country and age) and I think it could work if all three attributes are provided.
如果缺少三个属性,则可以使用去规范化可以在多个集合上复制数据,但是既然您希望便宜,我想您不想增加存储使用量。
If one of the three is missing you could use denormalization to replicate data on multiple collections, but since you want it cheap I presume you don't want to increase on the storage usage.
另一种选择是将它们全部存储在单个用户
集合中,并利用索引。索引是代表您创建的,这就是Firestore如此之快的原因。
Another option would be to store them all inside a single user
collection and query with where
conditions taking advantage of indexing. The indexes are created on you behalf and they are the reason Firestore is so fast.
这篇关于Firestore NoSql数据库结构(作为n元树)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!