When a table data record is too large, there will be performance bottlenecks, and the general solution is to either do partition table, or sub-table, partition table does not say, the table is divided into vertical and horizontal division, the specific difference please search by yourself. In general, the sub-database is split horizontally, and the data is inserted into different tables according to certain rules. The sub-Library can easily transfer the pressure of the database, such as a large library on the separate server.
Here is the implementation of a sub-library table that I wrote:
<?php/** * User:guoyu * date:14-8-12 * Time: PM 3:16 */namespace App\model\database; Class config{public $dsn; Public $user; Public $password; /** * @var Database name */public $dbname after a string sub-database table; /** * @var The table name given by the string sub-database table */public $table; /** * @var Array MySQL configuration arrays */private static $config; /** * @var string config file path */private static $configFile = ' mysql.php '; Public function __construct ($dbname, $table, $id = 0) {if (Is_null (static:: $config)) {$config = Inc Lude (static:: $configFile); Static:: $config = $config; } $config = static:: $config; if (Isset ($config [' shared ']) && isset ($config [' Shared '] [$dbname]) {$dbconfig = $config [' Shared '] [$dbn AME]; $id = Is_numeric ($id)? (int) $id: CRC32 ($id); $database _id = ($id/$dbconfig [' Database_split '][0])% $dbconfig [' database_split '][1]; $table _id = ($id/$DBCOnfig[' Table_split '][0])% $dbconfig [' table_split '][1]; foreach ($dbconfig [' Host '] as $key + = $conf) {list ($from, $to) = Explode ('-', $key); if ($from <= $database _id && $database _id <= $to) {$the _config = $conf; }} $this->dbname = $dbname. '_' . $database _id; $this->table = $table. '_' . $table _id; } else {$this->dbname = $dbname; $this->table = $table; $the _config = $config [' db '] [$dbname]; } $c = $the _config; if (Isset ($c [' Unix_socket ']) && $c [' Unix_socket ']) {$this->dsn = sprintf (' Mysql:dbname=%s;unix_sock et=%s ', $this->dbname, $c [' unix_socket ']); } else {$this->dsn = sprintf (' mysql:dbname=%s;host=%s;port=%s ', $this->dbname, $c [' Host '], $c [' Port ']; } $this->user = $c [' user ']; $this->password = $c [' PasswoRd ']; } }
The Config class does one thing, according to the configuration file, to get the corresponding library and table link configuration, and then the customer can link the corresponding database according to DSN. The corresponding configuration file is as follows:
<?php/** * User:guoyu * date:14-8-6 * Time: Morning 11:19 */$default = array (' unix_socket ' = null, ' host ' = = ' localhost ', ' port ' = ' 3306 ', ' user ' = ' root ', ' password ' = ', '; $config = Array (//database ' db ' = Array (' my_site ' = ') ' and ' => '),//Sub-Library sub-table ' GKFX ' ; Array (' user ' = = Array (' host ' = ' = ' = ' = ' = ' = ' = ' = ' = ' 0 ' to 10 '/** */' 0-10 ' + $default,/** * Link configuration used by libraries numbered 11 to 28 */' 11-28 ' + $default,/** * Link configuration used by libraries numbered 29 to 99 */' 29-99 ' + $default,),//Sub-list rules/** * The following configuration corresponds to hundred Library Hundred Table * if the table according to the UID, assuming the UID is 543234678, the corresponding library table is: * (543234678/1)% 100 = 78 is a library numbered 78 * (543234678/100)% 100 = 46 is a table with number 46 */' database_split ' = = Array (1, +), ' table_split ' = = = Array (100, 100),),); return $config;
Give an example of using this sub-Library table:
<?php/** * User:guoyu * date:14-8-6 * Time: Morning 10:23 * * namespace App\model; Use App\model\database\config;use \pdo; Abstract class model{/** * @var Config */public $config; /** * @var PDO */public $connection; protected $dbnamePrefix; protected $tablePrefix; /** * @var The corresponding table after the string sub-database table */protected $table; Public function __construct ($id) {$this->config = new Config ($this->dbnameprefix, $this->tableprefix, $ID); $this->connection = new Pdo ($this->config->dsn, $this->config->user, $this->config->password); $this->table = $this->config->table; The Public function is update (array $data, array $where = Array ()) {} Public Function Select (array $where) { Public function Insert (array $data) {} public Function query ($sql) {return $this->con Nection->query ($sql); }}
The following example shows how to use the Model class above:
<?php/** * User:guoyu * date:14-8-12 * Time: PM 4:06 */require ' config.php '; require ' model.php '; Use App\model\model; Class User extends model{ protected $dbnamePrefix = ' user '; protected $tablePrefix = ' userinfo ';} $user = new User (4455345345); Print_r ($user);
Share a MySQL sub-Library sub-table Class (PHP)