0

I am reading the docs and I wonder if I should add .indexOn in the JSON tree under Rules in RealTime Database in order to improve the performance of the queries for child TimeStampBookingDate The data will be read by the client(mobile app) using firebase Swift library. I am a confused since in the docs it reads that:
"The realtime client libraries can execute ad-hoc queries without specifying indexes."
My Database is structured in this way

blvc-72e18
    Users
       2s8uH7sDwGbRdYwmXpGrRngmnUV2
         cus_AQVJTpFX2LdkzL
             282579602
               BookingAmount: "63"
                 TimeStampBookingDate: "1491654600"

In order to improve query performance at child key TimeStampBookingDate, should I edit the default rules in firebase as follows?

{
 "rules": {
       ".read": "auth != null",
       ".write": "auth != null"
       "Users": {
       ".indexOn":"TimeStampBookingDate"
    }
  }
}
KENdi
  • 7,576
  • 2
  • 16
  • 31
bibscy
  • 2,598
  • 4
  • 34
  • 82
  • Why wouldn't you add the .indexOn? You can certainly continue to read that node without it, however it significantly improves performance as the node count grows in size. – Jay Jun 27 '17 at 14:55
  • Definitely add the index. This answer, from a Firebase team member, explains that an index significantly reduces the size of the downloaded data: https://stackoverflow.com/a/35442993/4815718 – Bob Snyder Jun 27 '17 at 15:22
  • @Jay should I insert .indexOn above this line `".read": "auth != null",`? – bibscy Jul 02 '17 at 01:13
  • It depends. In the rules provided, any authenticated user can read/write any node in the database (which may be a tad insecure). .indexOn is generally added to direct decedents of the nodes being queryed - so if you have a users node with /users/uid_0/name, /users/uid_1/name the .indexOn would be by *name*. However, your structure will not allow a query on TimeStampBookDate because it's too deep, so indexing on that would not have any effect. You may want to denormalize your structure and take a look at [Index Your Data](https://firebase.google.com/docs/database/security/indexing-data) – Jay Jul 02 '17 at 13:30

0 Answers0