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 :).
– rray
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 Ferrer
Ivan, in this case let’s assume that I can’t cache the data for even a few seconds.
– Victor Otávio
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
@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.
– Victor Otávio
@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.
– Claytinho