I'm building an API that reads data from a table in PostgresQL, perform some transformations and return it.
However, when I try to run pd.read_sql("SELECT * FROM my_table", engine), it seems to take quite a few seconds.
On the contrary, when I try to read it from DBeaver, the query took less than a second to return the result.
May I get some advice to improve this? Ideally, I'm trying to make the API to return the result quicker and optimising this seems to be a good step.
Additional information: The table has around 3k records and 10 columsn.
Code:
def query_data():
items = pd.read_sql("SELECT * FROM items", engine)
return items.to_list()
@app.get("/get_data")
def get_data():
t_1 = time()
items = query_data()
t_2 = time()
print("t_2 - t_1: ", round(t_3 - t_2, 0), "s")
return items