NoSQL database design for queries with multiple re

2019-01-29 13:02发布

I need best practice tips to enhance the performance of my noSQL database (hosted on Firebase). Moreover, I need tips on how to structure the nodes.

The database stores product information, with three main properties:

$productId          
     /date           
     /category           
     /subcategory

On my website, I have three views:

  • retrieve the last 4 products (orderBy date)
  • retrieve last 4 products (by date) of category X
  • retrieve last 4 products (by date) of category X and subcategory Y.

Note that I also have a node product_images, which have subnodes matching the productIDs. So constructing the databas as follows:

$categoryId
        $subCategoryId 
               $productId

will not work as I need to know beforehand the $categoryId and $subCatrgoryId before I can match it with $productId. It would also make it difficult to retrieve the last 4 products.

How would I construct my noSQL database in an efficient way, and how would I retrieve the products with Firebase filtering it out with multiple restrictions?

I know that in Firebase you can use orderByChild and equalTo, but this only works on one restriction, whereas I have to deal with one to three.

1条回答
萌系小妹纸
2楼-- · 2019-01-29 13:46

Since Firebase only can filter on one property, you'll have to combine the values you want to filter on into a single property. Since you have multiple filtering needs, you might need such a property for each filtering use-case.

It seems that this will be possible for you if you combine <category>_<date> into a single property, which we'll call multi-prop. We'll also combine <category>_<subcategory>_<date> into a property called megaprop:

$productId          
    /date           
    /category           
    /subcategory
    /multiprop
    /megaprop

Some sample data:

id_cc_1234
    date: 20151031
    category: candy
    subcategory: halloween
    multiprop: candy_20151031
    megaprop: candy_halloween_20151031
id_tg_2345
    date: 20151125
    category: candy
    subcategory: thanksgiving
    multiprop: candy_20151125
    megaprop: candy_thanksgiving_20151125
id_tg_3456
    date: 20151125
    category: food
    subcategory: thanksgiving
    multiprop: food_20151125
    megaprop: food_thanksgiving_20151125
id_sk_4567
    date: 20151205
    category: candy
    subcategory: sinterklaas
    multiprop: candy_20151205
    megaprop: candy_sinterklaas_20151205
id_sc_5678
    date: 20151225
    category: candy
    subcategory: christmas
    multiprop: candy_20151225
    megaprop: candy_christmas_20151225

Now your queries turn into:

  1. retrieve the last 4 products

    ref.orderByChild('date').limitToLast(4);
    
  2. retrieve last 4 products of category X

    ref.orderByChild('multiprop').startAt('candy').endAt('candy_~').limitToLast(4);
    
  3. retrieve last 4 products of category X and subcategory Y.

    ref.orderByChild('megaprop').startAt('candy_christmas').endAt('candy_christmas_~').limitToLast(4);
    

Also see: http://jsbin.com/piluzo/edit?js,console for a working version.

查看更多
登录 后发表回答