phpmysqlweb-architecture

Architecture needed to reduce mysql connections in AJAX/PHP/MYSQL


BACKGROUND:

I am passing variables through AJAX to php file. The php file connects to a server and retrieves the result which it passes back to javascript. This happens every time a user clicks on the request button (about every 5 secs). Thus for each user, the php file (and so the mysql connection) is called once every 5 secs.

ISSUE:

As is apparent above, the number of mysql connections are impractically high.

QUESTION:

Is there a better architecture where instead of having so many mysql connections, I can rather have fewer connections.

enter image description here

I have read a little bit about mysql_pconnect. But what happens if I have to upgrade since I read somewhere that mysqli doesnt support it? How many queries can a single mysql_pconnect handle? If anyone suggests mysql_pconnect then how to implement it?


Solution

  • Is there a better architecture where instead of having so many
    mysql connections, I can rather have fewer connections.
    

    Don't really know, but I think that for you the proposed pconnect is the best available option. Unless you have either mysqli or PDO_mysql available now?

    I have read a little bit about mysql_pconnect.
    But what happens if I have to upgrade since I read somewhere that mysqli doesnt support it?
    

    You would probably need to change method when upgrading beyond PHP 5.5.

    How many queries can a single mysql_pconnect handle?
    

    Unlimited, as long as the connection is kept alive. If there are no available free connections a new one is created.

    If anyone suggests mysql_pconnect then how to implement it?
    

    Change your current mysql_connect calls to mysql_pconnect. That should be all.