NoSQL database design for queries with multiple restrictions (Firebase)

前端 未结 1 418
隐瞒了意图╮
隐瞒了意图╮ 2020-12-22 12:32

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 sto

相关标签:
1条回答
  • 2020-12-22 12:58

    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.

    0 讨论(0)
提交回复
热议问题