Detailed code for thinkphp disposal of massive data table mechanism

Source: Internet
Author: User
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; }  }

?

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.