Home php教程 php手册 ThinkPHP处理海量数据分表机制详细代码及说明

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

Jun 21, 2016 am 08:55 AM
data gt partition this

   应用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;$ipartition['num'];$i++)

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

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

  return $tableName;

  }

  }




Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

What are the differences between Huawei GT3 Pro and GT4?

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Fix: Snipping tool not working in Windows 11

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

How to Fix Can't Connect to App Store Error on iPhone

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

What data is in the data folder? What data is in the data folder? May 05, 2023 pm 04:30 PM

What data is in the data folder?

Is watch4pro better or gt? Is watch4pro better or gt? Sep 26, 2023 pm 02:45 PM

Is watch4pro better or gt?

What to do if mysql load data is garbled? What to do if mysql load data is garbled? Feb 16, 2023 am 10:37 AM

What to do if mysql load data is garbled?

AI project failure rates top 80% — study cites poor problem recognition and a focus on latest tech trends among major problems AI project failure rates top 80% — study cites poor problem recognition and a focus on latest tech trends among major problems Aug 31, 2024 am 12:59 AM

AI project failure rates top 80% — study cites poor problem recognition and a focus on latest tech trends among major problems

See all articles