Share a MySQL database/table sharding class (php) and mysql database sharding class

Source: Internet
Author: User
Tags database sharding

Share a MySQL database/table sharding class (php) and mysql database sharding class

When the data record of a table is too large, a performance bottleneck will occur. Generally, the corresponding solution is to either create a partitioned table or a partitioned table, table shards are divided into vertical and horizontal partitions. You can search for the specific differences. Generally, database/table sharding is a horizontal split. Data is inserted into different tables according to certain rules. Database sharding can easily transfer the database pressure, for example, placing a large database on different servers.

Below is the implementation of a sub-database/sub-table I wrote:

<? Php/*** User: guoyu * Date: 14-8-12 * Time: */namespace App \ Model \ Database; class Config {public $ dsn; public $ user; public $ password;/*** @ var string database/table sharding database name */public $ dbname; /*** @ var string table name obtained after database/table sharding */public $ table;/*** @ var array MySQL configuration array */private static $ config; /*** @ var string configuration file path */private static $ configFile = 'mysql. php '; public function _ construct ($ dbname, $ Table, $ id = 0) {if (is_null (static: $ config) {$ config = include (static ::$ configFile); static :: $ config = $ config;} $ config = static: $ config; if (isset ($ config ['shared ']) & isset ($ config ['shared '] [$ dbname]) {$ dbconfig = $ config ['shared'] [$ dbname]; $ 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, $) = 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_socket = % 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, get the corresponding database and table link configuration, and then the customer can link the corresponding database according to the dsn. The corresponding configuration file is as follows:

<? Php/*** User: guoyu * Date: 14-8-6 * Time: */$ default = array ('unix _ socket '=> null, 'host' => 'localhost', 'Port' => '000000', 'user' => 'root', 'Password' => '',); $ config = array (// database without database/table sharding 'db' => array ('My _ site' => $ default ,), // database/table sharding 'shared '=> array ('user' => array ('host' => array (/*** link configuration used by libraries numbered 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 ,), // database/table sharding rule/*** the configuration below corresponds to a hundred databases/hundreds of tables * If table sharding is performed based on uid, assume that the uid is 543234678, and the corresponding database table is: * (543234678/1) % 100 = 78 the database * (543234678/100) % 100 = 46 is the table numbered 46 */'database _ split '=> array (1,100 ), 'table _ splits '=> array (100,100),); return $ config;

An example of using this database/table sharding function is provided:

<? Php/*** User: guoyu * Date: 14-8-6 * Time: am */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 string the table corresponding to the database/table sharding */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;} public function update (array $ data, array $ where = array ()) {} public function select (array $ where) {} public function insert (array $ data) {} public function query ($ SQL) {return $ this-> connection-> query ($ SQL );}}

The following example shows how to use the above Model class:

<? Php/*** User: guoyu * Date: 14-8-12 * Time: */require 'config. php'; require 'model. php '; use App \ Model; class User extends Model {protected $ dbnamePrefix = 'user'; protected $ tablePrefix = 'userinfo ';} $ user = new user( 4455345345); print_r ($ User );


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.