应用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;
}
}

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

에디트플러스 중국어 크랙 버전
작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SecList
SecLists는 최고의 보안 테스터의 동반자입니다. 보안 평가 시 자주 사용되는 다양한 유형의 목록을 한 곳에 모아 놓은 것입니다. SecLists는 보안 테스터에게 필요할 수 있는 모든 목록을 편리하게 제공하여 보안 테스트를 더욱 효율적이고 생산적으로 만드는 데 도움이 됩니다. 목록 유형에는 사용자 이름, 비밀번호, URL, 퍼징 페이로드, 민감한 데이터 패턴, 웹 셸 등이 포함됩니다. 테스터는 이 저장소를 새로운 테스트 시스템으로 간단히 가져올 수 있으며 필요한 모든 유형의 목록에 액세스할 수 있습니다.

MinGW - Windows용 미니멀리스트 GNU
이 프로젝트는 osdn.net/projects/mingw로 마이그레이션되는 중입니다. 계속해서 그곳에서 우리를 팔로우할 수 있습니다. MinGW: GCC(GNU Compiler Collection)의 기본 Windows 포트로, 기본 Windows 애플리케이션을 구축하기 위한 무료 배포 가능 가져오기 라이브러리 및 헤더 파일로 C99 기능을 지원하는 MSVC 런타임에 대한 확장이 포함되어 있습니다. 모든 MinGW 소프트웨어는 64비트 Windows 플랫폼에서 실행될 수 있습니다.

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경
