ライブラリとサブテーブル

3131 ワード

1,背景:アドレス帳のアプリケーションで、設計されたユーザー数は2億人で、統計によると、ユーザーごとのアドレス帳は30個程度で、全体のデータ量は60億人で、mysqlデータベースを使用しています.
計画は1000個のテーブル、100個のライブラリに分けられます.
2、ライブラリのサブテーブルコード

    private function getDbNo($email)
    {
        $m = md5($email);
        $n = hexdec(substr($m, 0, 16));
        $tableNo = fmod($n, 1000);
        $dbNo = $tableNo % 100;
        return array($dbNo, $tableNo);
    }

3,連携接続アクセスコード

require_once 'Db/Config.php';

class Db_Adapter
{
    const MASTER    = 0;
    const SLAVE     = 1;

    private static $instances = array();

    private $conf = array();

    private $conns = array();

    private $conn = NULL;
    private $stmt = NULL;
    
    public function __construct($conf)
    {
        $this->conf = $conf;
    }

    public function execute($sql, $params)
    {
        $cmd = substr(strtolower(trim($sql)), 0, 6);
        if ($cmd == 'select') {
            $conn = $this->getConn(self::SLAVE);
        } else {
            $conn = $this->getConn(self::MASTER);
        }

        $conn->prepare($sql);
        $stmt = $conn->execute($params);

        $this->conn = $conn;
        $this->stmt = $stmt;
    }

    public function fetch()
    {
        return $this->stmt->fetch();
    }

    public function fetchAll()
    {
        return $this->stmt->fetchAll();
    }

    public function lastInsertId($name = NULL)
    {
        return $this->conn->lastInsertId($name);
    }

    public function rowCount()
    {
        return $this->stmt->rowCount();
    }

    private function getConn($type)
    {
        if ($type == self::SLAVE && isset($this->conf[self::SLAVE])) {
            $id = 0;
        } else {
            $id = 1;
        }

        if (!isset($this->conns[$id])) {
            $conf = $this->conf[$id];
            $this->conns[$id] = new PDO(
                $conf['dsn'], $conf['user'], $conf['pass'],
                self::dbOptions);
        }

        return $this->conns[$id];
    }

    public static function getInstance($dbName, $dbNo = 0)
    {
        $key = $dbName . '_' . $dbNo;
        if (!isset(self::$instances[$key])) {
            $conf = Db_Config::getConfig($dbName, $dbNo); //      
            self::$instances[$key] = new self($conf);
        }

        return self::$instances[$key];
    }
}

4、潜在的な問題
テーブルのユーザーのアドレス帳の連絡先が1000人など、テーブルの中のユーザーのアドレス帳の連絡先が多すぎる場合、テーブルが大きくなる可能性があります.テーブルをサブテーブルに区別する必要があります.一時的には、この状況を処理するために構成センターがありません.
(本当にこの場合、接続パラメータという場所でhashを1回続けます).