This article mainly introduces the thinkphp MySQL database reading and writing separation code analysis, with a certain reference value, interested in small partners can refer to.
When using the original SQL statement to write operations, to use execute, read operations to use query.
MySQL Data master-slave synchronization or to rely on the MySQL mechanism to achieve, so this time MySQL master-slave synchronization delay problem is to be optimized, the delay time is too long not only affect the business, but also affect the user experience.
Thinkphp Core class thinkphp/library/model.class.php, the query method, called thinkphp/library/think/db/driver/mysql.class.php
/** * SQL query * @access public * @param string $sql SQL * @param mixed $parse need to parse SQL * @return mixed< c6/>*/Public function query ($sql, $parse =false) { if (!is_bool ($parse) &&!is_array ($parse)) { $ Parse = Func_get_args (); Array_shift ($parse); } $sql = $this->parsesql ($sql, $parse); return $this->db->query ($sql); }
Call thinkphp/library/think/db/driver/mysql.class.php
/** * Execute Query return DataSet * @access Public * @param string $str SQL instruction * @return Mixed */public function qu Ery ($str) { if (0===stripos ($str, ' call ')) {///stored procedure query support $this->close (); $this->connected = false; } $this->initconnect (false); if (! $this->_linkid) return false; $this->querystr = $str; Releases the previous query result if ($this->queryid) { $this->free (); } N (' Db_query ', 1); Record start execution time G (' querystarttime '); $this->queryid = mysql_query ($str, $this->_linkid); $this->debug (); if (false = = = $this->queryid) { $this->error (); return false; } else { $this->numrows = mysql_num_rows ($this->queryid); return $this->getall (); } }
When you initialize the database link above, Initconnect (false), call thinkphp/library/think/db/db.class.php, note false, true code implementation. True indicates that the main library is called directly, and false means that read-write separation is invoked.
/** * Initialize database connection * @access protected * @param boolean $master primary server * @return void */protected function Initcon Nect ($master =true) {if (1 = = C (' db_deploy_type '))//using a distributed database $this->_linkid = $this->multiconnect ($mas ter); else//default single database if (! $this->connected) $this->_linkid = $this->connect (); /** * Connect distributed server * @access protected * @param boolean $master primary server * @return void */protected function multic Onnect ($master =false) {foreach ($this->config as $key = + $val) {$_config[$key] = explode (', ', $val); }//database read and write whether to detach if (C (' db_rw_separate ')) {//master/slave with read-write separation if ($master)//master server Write $r = floor (M T_rand (0,c (' Db_master_num ')-1)); else{if (Is_numeric (C (' Db_slave_no '))) {//specify server Read $r = C (' db_slave_no '); }else{//Read operation connection from server $r = Floor (Mt_rand (C (' Db_master_num '), COUNT ($_config[' hostname '])-1); Each randomly connected database}}}else{The read-write operation does not differentiate between server $r = Floor (Mt_rand (0,count ($_config[' hostname '))-1); Each randomly connected database} $db _config = Array (' username ' + isset ($_config[' username ' [$r])? $_config[' username '] [$r]:$ _config[' username '][0], ' password ' = isset ($_config[' password ' [$r])? $_config[' Password ' [$r]:$_config[' Password '][0], ' hostname ' = isset ($_config[' hostname '] [$r])? $_config[' hostname '] [$r]:$_config[' hostname '][0], ' Hostport ' = isset ($_config[' hostport '] [$r])? $_config[' Hostport ' [$r]:$_config[' Hostport '][0], ' database ' => ; Isset ($_config[' database '] [$r])? $_config[' database ' [$r]:$_config[' database '][0], ' DSN ' = isset ($_config[' DSN '] [$r])? $_config[' DSN '] [$r]:$_config[' DSN '][0], ' params ' = isset ($_config[' params '] [$r])? $_config[' params '][$ r]:$_config[' params '][0], ' charset ' = Isset ($_config[' charset '] [$r])? $_config[' CharSet ' [$r]:$_config[' CharSet '][0],); return $this->connect ($db _config, $r); }
The Query method parameter is False, and other deletions, updates, and additions to the main library are read. This can be combined with the delete, save, add operation in thinkphp/library/model.class.php with the parameter true.
The above is the whole content of this article, I hope that everyone's study has helped.