中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

一個(gè) MySQL 分庫(kù)分表php類

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用

當(dāng)一個(gè)表數(shù)據(jù)記錄過(guò)大時(shí)就會(huì)出現(xiàn)性能瓶頸,而一般對(duì)應(yīng)的解決辦法是要么做分區(qū)表,要么分表,分區(qū)表就不說(shuō)了,分表又分為垂直分割和水平分割,具體區(qū) 別請(qǐng)自行搜索。一般而言,分庫(kù)分表屬于水平分割,按照一定的規(guī)則將數(shù)據(jù)插入到不同的表中去。而分庫(kù)則可以很方便的轉(zhuǎn)移數(shù)據(jù)庫(kù)的壓力,比如將一個(gè)很大庫(kù)的分 別放在不同的服務(wù)器上。

下面是我寫的一個(gè)分庫(kù)分表的實(shí)現(xiàn):

    <?php  
    /** 
     * User: guoyu 
     * Date: 14-8-12 
     * Time: 下午3:16 
     */  
       
    namespace App\Model\Database;  
       
    class Config  
    {  
        public $dsn;  
        public $user;  
        public $password;  
        /** 
         * @var string 分庫(kù)分表后得到的數(shù)據(jù)庫(kù)名 
         */  
        public $dbname;  
        /** 
         * @var string 分庫(kù)分表后得到的表名 
         */  
        public $table;  
       
        /** 
         * @var array MySQL 配置數(shù)組 
         */  
        private static $config;  
       
        /** 
         * @var string 配置文件路徑 
         */  
        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, $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_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'];  
        }  
       
    }  


Config 類就做一個(gè)事情,根據(jù)配置文件,去拿到對(duì)應(yīng)的庫(kù)和表的鏈接配置,然后客戶可以根據(jù) dsn 去鏈接對(duì)應(yīng)的數(shù)據(jù)庫(kù)。對(duì)應(yīng)的配置文件如下:

[php] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片

    <?php  
    /** 
     * User: guoyu 
     * Date: 14-8-6 
     * Time: 上午11:19 
     */  
       
    $default = array(  
        'unix_socket' => null,  
        'host' => 'localhost',  
        'port' => '3306',  
        'user' => 'root',  
        'password' => '',  
    );  
       
    $config = array(  
        // 不進(jìn)行分庫(kù)分表的數(shù)據(jù)庫(kù)  
        'db' => array(  
            'my_site' => $default,  
        ),  
        // 分庫(kù)分表  
        'shared' => array(  
            'user' => array(  
                'host' => array(  
                    /** 
                     * 編號(hào)為 0 到 10 的庫(kù)使用的鏈接配置 
                     */  
                    '0-10' => $default,  
                    /** 
                     * 編號(hào)為 11 到 28 的庫(kù)使用的鏈接配置 
                     */  
                    '11-28' => $default,  
                    /** 
                     * 編號(hào)為 29 到 99 的庫(kù)使用的鏈接配置 
                     */  
                    '29-99' => $default,  
       
                ),  
       
                // 分庫(kù)分表規(guī)則  
                /** 
                 * 下面的配置對(duì)應(yīng)百庫(kù)百表 
                 * 如果根據(jù) uid 進(jìn)行分表,假設(shè) uid 為 543234678,對(duì)應(yīng)的庫(kù)表為: 
                 *  (543234678 / 1) % 100 = 78 為編號(hào)為 78 的庫(kù) 
                 *  (543234678 / 100) % 100 = 46 為編號(hào)為 46 的表 
                 */  
                'database_split' => array(1, 100),  
                'table_split' => array(100, 100),  
            ),  
        ),  
    );  
       
       
    return $config;  


給出一個(gè)使用這個(gè)分庫(kù)分表的例子:

[php] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片

    <?php  
    /** 
     * User: guoyu 
     * Date: 14-8-6 
     * Time: 上午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 string 分庫(kù)分表后對(duì)應(yīng)的表 
         */  
        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);  
        }  
    }  

下面這個(gè)例子展示了如何使用上述的 Model 類:
    <?php  
    /** 
     * User: guoyu 
     * Date: 14-8-12 
     * Time: 下午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);  

來(lái)自:http://blog.csdn.net/phpfenghuo/article/details/45342311

標(biāo)簽: Mysql 代碼 服務(wù)器 數(shù)據(jù)庫(kù) 搜索

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇: jdbc 使用PreparedStatement來(lái)存儲(chǔ)和讀取大數(shù)據(jù)(Blob或Clob)

下一篇:字符串樸素匹配C++實(shí)現(xiàn)