Detailed code for thinkphp processing of massive data table mechanism
The thinkphp built-in sub-table algorithm is applied to process millions user data. Data sheet: house_member_0 house_member_1 house_member_2 house_member_3 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 in class Memberaction extends Baseaction {public Function login () {if ($this->ispost ()) {$this->validto Ken (); $dao = D (' Member ')->getdao (); $res = $dao->where (' username = '. $_post[' username ')->find (); Output is a custom method//$isAjax-bool $this->output (false); } $this->display (); }}/** +----------------------* Gets the table name of the data table +----------------------* @access public +----------------------* @p Aram Array $data operation data +----------------------* @return string +----------------------*/Public function Getpartiti Ontablename ($data =array ()) {//Partitioning the data table IF (Isset ($data [$this->partition[' field '])) {$field = $data [$this->partition[' field ']; Switch ($this->partition[' type ']) {case ' id '://by ID range $step = $this->partition[' expr ']; $seq = Floor ($field/$step) +1; Break Case ' Year '://According to the date of the table if (!is_numeric ($field)) {$field = Strtotime ($field); } $seq = Date (' Y ', $field)-$this->partition[' expr ']+1; Break Case ' mod '://According to the module of the id $seq = ($field% $this->partition[' num ']) +1; Break Case ' MD5 '://According to MD5 sequence sub-table $seq = (ORD (substr (MD5 ($field), 0,1))% $this->partition[' num ']) +1; Break Default:if (function_exists ($this->partition[' type ')) {//supports specifying function hash $fun = $this->partition[' type ']; $seq = (Ord (substr ($fun ($field), 0,1))% $this->partition[' num ']) +1; }else{//By 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 table field is not in the query condition or data//for the federated query, you must set 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; } }
?