简体   繁体   中英

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 ?

How much data gets retrieved by the sql query? It can happen that your query is fast but python is spending lots of time deserializing the result back to object. Maybe try retrieving less number of rows.

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM