Connect efficiently to MySQL Database on Azure
Database connections are a limited resource, so making sensible use of connection pooling to access MySQL Database on Azure optimizes performance.
Access databases that use connection pooling (recommended)##
Managing database connections can have a significant impact on the performance of the application as a whole. To optimize the performance of your program:
- Reduce the number of times connections are established.
- Avoid specifying the time for establishing connections in key code paths.
- Use database connection pooling or persistent connections to connect to MySQL Database on Azure.
Database connection pooling handles the creation, management, and allocation of database connections. When a program requests a database connection, it prioritizes the allocation of existing idle database connections, rather than the creation of a new connection. Once the program has finished using the database connection, the connection is recovered in preparation for further use, rather than simply being closed down.
The server configures a timeout mechanism, closing a connection that has been in an idle state for some time, in order to free up resources. Be sure to set up the authentication system to ensure the effectiveness of persistent connections when you are using them. For more information, see: How to configure authentication systems on the client side to ensure the effectiveness of persistent connections.
Access databases that use persistent connections (recommended)##
Use persistent connections in PHP. The concept of persistent connections is similar to that of connection pooling. It is important to note that PHP currently has three types of drivers. While the MySQLi driver type does not support persistent connection, the other two types of drivers do.
Replacing short connections with persistent connections requires only minor changes to the code, but has a major effect in terms of improving performance in many typical application scenarios.
For more information, see Use PDO to create persistent connections and Use the MySQL engine to create persistent connections.
Access databases by using wait and retry mechanisms with short connections##
If you use short connections and experience connection failures when approaching the upper limit on the number of concurrent connections, you can try connecting multiple times. You can set an appropriate wait time, with a shorter wait time after the first attempt. After this, waiting for random events can be performed multiple times.