thinkphp下MySQL數(shù)據(jù)庫讀寫分離代碼剖析
當(dāng)采用原生態(tài)的sql語句進(jìn)行寫入操作的時候,要用execute,讀操作要用query。
MySQL數(shù)據(jù)主從同步還是要靠MySQL的機(jī)制來實現(xiàn),所以這個時候MySQL主從同步的延遲問題是需要優(yōu)化,延遲時間太長不僅影響業(yè)務(wù),還影響用戶體驗。
thinkphp核心類Thinkphp/library/Model.class.php 中,query 方法,調(diào)用Thinkphp/library/Think/Db/Driver/Mysql.class.php
/**
* SQL查詢
* @access public
* @param string $sql SQL
* @param mixed $parse 是否需要解析SQL
* @return mixed
*/
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);
}
調(diào)用Thinkphp/library/Think/Db/Driver/Mysql.class.php
/**
* 執(zhí)行查詢 返回數(shù)據(jù)集
* @access public
* @param string $str sql指令
* @return mixed
*/
public function query($str) {
if(0===stripos($str, 'call')){ // 存儲過程查詢支持
$this->close();
$this->connected = false;
}
$this->initConnect(false);
if ( !$this->_linkID ) return false;
$this->queryStr = $str;
//釋放前次的查詢結(jié)果
if ( $this->queryID ) { $this->free(); }
N('db_query',1);
// 記錄開始執(zhí)行時間
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();
}
}
上面初始化數(shù)據(jù)庫鏈接時,initConnect(false),調(diào)用Thinkphp/library/Think/Db/Db.class.php,注意false、true代碼實現(xiàn)。true表示直接調(diào)用主庫,false表示調(diào)用讀寫分離的讀庫。
/**
* 初始化數(shù)據(jù)庫連接
* @access protected
* @param boolean $master 主服務(wù)器
* @return void
*/
protected function initConnect($master=true) {
if(1 == C('DB_DEPLOY_TYPE'))
// 采用分布式數(shù)據(jù)庫
$this->_linkID = $this->multiConnect($master);
else
// 默認(rèn)單數(shù)據(jù)庫
if ( !$this->connected ) $this->_linkID = $this->connect();
}
/**
* 連接分布式服務(wù)器
* @access protected
* @param boolean $master 主服務(wù)器
* @return void
*/
protected function multiConnect($master=false) {
foreach ($this->config as $key=>$val){
$_config[$key] = explode(',',$val);
}
// 數(shù)據(jù)庫讀寫是否分離
if(C('DB_RW_SEPARATE')){
// 主從式采用讀寫分離
if($master)
// 主服務(wù)器寫入
$r = floor(mt_rand(0,C('DB_MASTER_NUM')-1));
else{
if(is_numeric(C('DB_SLAVE_NO'))) {// 指定服務(wù)器讀
$r = C('DB_SLAVE_NO');
}else{
// 讀操作連接從服務(wù)器
$r = floor(mt_rand(C('DB_MASTER_NUM'),count($_config['hostname'])-1)); // 每次隨機(jī)連接的數(shù)據(jù)庫
}
}
}else{
// 讀寫操作不區(qū)分服務(wù)器
$r = floor(mt_rand(0,count($_config['hostname'])-1)); // 每次隨機(jī)連接的數(shù)據(jù)庫
}
$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);
}
query方法參數(shù)為false,其他刪除、更新、增加讀主庫。這一點可以結(jié)合Thinkphp/library/Model.class.php中的delete、save、add操作,參數(shù)為true。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- tp5(thinkPHP5框架)時間查詢操作實例分析
- ThinkPHP中SHOW_RUN_TIME不能正常顯示運(yùn)行時間的解決方法
- 獲取php頁面執(zhí)行時間,數(shù)據(jù)庫讀寫次數(shù),函數(shù)調(diào)用次數(shù)等(THINKphp)
- ThinkPHP中獲取指定日期后工作日的具體日期方法
- thinkPHP+PHPExcel實現(xiàn)讀取文件日期的方法(含時分秒)
- ThinkPHP框架實現(xiàn)的MySQL數(shù)據(jù)庫備份功能示例
- thinkphp3.x連接mysql數(shù)據(jù)庫的方法(具體操作步驟)
- 基于ThinkPHP5框架使用QueryList爬取并存入mysql數(shù)據(jù)庫操作示例
- thinkphp5框架結(jié)合mysql實現(xiàn)微信登錄和自定義分享鏈接與圖文功能示例
- thinkphp5.1框架實現(xiàn)格式化mysql時間戳為日期的方式小結(jié)
相關(guān)文章
Yii框架批量插入數(shù)據(jù)擴(kuò)展類的簡單實現(xiàn)方法
這篇文章主要介紹了Yii框架批量插入數(shù)據(jù)擴(kuò)展類的簡單實現(xiàn)方法,涉及Yii擴(kuò)展類及數(shù)據(jù)庫相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
php excel reader讀取excel內(nèi)容存入數(shù)據(jù)庫實現(xiàn)代碼
很多新手朋友對于如何使用php-excel-reader讀取excel內(nèi)容存入數(shù)據(jù)庫,甚是疑惑,本文將介紹詳細(xì)的解決方案,需要了解的朋友可以參考下2012-12-12
php定義數(shù)組和使用示例(php數(shù)組的定義方法)
這篇文章主要介紹了php定義數(shù)組和使用示例(php數(shù)組的定義方法),需要的朋友可以參考下2014-03-03
基于jQueryUI和Corethink實現(xiàn)百度的搜索提示功能
這篇文章主要介紹了基于jQueryUI和Corethink實現(xiàn)百度的搜索提示功能,這里是以corethink模塊的形式,只需要安裝上訪問index.php?s=/test/index 就可以了,需要的朋友可以參考下2016-11-11
PHP 閉包獲取外部變量和global關(guān)鍵字聲明變量的區(qū)別講解
閉包是一個常見的概念,我們通常可以將其與回調(diào)函數(shù)配合使用,可以使代碼更加簡潔易讀。這篇文章主要介紹了PHP 閉包獲取外部變量和global關(guān)鍵字聲明變量的區(qū)別,需要的朋友可以參考下2017-12-12

