Home > php教程 > PHP源码 > thinkphp项目中使用分表的思路(适用于大数据)

thinkphp项目中使用分表的思路(适用于大数据)

PHP中文网
Release: 2016-05-26 08:19:55
Original
1015 people have browsed it

php代码

/**
 * 获得分表名
 * @param $tableName            基础表名
 */
function getSubTable($tableName, $companyId = null)
{
    // 每张表中放50个集团的数据
    $table_user = 50;

    //确定 companyInfo数组
    if (null === $companyId)    //未指定,根据session取
    {
        $companyInfo = M('Company')->find(COMPANY_ID);
    }
    else
    {
        $mCompany = M('Company');
        $companyInfo = $mCompany->where('id = ' . $companyId)->find();
    }

    //公司信息中 指定了table_name,直接取出返回
    if (isset($companyInfo['table_name']) && $companyInfo['table_name'] != '')  
    {
        return $tableName . '_' . $companyInfo['table_name'];
    }

    //公司信息不存在直接返回表名
    if (!$companyInfo)
    {
        return $tableName;
    }

    //company表未指定id,按算法返回表名 (公司id % 50) = 分表id
    $total = $companyInfo['id'] % $table_user;
    //短路模式 当前面的 $total === 0 为真时候  $total = 1
    $total === 0 && $total =1;  
    return $tableName . '_' . $total;
}
Copy after login

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template