Design of database Shard ID (php instance)

Source: Internet
Author: User

Sharding is a frequently used solution in the design of large Web site database, this paper tries to give a design scheme implemented by PHP, the structure of shard_id is 64bit,10bit Sharid system +10bit for type id+10 subtype Id+34bit self-increment ID. For more information on Shard Design, please refer to my other articles.


<?php    /**     *  Production Database Partition id      *     *  Description      *     *   @package         api     *  @author          xxx     *  @copyright        Copyright  (c)  2014, xx.im.     * @ since          version 1.0      *  @filesource      *     *  @property   database2  $database      */    class shard     {        var  $database  = null;         function shard ()         {              $this->database = load (  ' database '  );         }        function get_ shard_id (  $table _name,  $cate _id,  $subcate _id )          {            return  $this->gen_shard _id (  $this->get_next_id (  $table _name ),  $cate _id,  $subcate _id );         }        /**          *  Generate shared id         The structure of  *         * id:          *  altogether64-bit, 10-bit shard_id,10 bit type id,10 seat type id,34 bit self-increment id         * 10  Bits shard_id,10 bits cate_id,10 bits subcate_id,34 auto increment id          * 10+10+10+34          *         *  @param   $next _auto_ increment_id required  self-increment id         *  @param  $ cate_id                 required  type id         *  @param   $subcate _id              optional  Sub-type id          *         *  @return  bigint    &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;*/&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;FUNCTION&NBSP;GEN_SHARD_ID (   $next _auto_increment_id,  $cate _id,  $subcate _id )          {            if ( empty ( $ next_auto_increment_id )  )             {                 return 0;             }              $shard _id =  $this->get_shard_num (  $next _auto_increment_ id, total_shard_num );             $shard _id =  $shard _id <<  ( 64 - 10 );             //shard id             $shard _ id |=  $cate _id <<  ( 64 - 10 - 10 );             //Big Type               $shard _id |=  $subcate _id <<  ( 64 - 10 -  10 - 10 );          //subtype               $shard _id |=  $next _auto_increment_id;    //self-increment id            return  $shard _ id;        }        /**          *  parsing  shard id  Structure          &The structure of Nbsp;*         * id:          *  altogether 64-bit, 10-bit shard_id,10 bit type id,10 seat type id,34 bit self-increment id          * 10 bits shard_id,10 bits cate_id,10 bits subcate_id,34  auto increment id         * 10+10+10+34          *         *  @param   $shard _id         *          *  @return  array (' Shard_num ' =>0, ' cate_id ' =>0, ' subcate_id ' + = ', ' id ' = >0)          */         function parse_shard_id (  $shard _id )         {              $ret  = array (  ' Shard_num '   => 0,                             ' cate_id '     => 0,                             ' subcate_id '  => 0,                            ' Id '          => 0 );             if ( empty (  $shard _id )  )              {                 return  $ret;            }              $ret [  ' Shard_num '  ] = $ shard_id >>  ( 64 - 10 );              $ret [  ' cate_id '  ] =  (  $shard _id >>  ( 64  - 10 - 10 )  )  & 1023;              $ret [  ' subcate_id '  ] =  (  $shard _id >>  (  64 - 10 - 10 - 10 )  )  & 1023;              $ret [  ' id '  ] =  $shard _id &  17179869183;            return  $ret;         }        /**          * get shard location          *         *  @param   $next _auto_increment_id          *  @param   $total _shard_num          *         *  @return  int          */         Function get_shard_num (  $next _auto_increment_id,  $total _shard_num )          {            return   $next _auto_increment_id %  $total _shard_num;        }         /**         *  the next self-increment id     of the query table      *         *  @param   $table _name         *          *  @return  int         */         function get_next_id (  $table _name )          {            if ( empty ( $ table_name )  )             {                 return 0;             }            &Nbsp;//            //  need grant  access information_schema privilege             //             $sql  =  " select auto_increment from information_schema.tables where table_name =  ' $ table_name '  and table_schema = database () ";             /**  @var  CI_DB_Result  $query  */              $data  =  $this->database->unique (  $sql  )             if ( !empty (  $data  )  )             {          &nbsP;      return isset (  $data [  ' auto_increment '  ] )   ?  $data [  ' auto_increment '  ] : 0;             }            else             {                 return 0;             }        }    }


Design of database Shard ID (php instance)

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.