Use ThinkPHP’s built-in table splitting algorithm to process millions of user data.
Data sheet:
house_member_0
house_member_1
house_member_2
house_member_3
In model
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);
}
}
Method
class MemberAction extends BaseAction {
public function login() {
if($this->isPost()) {
$this->validToken();
$dao = D(Member)->getDao();
$res = $dao->where(username = .$_POST[username])->find();
// output is a custom method
// $isAjax - bool
$this->output(false);
}
$this->display();
}
}
/**
+------------------------------------------------- ------------
* Get the data table name of the sub-table
+------------------------------------------------- ------------
* @access public
+------------------------------------------------- ------------
* @param array $data The data to be operated on
+------------------------------------------------- ------------
* @return string
+------------------------------------------------- ------------
*/
public function getPartitionTableName($data=array()) {
// Partition the data table
if(isset($data[$this->partition[field]])) {
$field = $data[$this->partition[field]];
switch($this->partition[type]) {
case id:
// Split tables according to id range
$step = $this->partition[expr];
$seq = floor($field / $step)+1;
break;
case year:
// Table by year
if(!is_numeric($field)) {
$field = strtotime($field);
}
$seq = date(Y,$field)-$this->partition[expr]+1;
break;
case mod:
// Split the table according to the modulus of id
$seq = ($field % $this->partition[num])+1;
break;
case md5:
// Split the table according to the md5 sequence
$seq = (ord(substr(md5($field),0,1)) % $this->partition[num])+1;
break;
default:
if(function_exists($this->partition[type])) {
// Support specified function hash
$fun = $this->partition[type];
$seq = (ord(substr($fun($field),0,1)) % $this->partition[num])+1;
}else{
// Split the table according to the value of the first letter of the field
$seq = (ord($field{0}) % $this->partition[num])+1;
}
}
return $this->getTableName()._.$seq;
}else{
// When the set sub-table field is not in the query conditions or data
// To perform a joint query, partition[num]
must be set$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;
}
}