Firebase warning: Using an unspecified index

2019-04-19 06:06发布

问题:

I get this warning message from Firebase for every single orderByChild I used in my queries from JavaScript:

FIREBASE WARNING: Using an unspecified index. Consider adding ".indexOn": "" at /tables to your security rules for better performance

So far my queries ran quite fast so I think it is not necessary to include the indexOn. Just wondering what are the negative impacts?

Also, how do I actually disable the warnings as they are quite irritating when I am debugging.

回答1:

If you are on development phase, you won't feel the difference. But as your data grows indexing helps making your queries much faster.

Read this to know more on advantages of indexing. https://firebase.google.com/docs/database/security/indexing-data

As far as I know to disable the warnings, you have to edit firebase source code. Is there someway to disable all firebase logging to console?



回答2:

As the others have said: if you don't define an index, all filtering happens client-side. This is likely fine during initial development, but as you add more data it becomes more and more wasteful.

To fix the problem, define the correct index in your Firebase Database rules. You can modify the rules in the Database panel in your project's Firebase console. See defining data indexes in the Firebase documentation.

Is there any way for me to observe what has changed through the indexing?

  1. After you define the correct index, the warning will disappear from your JavaScript console.
  2. In addition you can watch the traffic between the Firebase client and the server in the Network tab of your browser's developer tools. Before you define the correct index, you'll see that the Firebase client downloads all data in the location (since it performs the filtering locally). After you define the correct index, you'll see that the Firebase client only downloads the data that matching the query.


回答3:

Firebase provides powerful tools for ordering and querying your data. Indexing can improve the performance of our queries. consider a table called dinosaur has some data like:

{
  "lambeosaurus": {
    "height" : 2.1,
    "length" : 12.5,
    "weight": 5000
  },
  "stegosaurus": {
    "height" : 4,
    "length" : 9,
    "weight" : 2500
  }
}

You need to (search) order the dinosaurs height, and length, but never by weight. so you need to set a rules for dinosaur table like:

"rules": {
  "dinosaurs": {
    ".indexOn": ["height", "length"]
  }
}

By telling Firebase this information, Firebase can use this .indexOn to optimize our queries for height and length. Then we maintained Firebase security for querying our data and improve the performance of our queries as well.