mysql 水平分表


新建10張表,user_0,user_1,...user_9,方法不可串用,采用hash或取余法,獲取要操作的表名,取值用對應存值的方法

1.hash取余法

public function part_table(){

        $id=uniqid();                       //php生成唯一 uuid
        $str = crc32($id);                  //hash 算法轉換
        $table_name = 'user_'.$str%10;      //取余獲取 表名

        $user['id']=$id;
        $user['age']='20';
        $user['name']='小豬_'.time();

        db($table_name)->insert($user);      //插入數據庫

        echo $table_name;
    }

2.hash

public function part_table(){

        $id=uniqid();                            //php生成唯一 uuid
        $str = crc32($id);                       //hash 算法轉換
        $hash = substr($str, 0, 1);              // 32位 與64位操作 有差別
        $table_name = 'user_'.$hash;             //獲取 表名

        $user['id']=$id;
        $user['age']='20';
        $user['name']='小豬_'.time();

        db($table_name)->insert($user);      //插入數據庫

        echo $table_name;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM