澳门威利斯人_威利斯人娱乐「手机版」

来自 网络资讯 2019-07-13 18:56 的文章
当前位置: 澳门威利斯人 > 网络资讯 > 正文

应用ThinkPHP内置的分表算法处理百万级用户数据,

  数据表:

ThinkPHP管理海量数据分表机制详细代码及申明

 

选取ThinkPHP内置的分表算法管理百万级用户数据. 数据表: house_member_0 house_member_1 house_member_2 house_member_3 模型中 class MemberModel extends AdvModel { protected $partition = array('field'='username','type'='id','num'='4'); public f

 

   

 

   应用ThinkPHP内置的分表算法管理百万级用户数据.

  数据表:

  house_member_0

  house_member_1

  house_member_2

  house_member_3

  模型中

  class MemberModel extends AdvModel {

  protected $partition = array('field'=>'username','type'=>'id','num'=>'4');

  public function getDao($data=array()) {

  $data = empty($data) ? $_POST : $data;

  $table = $this->getPartitionTableName($data);

  return $this->table($table);

  }

  }

  方法中

  class MemberAction extends BaseAction {

  public function login() {

  if($this->isPost()) {

  $this->validToken();

  $dao = D('Member')->getDao();

  $res = $dao->where('username = '.$_POST['username'])->find();

  // output 为自定义方法

  // $isAjax - bool

  $this->output(false);

  }

  $this->display();

  }

  }

  /**

   ----------------------------------------------------------

  * 获得分表的的数码表名

   ----------------------------------------------------------

  * @access public

   ----------------------------------------------------------

  * @param array $data 操作的多少

   ----------------------------------------------------------

  * @return string

   ----------------------------------------------------------

  */

  public function getPartitionTableName($data=array()) {

  // 对数据表举办分区

  if(isset($data[$this->partition['field']])) {

  $field = $data[$this->partition['field']];

  switch($this->partition['type']) {

  case 'id':

  // 按照id范围分表

  $step = $this->partition['expr'];

  $seq = floor($field / $step) 1;

  break;

  case 'year':

  // 依据年度分表

  if(!is_numeric($field)) {

  $field = strtotime($field);

  }

  $seq = date('Y',$field)-$this->partition['expr'] 1;

  break;

  case 'mod':

  // 根据id的模数分表

  $seq = ($field % $this->partition['num']) 1;

  break;

  case 'md5':

  // 依照md5的队列分表

  $seq = (ord(substr(md5($field),0,1)) % $this->partition['num']) 1;

  break;

  default :

  if(function_exists($this->partition['type'])) {

  // 扶助钦点函数哈希

  $fun = $this->partition['type'];

  $seq = (ord(substr($fun($field),0,1)) % $this->partition['num']) 1;

  }else{

  // 依照字段的首字母的值分表

  $seq = (ord($field{0}) % $this->partition['num']) 1;

  }

  }

  return $this->getTableName().'_'.$seq;

  }else{

  // 当设置的分表字段不在查询条件还是数额中

  // 实行共同查询,必须设定 partition['num']

  $tableName = array();

  for($i=0;$i<$this->partition['num'];$i )

  $tableName[] = 'SELECT * FROM '.$this->getTableName().'_'.$i;

  $tableName = '( '.implode(" UNION ",$tableName).') AS '.$this->name;

  return $tableName;

  }

  }


本小说重即使介绍有关用php对mysql数据库,与数据表以及数据开始展览备份了,代码写得有一些长然而行行都以实例了,好了费话不说多了小编们来看看那款mysql数据库备份代码的吗。

  house_member_0

本小说首假设介绍有关用php对mysql数据库,与数据表以及数据开始展览备份了,代码写得有点长不过行行都是实例了,好了费话不说多了我们来看看那款mysql数据库备份代码的吗。

  house_member_1

本条顺序仅仅备份和复苏数据,使用方法很简短,实例化DbBak,然后调用bakupDb和restoreDb方法:
   1、实例化DbBak须要报告它两件事:数据服务器在什么地方($connectid)、备份到哪个目录($backupDir):

  house_member_2

require_once('DbBak.php');
<?php
//独有DbBak技术调用这几个类
class TableBak{
        var $_mysql_link_id;
        var $_dbDir;
        //private $_DbManager;
        function TableBak($mysql_link_id,$dbDir)
        {
                $this->_mysql_link_id = $mysql_link_id;
                $this->_dbDir = $dbDir;
        }
       
        function backupTable($tableName)
        {
                //step1:创制表的备份目录名:
                $tableDir = $this->_dbDir.DIRECTORY_SEPARATOR.$tableName;
                !is_dir($tableDir) && mkdir($tableDir);
                //step2:开首备份:
                $this->_backupTable($tableName,$tableDir);
        }
       
        function restoreTable($tableName,$tableBakFile)
        {
                set_time_limit(0);
                $fileArray = @file($tableBakFile) or die("can open file $tableBakFile");
                $num = count($fileArray);
                mysql_unbuffered_query("DELETE FROM $tableName");
                $sql = $fileArray[0];
                for ($i=1;$i<$num-1;$i ){                       
                        mysql_unbuffered_query($sql.$fileArray[$i]) or (die (mysql_error()));
                }
                return true;
        }
       
        function _getFieldInfo($tableName){
                $fieldInfo = array();
                $sql="SELECT * FROM $tableName LIMIT 1";
                $result = mysql_query($sql,$this->_mysql_link_id);
                $num_field=mysql_num_fields($result);
                for($i=0;$i<$num_field;$i ){
                        $field_name=mysql_field_name($result,$i);
                        $field_type=mysql_field_type($result,$i);
                        $fieldInfo[$field_name] = $field_type;
                }
                mysql_free_result($result);
                return $fieldInfo;
        }
        function _quoteRow($fieldInfo,$row){
                foreach ($row as $field_name=>$field_value){
                        $field_value=strval($field_value);
                        switch($fieldInfo[$field_name]){ 
                                case "blob":     $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;                         
                                case "string":   $row[$field_name] = "'".mysql_escape_string($field_value)."'";break; 
                                case "date":          $row[$field_name] = "'".mysql_escape_string($field_value)."'";break; 
                                case "datetime": $row[$field_name] = "'".mysql_escape_string($field_value)."'";break; 
                                case "time":          $row[$field_name] = "'".mysql_escape_string($field_value)."'";break; 
                                case "unknown":  $row[$field_name] = "'".mysql_escape_string($field_value)."'";break;                                         
                                case "int":          $row[$field_name] = intval($field_value); break;
                                case "real":     $row[$field_name] = intval($field_value); break;
                                case "timestamp":$row[$field_name] = intval($field_value); break;
                                default:                  $row[$field_name] = intval($field_value); break;
                        }
                }
                return $row;
        }
        function _backupTable($tableName,$tableDir)
        {
                //猎取表的字段类型:
                $fieldInfo = $this->_getFieldInfo($tableName);
               
                //step1:构造INSERT语句前半有个别 并写入文件:
                $fields = array_keys($fieldInfo);
                $fields = implode(',',$fields);
                $sqltext="INSERT INTO $tableName($fields)VALUES rn";
                $datafile = $tableDir.DIRECTORY_SEPARATOR.$tableName.'.sql';
                (!$handle = fopen($datafile,'w')) && die("can not open file <b>$datafile</b>");
                (!fwrite($handle, $sqltext))  && die("can not write data to file <b>$datafile</b>");
                fclose($handle);
               
                //step2:获得数据 并写入文件:
                //收取表财富:
                set_time_limit(0);
                $sql = "select * from $tableName";
                $result = mysql_query($sql,$this->_mysql_link_id);
                //张开数据备份文件:$tableName.xml
                $datafile = $tableDir.DIRECTORY_SEPARATOR.$tableName.'.sql';
                (!$handle = fopen($datafile,'a')) && die("can not open file <b>$datafile</b>");
                //逐个取得表记录并写入文件:
                while ($row = mysql_fetch_assoc($result)) {
                        $row = $this->_quoteRow($fieldInfo,$row);       
                        $record='(' . implode(',',$row) . ");rn";
                        (!fwrite($handle, $record))  && die("can not write data to file <b>$datafile</b>");
                }
                mysql_free_result($result);
                //关闭文件:
                fclose($handle);
               
                return true;
        }

  house_member_3

}
?>
require_once('TableBak.php');
那个文件代码

  模型中//wedding jewelry wholesale

require_once('TableBak.php');
class DbBak {
        var $_mysql_link_id;
        var $_dataDir;
        var $_tableList;
        var $_TableBak;
       
        function DbBak($_mysql_link_id,$dataDir)
        {
                ( (!is_string($dataDir)) || strlen($dataDir)==0) && die('error:$datadir is not a string');
                !is_dir($dataDir) && mkdir($dataDir);
                $this->_dataDir = $dataDir;
                $this->_mysql_link_id = $_mysql_link_id;
        }
       
        function backupDb($dbName,$tableName=null)
        {
                ( (!is_string($dbName)) || strlen($dbName)==0 ) && die('$dbName must be a string value');
                //step1:接纳数据库:
                mysql_select_db($dbName);
                //step2:成立数据库备份目录
                $dbDir = $this->_dataDir.DIRECTORY_SEPARATOR.$dbName;
                !is_dir($dbDir) && mkdir($dbDir);
                //step3:获得数据库全体表名 并初叶备份表
                $this->_TableBak = new TableBak($this->_mysql_link_id,$dbDir);
                if(is_null($tableName)){//backup all table in the db
                        $this->_backupAllTable($dbName);
                        return;
                }
                if(is_string($tableName)){
                        (strlen($tableName)==0) && die('....');
                        $this->_backupOneTable($dbName,$tableName);
                        return;
                }
                if (is_array($tableName)){
                        foreach ($tableName as $table){
                                ( (!is_string($table)) || strlen($table)==0 ) && die('....');
                        }
                        $this->_backupSomeTalbe($dbName,$tableName);
                        return;
                }
        }
       
        function restoreDb($dbName,$tableName=null){
                ( (!is_string($dbName)) || strlen($dbName)==0 ) && die('$dbName must be a string value');
                //step1:检查是或不是留存数据库 并连接:
                @mysql_select_db($dbName) || die("the database <b>$dbName</b> dose not exists");
                //step2:检查是否存在数据库备份目录
                $dbDir = $this->_dataDir.DIRECTORY_SEPARATOR.$dbName;
                !is_dir($dbDir) && die("$dbDir not exists");
                //step3:start restore
                $this->_TableBak = new TableBak($this->_mysql_link_id,$dbDir);
                if(is_null($tableName)){//backup all table in the db
                        $this->_restoreAllTable($dbName);
                        return;
                }
                if(is_string($tableName)){
                        (strlen($tableName)==0) && die('....');
                        $this->_restoreOneTable($dbName,$tableName);
                        return;
                }
                if (is_array($tableName)){
                        foreach ($tableName as $table){
                                ( (!is_string($table)) || strlen($table)==0 ) && die('....');
                        }
                        $this->_restoreSomeTalbe($dbName,$tableName);
                        return;
                }
        }
       
        function _getTableList($dbName)
        {
                $tableList = array();
                $result=mysql_list_tables($dbName,$this->_mysql_link_id);
                for ($i = 0; $i < mysql_num_rows($result); $i ){
                array_push($tableList,mysql_tablename($result, $i));
                }
                mysql_free_result($result);
                return $tableList;
        }
       
        function _backupAllTable($dbName)
        {
                foreach ($this->_getTableList($dbName) as $tableName){
                        $this->_TableBak->backupTable($tableName);
                }
        }
       
        function _backupOneTable($dbName,$tableName)
        {
                !in_array($tableName,$this->_getTableList($dbName)) && die("钦定的表名<b>$tableName</b>在数据库中不设有");
                $this->_TableBak->backupTable($tableName);
        }
       
        function _backupSomeTalbe($dbName,$TableNameList)
        {
                foreach ($TableNameList as $tableName){
                        !in_array($tableName,$this->_getTableList($dbName)) && die("内定的表名<b>$tableName</b>在数据库中不真实");
                }
                foreach ($TableNameList as $tableName){
                        $this->_TableBak->backupTable($tableName);
                }
        }
       
        function _restoreAllTable($dbName)
        {
                //step1:检查是还是不是存在全部数据表的备份文件 以及是还是不是可写:
                foreach ($this->_getTableList($dbName) as $tableName){
                        $tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR
                                                       . $dbName.DIRECTORY_SEPARATOR
                                                      . $tableName.DIRECTORY_SEPARATOR
                                               . $tableName.'.sql';
                        !is_writeable ($tableBakFile) && die("$tableBakFile not exists or unwirteable");
                }
                //step2:start restore
                foreach ($this->_getTableList($dbName) as $tableName){
                        $tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR
                                                      . $dbName.DIRECTORY_SEPARATOR
                                                      . $tableName.DIRECTORY_SEPARATOR
                                                      . $tableName.'.sql';
                        $this->_TableBak->restoreTable($tableName,$tableBakFile);
                }
        }
       
        function _restoreOneTable($dbName,$tableName)
        {
                //step1:检查是不是留存数据表:
                !in_array($tableName,$this->_getTableList($dbName)) && die("钦点的表名<b>$tableName</b>在数据库中官样文章");
                //step2:检查是或不是留存数量表备份文件 以及是还是不是可写:
                $tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR
                                            . $dbName.DIRECTORY_SEPARATOR
                                             . $tableName.DIRECTORY_SEPARATOR
                                            . $tableName.'.sql';
                !is_writeable ($tableBakFile) && die("$tableBakFile not exists or unwirteable");
                //step3:start restore
                $this->_TableBak->restoreTable($tableName,$tableBakFile);
        }
        function _restoreSomeTalbe($dbName,$TableNameList)
        {
                //step1:检查是还是不是留存数据表:
                foreach ($TableNameList as $tableName){
                        !in_array($tableName,$this->_getTableList($dbName)) && die("内定的表名<b>$tableName</b>在数据库中不设有");
                }
                //step2:检查是还是不是留存数量表备份文件 以及是或不是可写:
                foreach ($TableNameList as $tableName){
                        $tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR
                                                      . $dbName.DIRECTORY_SEPARATOR
                                                      . $tableName.DIRECTORY_SEPARATOR
                                                      . $tableName.'.sql';
                        !is_writeable ($tableBakFile) && die("$tableBakFile not exists or unwirteable");
                }
                //step3:start restore:
                foreach ($TableNameList as $tableName){
                        $tableBakFile = $this->_dataDir.DIRECTORY_SEPARATOR
                                                      . $dbName.DIRECTORY_SEPARATOR
                                                      . $tableName.DIRECTORY_SEPARATOR
                                                      . $tableName.'.sql';
                        $this->_TableBak->restoreTable($tableName,$tableBakFile);
                }
        }
}
?>

  class MemberModel extends AdvModel {

$connectid = mysql_connect('localhost','root','123456');

  protected $partition = array('field'=>'username','type'=>'id','num'=>'4');

$backupDir = 'data';

  public function getDao($data=array()) {

$DbBak = new DbBak($connectid,$backupDir);
复制代码2、然后就足以起来备份数据库了,你非但能够内定备份那二个数据库,何况能详细设置只备份这个表:
  2.1如若你想备份mybbs库中的全数表,只要这么: $DbBak->backupDb('mybbs');
复制代码2.2万一您只想备份mybbs库中的board、face、friendlist表,能够用一个一维数组点名: $DbBak->backupDb('mybbs',array('board','face','friendsite'));
复制代码2.3若是只想备份贰个表,比如board表: $DbBak->backupDb('mybbs','board');
复制代码3,数据复苏:
对此2.1、2.1、2.3二种景况,只要相应的修改下语句,把backupDb换来restoreDb就能够促成数据复苏了: $DbBak->restoreDb('mybbs');
复制代码$DbBak->restoreDb('mybbs',array('board','face','friendsite'));
复制代码$DbBak->restoreDb('mybbs','board');

  $data = empty($data) ? $_POST : $data;

测验实例

  $table = $this->getPartitionTableName($data);

//example 1 backup:
require_once('DbBak.php');
require_once('TableBak.php');
$connectid = mysql_connect('localhost','root','123456');
$backupDir = 'data';
$DbBak = new DbBak($connectid,$backupDir);
$DbBak->backupDb('mybbs');

  return $this->table($table);

require_once('DbBak.php');
require_once('TableBak.php');
$connectid = mysql_connect('localhost','root','123456');
$backupDir = 'data';
$DbBak = new DbBak($connectid,$backupDir);
$DbBak->restoreDb('mybbs');

  }

...

  }

  方法中

  class MemberAction extends BaseAction {

  public function login() {

  if($this->isPost()) {

  $this->validToken();

  $dao = D('Member')->getDao();

  $res = $dao->where('username = '.$_POST['username'])->find();

  // output 为自定义方法

  // $isAjax - bool

  $this->output(false);

  }

  $this->display();

  }

  }

  /**

本文由澳门威利斯人发布于网络资讯,转载请注明出处:应用ThinkPHP内置的分表算法处理百万级用户数据,

关键词: 澳门威利斯人