Automatic Control Of Statement Execution

Once we have assigned the primary write server, we can assign readonly servers. These servers are used to delegate readonly SQL operations, thereby reducing the load on the primary server. The Load balancer class can automatically determine which SQL statements can be delegated onto secondary nodes. There is no limit to the number of secondary servers that can be created.

$db = new ADOdbLoadbalancer;
 
$db0 = new ADOdbLoadbalancerConnection('mysqli','write',1,'host1','user','pass','database');
$db->addConnection($db0);
 
$db1 = new ADOdbLoadbalancerConnection('mysqli','readonly',2,'host2','user','pass','database');
$db->addConnection($db1);
 
$db2 = new ADOdbLoadbalancerConnection('mysqli','readonly',3,'host3','user','pass','database');
$db->addConnection($db2);
 
/*
*  Keep adding readonly servers
*/

The load balancer automatically controls the execution of SQL statements through ADOdb by parsing the statement and directing the execution to the appropriate server:

  1. Statements that require read-write access to the database are directed to the primary server.
  2. Statements that require read-only access to the database are directed to the next available second

In this way, expensive or long running queries that might impact the performance of the primary server are offloaded to a secondary device.