简体   繁体   中英

Difference between Sqlalchemy execution time and execution time from EXPLAIN query?

I am using postgresql with Flask and sqlalchemy. I found that if I do something like in python,

t1 = datetime.datetime.now()
session.query(Table_Class).all()
t2 = datetime.datetime.now()
print (t2 - t1)

I am getting execution time of sqlalchemy query. But if I go to postgresql and write same sql statement with EXPLAIN ANALYSE then also I can get execution time of sql query. Like,

EXPLAIN ANALYSE select * from table;

I found that the difference between these execution times is much. The sqlalchemy execution time is almost 10 times than execution time from EXPLAIN query. For small queries the two times are (75ms and 2 ms) and for long queries with many joins the times are (1.5sec and 200ms). I don't know why this is happening. Why do these times have big difference?

The difference is probably cause by communication overhead. When you run explain analyse you are measuring the exact time of the query in your postgres database. When you measure the time in the python code you are measuring the time of the query execution plus: the time of connecting to the database, sending the data, receiving/parsing the result, etc. Depending on the amount of data transferred and the physical location of your database you can expect a significant overhead in communication compared to the actual execution time of the query.

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