Code and description for processing massive data table sharding using ThinkPHP

Source: Internet
Author: User

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;

}

}

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.