user8100646
user8100646

Reputation: 137

The most efficient way to use mysqli connection

I am searching for a efficient way to use PHP MySQL innoDB connection but not able to found conclusive information on the web.

As I know, persistent connection is much faster than non-persistent one,

we can set up the connection in following way:

$instance_mysqli = new mysqli('p:127.0.0.1', 'username', 'password', 'db');

However, from the official website, it said the default behavior is "reset" on reuse, which is slower. http://php.net/manual/en/mysqli.persistconns.php

The mysqli extension does this cleanup by automatically calling the C-API function mysql_change_user(). The automatic cleanup feature has advantages and disadvantages though. The advantage is that the programmer no longer needs to worry about adding cleanup code, as it is called automatically. However, the disadvantage is that the code could potentially be a little slower, as the code to perform the cleanup needs to run each time a connection is returned from the connection pool.

So, there is no way to pass parameter to the above constructor to avoid "reset"? The only way is to recompile extension from source code as the document suggested?

And my anther question is... if mysqli is so smart that it can automatically reset connection by default, what is the point many people still use non-persistent connection, which is even slower.

Upvotes: 0

Views: 152

Answers (1)

Rick James
Rick James

Reputation: 142208

The cost of a connection is quite small, whether it is persistent or not, whether there is cleanup or not.

Normally, one should acquire one connection at the beginning of the program, and keep it until the end. (There are some exceptions.)

The only time a connection is really noticeable is if you acquire a connection before each and every SQL query.

Bottom line: Worry about your indexes, system design, etc, not about acquiring the connection.

Upvotes: 2

Related Questions