Use the built-in table sharding algorithm of ThinkPHP to process millions of user data. Data Table: house_member_0house_member_1house_member_2house_member_3 classMemberModelextendsAdvModel {protected $ partition = arr
Use the built-in table sharding algorithm of ThinkPHP to process millions of user data.
Data table:
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
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 name of the table shard.
+ ----------------------------------------------------------
* @ Access public
+ ----------------------------------------------------------
* @ Param array $ data operation data
+ ----------------------------------------------------------
* @ 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 ':
// Table sharding by id range
$ Step = $ this-> partition ['expr'];
$ Seq = floor ($ field/$ step) + 1;
Break;
Case 'year ':
// Table sharding by year
If (! Is_numeric ($ field )){
$ Field = strtotime ($ field );
}
$ Seq = date ('Y', $ field)-$ this-> partition ['expr'] + 1;
Break;
Case 'mod ':
// Id-based module/table sharding
$ Seq = ($ field % $ this-> partition ['num']) + 1;
Break;
Case 'md5 ':
// Table sharding by md5 sequence
$ Seq = (ord (substr (md5 ($ field), 0, 1) % $ this-> partition ['num']) + 1;
Break;
Default:
If (function_exists ($ this-> partition ['type']) {
// Supports specified function hashing
$ Fun = $ this-> partition ['type'];
$ Seq = (ord (substr ($ fun ($ field), 0, 1) % $ this-> partition ['num']) + 1;
} Else {
// Table sharding based on the first letter of the field
$ Seq = (ord ($ field {0}) % $ this-> partition ['num']) + 1;
}
}
Return $ this-> getTableName (). '_'. $ seq;
} Else {
// When the table sharding field is not in the query condition or data
// For 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;
}
}