krt24
krt24

Reputation: 85

Firestore database reads/writes incredibly slow

OS: Mac OS Catalina v 10.15.1

Python version: Python 3.7.1

Firestore pip package version: google-cloud-firestore 1.6.1

I'm testing simple database reads and writes on a very simple Firestore database. I'm looking at a single collection with a single document containing 1 data item (a string); however, when I try to write (as shown below), the timer I have (also in the below code) shows that the first write takes over 30 seconds. This same behavior is present for reads.

However, this is only present for the first write/read in this python script. All subsequent writes/reads are timed at roughly 100ms (still slow for such a tiny database). I have not been able to find this issue replicated on the internet, and there is no mention of behavior like this in the Firestore Python SDK documentation. I am running the below code locally on my machine.

import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import datetime

cred = credentials.Certificate("./path/to/adminsdk.json")
firebase_admin.initialize_app(cred)
db = firestore.client()

start = datetime.datetime.now()

#the below write takes >30 seconds to execute
doc_ref = db.collection(u'test_collection').document(u'test_document')
doc_ref.set({u'test_field':'data'})

time_after_first = datetime.datetime.now()
print(time_after_first - start)

#The below read takes 80 ms to execute
doc_data = doc_ref.get()
print(doc_data.get('test_field'))

print(datetime.datetime.now()-time_after_first)

print("done")

I found a suggestion on another StackOverflow post suggesting to use on_snapshot instead of get; however, the problem persists (the DocumentSnapshot is only received after roughly 30 seconds). Additionally, the problem is not unique to get, but for set, update, etc. as well.

Any tips would be appreciated!

Upvotes: 1

Views: 1923

Answers (1)

vitooh
vitooh

Reputation: 4262

You should think of your connection performance. I have implemented this code on my side and here are effects. It never reached 0.5s. Seems Firestore performance is very good. I hope this will help you solve it...

enter image description here

Upvotes: 1

Related Questions