Django & MySQL - connecting to a database on demand is very slow

We just set up a “New Relic” for monitoring, and he says that 90% of our query time is spent on MySQLdb:connect - between one and ten seconds per request!

Django seems to open a new MySQL connection for each query. How can I further diagnose what is happening? Are there any Django or MySQL options that I can use to speed up my MySQL connection?

Note. I excluded DNS because the interfaces connect to the database using digital IP.

+4
source share
1 answer

The problem is that django will close the connection on every request and initialize it again; as the db code listens for the signal:

signals.request_finished.connect(close_connection) in django/db/__init__.py

One solution is to implement a connection pool for MySQL using SQLAlchemy , which is the least intrusive and most convenient to upgrade I've seen.

+3
source

Source: https://habr.com/ru/post/1384822/


All Articles