baltiturg
baltiturg

Reputation: 366

Why PostgreSQL execution time is fast but respond is slow?

Using Google Cloud PostgreSQL the execution time of a simple query is 3ms on the Google Console, but when I send the request from my Python on my Mac, it takes 4 seconds to get the respond and print it.

I do create a connection every time I run the script :

engine = sqlalchemy.create_engine('postgresql://' + username + ':' + password + '@' + host + ':' + port + '/' + database)

and then I send query with pandas:

df = pd.read_sql_query(e, engine)

Is this something that slow down the round trip? Should I not create a connection every time? How can I get a much faster respond ?

3ms for the query grows to 4 seconds getting the final respond - is this going to improve if I run it as a web client sending a normal REST API request ?

Upvotes: 0

Views: 439

Answers (1)

llompalles
llompalles

Reputation: 3176

I recommend you to test the performance inside the code. This way you could measure the time it takes it line of your code and decide if this is because your environment ( for example the building time of your script) or it is taking more time due to the response from the server side.

To to that you can use the time Python library. An example on how to do this could be:

import time

start_time = time.time()

#
# Your code stuff
#

elapsed_time = time.time() - start_time
print('It took {} seconds to run your code'.format(elapsed_time))

Upvotes: 1

Related Questions