Black Lives Matter. Please consider donating to Black Girls Code today.

MySQL recommended connection method for Dash app

Hi there

For Using MySQL database systems, what would be the most effective connecting type

  • connecting at start of the code? disconnecting at …?
  • connecting and disconnecting in a separate function called many times?
  • Connecting and disconnecting in a callback?

I aim building an app which runs beautifully locally, but up in a server takes ages to load, I was recommended to use MySQL instead of the csv files that I was loading, I just don’t know how friendly is Dash with MySQL


It depends a lot on your usage pattern as well as the client implementation, i.e. the amount of overhead associated with establishing a connection.

Generally speaking, if you only need to make a large query once in a while, it would probably be OK just to open/close a connection each time. If you make lots of small queries, it might be worth reusing the connection.

As an example i recently implemented auditing on an app (lots of small inserts), in which case I used a threaded connection pool stored in global scope in order to enable reuse of connections (that was a psql db, but the principle is the same).

NOTE: Depending in your data usage pattern, the MySQL solution might be faster that a csv file, but I could also be slower. How does your data/usage look?

Thanks Emil
I am consulting a database many times, just reading ~10k rows tables to Dataframes and slicing
I am trying to reuse the already read dataframes, but i note that I am consulting the MySQL many times, so perhaps a one connection could be sufficient as a main inflow of data