MichaelSolati / geofirestore-js

Location-based querying and filtering using Firebase Firestore.
https://geofirestore.com
MIT License
504 stars 58 forks source link

Geofirestore + Firestore Eating Up Reads (1,200 Reads for ~15 Documents & 1,200 Reads for ~45 Documents) #110

Closed jefelewis closed 4 years ago

jefelewis commented 5 years ago

I recently got everything working with Geofirestore and my Cloud Firestore database, but there seems to be an issue with Geofirestore eating up Cloud Firestore database reads.

I did a location query last night with a radius of 1 and that returned about ~10 documents, but resulted in 1,200 database reads.

This morning I did a query with a radius of 2, which resulted in about ~45 documents, but ate up 600 reads.

Cloud Firestore Format: This is my first time using Geofirestore, but here's what my Cloud Firestore looks like (I used the Geohash as the Ids when I loaded them. Not sure if that was an issue)

Screen Shot 2019-05-31 at 9 00 16 AM

Database Reads:

Screen Shot 2019-05-31 at 9 00 35 AM

Update: Cloud Firestore now shows 2,400 reads, so 1,200 per query? (Only 2 queries ever made for this database)

Screen Shot 2019-05-31 at 9 22 13 AM

My Query Function:

  // Get Nearest Locations
  getNearestLocations = () => {
    try {
      // Current Location: Latitude + Longitude
      let currentLatitude = Number(this.state.location.coords.latitude);
      let currentLongitude = Number(this.state.location.coords.longitude);

      // GeoFirestore: Reference (Importing Database from App)
      // const firestore = firebase.firestore();

      // Create a GeoFirestore reference
      const geofirestore = new GeoFirestore(database);

      // Create a GeoCollection reference
      const geocollection = geofirestore.collection('locations')

      // Create a GeoQuery based on a location
      const query = geocollection.near({
        center: new firebase.firestore.GeoPoint(currentLatitude, currentLongitude),
        radius: 2,
      });

      // Get query (as Promise)
      query.get().then((value) => {
        console.log(value.docs); // All docs returned by GeoQuery

        this.setState({
          nearbyLocations: value.docs,
        })
      });
    }
    catch (error) {
      console.log(error);
    }
  };
MichaelSolati commented 5 years ago

Interesting... Could you build a quick sample app based on how you are using it? (I'll provide my own firebase app to test it, I just want to dig through everything to understand how your app works and why there are so many reads)

jefelewis commented 5 years ago

Hey @MichaelSolati, I created this repository to reproduce the problem. https://github.com/jefelewis/geofirestore-test

This may require the expo-cli to be installed as a global package as well as the Expo android/iphone app.

  1. To start, enter the Firebase API info in the config folder.
  2. Run expo start to being the project.
  3. Expo browser window should automatically pop up. Click on Tunnel Screen Shot 2019-05-31 at 10 48 30 AM

Let me know if you need any more information. Thank you!

pushkine commented 5 years ago

it counts a query per document each time you open the console url in your browser, paginated at 600 so if it defaults to a collection with 1k documents, you'll get +600 reads every time you open the database tab in the console

lsps9150414 commented 4 years ago

it counts a query per document each time you open the console url in your browser, paginated at 600 so if it defaults to a collection with 1k documents, you'll get +600 reads every time you open the database tab in the console

Hi @pushkine , by console do you mean the Firebase web console?

MichaelSolati commented 4 years ago

Web console counts as a read as well (from my experience). Also this Stack Overflow response seems to confirm that.