mysql分表

用戶表量級過大 目的把用戶表分一百張:
下面是試例:
<?php
namespace app\index\controller;

class Index
{
    public function index()
    {
        $uId = '123123';
        $tabCount = 100;//分成100個表 表名user0-user99 下表從0開始
        $res = $this->getStringHash($uId,$tabCount);
        halt($res);
        //1·100 會根據uid返回 剩下的就是業務邏輯了。
    }

    public function getStringHash($uId, $tabCount)
    {
        $unsign = sprintf('%u', crc32($uId));
        if ($unsign > 2147483647) {
            $unsign -= 4294967296;
        }
        return abs($unsign) % $tabCount;
    }
}

//創建表

       for ($i=0; $i <= 99; $i++) {
            Db::query("CREATE TABLE user$i 裏面是 創建表語句")
       }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章