mysql database sub-library

Summarize the experience and experience of the recent project, and share it with you: mysql database sub-library

Let's first look at the sub-database structure of the database:


There are about 7 or 8 sub-libraries (sub-libraries by region)

config corresponds to the configuration of the sub-database (DB_CONFIG1 in Huangpu District; DB_CONFIG2 in Pudong New District...), the district id of Huangpu District is 1, and the district id of Pudong New District is 2:


//model layer reads the sub-library:

class XxxModel {

    /*

     * new db

     */

     private function iDb($region) { // District id (the district id is spliced ​​into config)

          return M()->db($region, "DB_CONFIG".$region); // The db function is defined in the model class file below

     }

      /*

     * get log

     * @param $region #region ID

     * @param $uid #User ID 

     * @param $coursewareid #Class course ID

     * return false|array

     */

      public function getRecordByXx($region, $uid, $coursewareid) {

       return $this->iDb($region)->table('xxxx_xxxx_record')->where(array("uid" => $uid, "over_time" => 0, "courseid" => $courseid))->find();

    }

}

 

// thinkphp framework DB class library (framework comes with, no need to modify)

class Model {

    // current database operation object

    protected $db = null;

    ....

   /**

     * Switch the current database connection

     * @access public

     * @param integer $linkNum connection number

     * @param mixed $config database connection information

     * @param array $params model parameters

     * @return Model

     */

    public function db($linkNum,$config='',$params=array()){

        static $_db = array();

        if(!isset($_db[$linkNum])) {

            // create a new instance

            if(!empty($config) && false === strpos($config,'/')) { // supports reading configuration parameters

                $config  =  C($config);

            }

            $_db[$linkNum]            =    Db::getInstance($config);

        }elseif(NULL === $config){

            $_db[$linkNum]->close(); // close the database connection

            unset ($ _ db [$ linkNum]);

            return ;

        }

        if(!empty($params)) {

            if(is_string($params))    parse_str($params,$params);

            foreach ($params as $name=>$value){

                $this->setProperty($name,$value);

            }

        }

        // switch database connection

        $this->db   =    $_db[$linkNum];

        return $this;

    }

 

}

 // Architecture ideas:

First: The database sub-database is divided into multiple databases according to certain rules;

Second: In the configuration file config, set multiple connection database configurations (as shown in the figure above, corresponding to the database name);

Third: When the model layer reads the operation table, the user's district/county id is passed in to locate which database to read;

Fourth: Create a table in the general database to record the uid of all users in all districts and counties, the id of districts and counties and other fields that need to be associated with the tables in the sub-libraries;

Note: This is only a general idea, and the specific business process can be changed. .

The forecast will share with you next time: php implementation and ranking algorithm for large data volume

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326681457&siteId=291194637