Detailed code and description of thinkphp processing massive data table mechanism

Source: Internet
Author: User
Tags array md5 ord query

The millions user data is processed using the thinkphp built-in table algorithm.

Data table:

House_member_0

House_member_1

House_member_2

House_member_3

In the 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->validtoken ();

$dao = D (' member ')->getdao ();

$res = $dao->where (' username = '. $_post[' username ')->find ();

Output as a custom method

$isAjax-bool

$this->output (FALSE);

}

$this->display ();

}

}

/**

+----------------------------------------------------------

* Get the datasheet name of the table

+----------------------------------------------------------

* @access Public

+----------------------------------------------------------

* Data @param array $data operations

+----------------------------------------------------------

* @return String

+----------------------------------------------------------

*/

Public Function Getpartitiontablename ($data =array ()) {

Partitioning a data table

if (Isset ($data [$this->partition[' field ']]) {

$field = $data [$this->partition[' field ']];

Switch ($this->partition[' type ']) {

Case ' ID ':

List by 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 ':

According to the ID modulus of the table

$seq = ($field% $this->partition[' num ']) +1;

Break

Case ' MD5 ':

According to the sequence of MD5

$seq = (ORD (substr (MD5 ($field), 0,1))% $this->partition[' num '] +1;

Break

Default:

if (function_exists ($this->partition[' type ')) {

Supports specifying function hashes

$fun = $this->partition[' type '];

$seq = (Ord (substr ($fun ($field), 0,1))% $this->partition[' num '] +1;

}else{

Table by the value of the first letter of the field

$seq = (Ord ($field {0})% $this->partition[' num ') +1;

}

}

return $this->gettablename (). ' _ '. $seq;

}else{

When you set a table field that is not in query criteria or data

To make a joint 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;

}

}




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.