title: Firebase Realtime Database Pricing with Query
body: <p>I have a question about the firebase database pricing. I have about 400,000 rows in the leaderboard of my database, but in my app I just want to load the last 500 rows, so my question will I get charged for the 500 rows loaded when I run the query or will I get charged for all 400,000 rows. </p>
  
  <p>Realtime database charges 5$ per gb stored and 1$ per gb downloaded. I did the calculation with Firestore and found that Realtime Database would be way cheaper if i get charged for the 500 rows and not the 400,000 rows. </p>
  
  <p>I searched all documentation and have not found anything about queries: <a href="https://firebase.google.com/pricing" rel="noreferrer">https://firebase.google.com/pricing</a>
  <a href="https://firebase.google.com/docs/database/usage/billing" rel="noreferrer">https://firebase.google.com/docs/database/usage/billing</a></p>
  
  <p>Can someone tell me if I get charged for just the 500 rows in my collection or for all the data in the collection and if there is a way to only get charged for the 500 rows maybe with security rules?</p>
  
  <p>Here is my query code:</p>
  
  <pre><code>let queryRef = ref.child("Leaderboard").queryOrdered(byChild: "totalStars").queryLimited(toLast: 500)
  </code></pre>
  
  <p>How the database looks like. (It will have about 500,000 childs same as these and be loaded 200,000 per day, But I just want to be priced on the top 500 that I load and not the whole 500,000 each time a user loads the leaderboard is it possible?)</p>
  
  <p><a href="https://i.stack.imgur.com/9pQJi.png" rel="noreferrer"><img src="https://i.stack.imgur.com/9pQJi.png" alt="enter image description here"></a></p>