Detailed code of ThinkPHP processing massive data table sharding mechanism

Source: Internet
Author: User
ThinkPHP processing massive data table sharding mechanism detailed Code application ThinkPHP built-in table sharding algorithm to process millions of user data. Data Table: house_member_0house_member_1house_member_2house_m ThinkPHP processing massive data table sharding mechanism detailed code
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 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 );}} 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 table sharding data table name + partition * @ access public + -------------------- * @ param array $ data operation data + partition * @ 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 $ ste P = $ 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 modulus table sharding $ seq = ($ field % $ this-> partition ['num']) + 1; break; case 'md5 ': // table sharding by md5 sequence $ seq = (ord (substr (md5 ($ field),) % $ this-> partition ['num']) + 1; break; default: if (function_exists ($ this-> partition ['type']) {// supports specifying the function hash $ fun = $ this-> partition ['type']; $ seq = (ord (substr ($ fun ($ field )) % $ this-> partition ['num']) + 1;} else {// table sharding 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 table sharding field is not in the query condition or is in the 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 ). ') '. $ 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.