Optimization of Mysql connections

Asked

Viewed 834 times

2

I have a pertinent question regarding the connections in my database.

I was told that every new connection to Mysql a portion of the RAM is reserved for that connection. I have several applications developed in PHP and that connect in the same database. My question is: how do connections with the database work in PHP and whether there is a way to optimize them.

As far as I know, every time the PHP script runs, it opens a connection to the database and this connection usually lasts until the end of the script execution. Following this reasoning, if the same script is executed 10 times by different clients or not at the same time, even if by thousandths of a second, we will have 10 different connections with the bank. Am I right!? This is what happens in practice?

If the above reasoning is correct and new connections to the database are opened every time my PHP script is run, then a portion of RAM is dynamically allocated to each execution of the script, correct? Is there any way to optimize connections with the database in order to reuse open connections? If possible, open a single connection where the script will communicate with the bank as many times as necessary. This is possible?

From now on I appreciate any help. ;)

  • Welcome to stack overflow, recommend reading from tour to get acclimated with the site, it works different from forum. So I edited your question, here no time subscriptions :).

  • 1

    The ideal is that you keep a cache of requests within php code and avoid accessing the database many times, only accessing when there is an update or insertion of data. Queries can be curled, very simply.

  • Ivan, in this case let’s assume that I can’t cache the data for even a few seconds.

  • I have the same question as you regarding the applications that access the same bank. as your question is from 2015, I think you’ve already learned something about. could help me?

  • @Claytinho, what I did was follow suggestion of our friend Sanction and use persistent connections. Thus, all requests from the same user@host use a single connection. Also set a 30-second timeout for each inactive connection. Then, after this period, an inactive connection is automatically closed.

  • @Victorotávio And you never had problem with locked script preventing new connections or something with transactions? Anyway I appreciate the attention. I’ll try that.

Show 1 more comment

2 answers

2

Hello, I’m not sure if this is valid in your case but I bring you a tip: use the Singleton design pattern. Basically it seeks to use the same instance of an object, and thus the same connection to the database:

class Conexao {
//utilizando singleton
public static $instance;

public function __construct() {
    //construtor vazio singleton
}

public static function getInstance(){
    if(!isset(self::$instance)){
        self::$instance = new PDO('mysql:host=SERVIDOR;dbname=BANCO', 'USUARIO', 'SENHA', array(PDO::MYSQL_ATTR_INIT_COMMAND=>'SET NAMES utf8'));
        self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    return self::$instance;
}}

And replace SERVER, DATABASE, USER, PASSWORD with your data, to start a new connection use the following code:

$conexao = Conexao::getInstance();

So you don’t "build" a new object if you already have one built.

  • Thanks for the tip, Natalia. All my applications already work like this. My question is when two different clients run the same script, thus generating two connections etc... :)

  • @Natalia Kelim Thiel works as a persistent connection? This is good for the same application. In the case of a database serving multiple application servers, only optimize individual applications.

  • Well, with two clients at the same time it won’t work, but it already helps individually.

1


Use persistent connections:

Persistent connections are connections that do not close when your script runs out. When a persistent connection is requested, PHP checks for an identical persistent connection (which was previously kept open) - and, if it exists, it uses it. If it doesn’t exist, it creates the connection. An 'identical' connection is a connection that has been opened to the same host, with the same username and password (where applicable).

Source: Persistent Connections to the Database.


If you use PDO just enter the option PDO::ATTR_PERSISTENT in the builder:

<?php
$conexao = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
    PDO::ATTR_PERSISTENT => true
));

Is used mysqli add p: at the server address:

<?php
$conexao = new mysqli('p:localhost', $user, $pass, $database);

Note: I didn’t add the shape to using mysql_connect() because the extension was discontinued: /a/4675/13561

Browser other questions tagged

You are not signed in. Login or sign up in order to post.