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 ;}}
?