Forums

Slow database connections via Flask App

I'm just wondering if there is an obvious reason for this....

I run a query in a MySQL database console and the results are returned within 0.2 seconds.

The same query then via a URL route in a flask app (even stripping further code - just simply make the request and return the results) takes around 5 to 9 seconds.

Is this just all the remote connections to the database etc? If so, is there anything I can do to speed things up further?

There shouldn't be any real difference between the two cases if you're running exactly the same query -- although there is a small overhead to connect to the database server, it should be pretty minimal.

How are you connecting to MySQL from Flask? Are you sending a raw SQL query to it, or is there something like SQLAlchemy in the middle that might be generating a different query to what you might expect?

You should make sure that a single-colomn layout is used on small screens. This can easily be done in Bootstrap: https://getbootstrap.com/docs/4.1/layout/grid/

You should also use PythonAnywhere's static file service to serve your images, since it's faster than doing that through the framework you use: https://help.pythonanywhere.com/pages/StaticFiles