firebase database: index your data
2018-03-03 07:15
399 查看
Index Your Data
Firebase allows you to do ad-hoc queries on your data using an arbitrary child key. If you know in advance what your indexes will be, you can define them via the.indexOnrule in your Firebase Realtime Database Rules to improve query performance.
Defining Data Indexes
Firebase provides powerful tools for ordering and querying your data. Specifically, Firebase allows you to do ad-hoc queries on a collection of nodes using any common child key. As your app grows, the performance of this query degrades. However, if you tell Firebase about the keys you will be querying, Firebase will index those keys at the servers, improving the performance of your queries.A node's key is indexed automatically, so there is no need to index it explicitly.Indexing with orderByChild
The easiest way to explain this is through an example. All of us at Firebase agree that dinosaurs are pretty cool. Here's a snippet from a sample database of dinosaur facts. We will use it to explain how.indexOnworks with
orderByChild().{
"lambeosaurus": {
"height" : 2.1,
"length" : 12.5,
"weight": 5000
},
"stegosaurus": {
"height" : 4,
"length" : 9,
"weight" : 2500
}
}Let's imagine that in our app, we often need to order the dinosaurs by name, height, and length, but never by weight. We can improve the performance of our queries by telling Firebase this information. Since the name of the dinosaurs are just the keys, Firebase already optimizes for queries by dinosaur name, since this is the key of the record. We can use
.indexOnto tell Firebase to optimize queries for height and length as well:{
"rules": {
"dinosaurs": {
".indexOn": ["height", "length"]
}
}
}Like other rules, you can specify an
.indexOnrule at any level in your rules. We placed it at the root level for the example above because all the dinosaur data is stored at the root of the database.
Indexing with orderByValue
In this example, we'll demonstrate how.indexOnworks with
orderByValue(). Let's say we're making a leaderboard of dino sports scores with the following data:{
"scores": {
"bruhathkayosaurus" : 55,
"lambeosaurus" : 21,
"linhenykus" : 80,
"pterodactyl" : 93,
"stegosaurus" : 5,
"triceratops" : 22
}
}Since we're using orderByValue() to create the leaderboard, we can optimize our queries by adding a
.valuerule at our
/scoresnode:{
"rules": {
"scores": {
".indexOn": ".value"
}
}
}Indexes are not required for development:Indexes are not required for development unless you are using the REST API. The realtime client libraries can execute ad-hoc queries without specifying indexes. Performance will degrade as the data you query grows, so it is important to add indexes before you launch your app if you anticipate querying a large set of data.
相关文章推荐
- firebase database: secure your data
- best practice of data structure of firebase database
- 错误: Could not edit the data in the folder or database selected. The current version does not support editing (base, consistent, or closed)
- firebase database - work with lists
- firebase database - offline access
- firestore vs firebase database
- 75.Your test database is configured to run in NOARCHIVELOG mode. One of the data files in the USERS
- understanding firebase database billing
- SQL SERVER – Fix : Error Msg 1813, Level 16, State 2, Line 1 Could not open new database ‘yourdatabasename’. CREATE DATA
- [React Native + Firebase] React Native: Real time database with Firebase -- setup & CRUD
- Using the data layer API to query your Kentico database
- firebase database limit
- 94.You plan to move data from a flat file to a table in your database. You decide to use SQL*Loader
- Android 集成FireBase Realtime DataBase实现聊天
- understand firebase realtime database rules
- 拥抱Firebase,Firebase之Realtime Database。(含github源码),欢迎关注。
- [Firebase] 4. Firebase Object related Database
- Firebase Database setup
- 143.You execute a command to resize a data file, sales.dbf, of size 200 MB in your database: ALTER D
- firebase database: user based security