mysql sub-table + query

In a recent interview, I asked some questions about the sub-tables of the MySQL database, such as designing a membership database of tens of millions of levels. I thought of sub-tables at that time, but when I asked how to query the user's information according to the username, at this time, it was a bit difficult. I got stuck, so I recorded it in order to re-understand the sub-table.

Vertical split table:

  In fact, there is nothing to talk about, that is, the  primary key + common columns  are placed in the original table, and  then the primary key + some uncommon columns are  placed in another table .

  Such a data page can store more data. But the disadvantages are also obvious, and operations such as join or union may be added.

 

Horizontal sub-table:

  I was asked about the level score table in the interview today, and I was suddenly stunned. I knew the score, but it was hard to say how to effectively query after the score.

  Principle: Specific analysis of specific situations.

  Several common divisions:

    1. by time

      Typical applications: news, QQ status, Moments, etc. focus on real-time or recent events, which can be divided by time, such as one table for the current month and one table for the previous month.

    2. According to the interval

      Usually each table will have an auto-increment id, you can use the auto-increment id score, such as

      user1 table is 1~50

      The user2 table is 51~100 //After the insert operation is completed, determine the id value, and create a new table when it exceeds 50w

    3. Hash table

      In essence, it doesn't make any sense. The modulo of each inserted data is ok for single-handed record query. If you query adjacent rows of data, it will be a tragedy. Interested students can refer to the first link below.

 

  Query after table division:

    1. For the time level sub-table:

      Suppose the state table of the circle of friends will generate 20w records every day, and create a table table_20150401 to store them. Then use cron to run a script to create a new table in the early morning every night, such as table_20150402. in php program

     The function that performs the insertion is encapsulated, and the following two functions are used. 

copy code
insertData( $data ){
     $table = "table_". date ("Ymd", time ()); // Generate the table name of the day 
    insert( $data , $table );   // Insert into a new table 
}

GetData( $condition ){  
     // If there is a time search in the condition, such as: $condition[time] , then analyze the corresponding table name and select one or more tables 
    $table = "table_". date ("Ymd ", time ()); // Generate the table name of the day 
    Get( $condition , $table );   // Find it in the new table 
}
copy code

    2. For id interval division

      新闻或朋友圈状态id 1~1000   1001~2000
                一、数据库里面建表 breakup_table   //这个表里专门记录,新分表和原表的 记录数,方便确定查找哪个表
                比如:   news_1  1   //起始key为1
                        news_2  1001  //起始key为1001
                        news_3  2001  

        二、上述 news_1 之类的数据,第一次需要同数据库拿,之后可以放到session或memcached里面

 

copy code
insertUser($data){
   $table_num = getCurrentNewNum(); //这个值用memcached去包裹,if($count < 2001)return 3;elseif($count < 1001)return 2;else 1;
$table = "table_".$table_num); //生成指定表名 insert($data,$table); // 插入新的表中 } GetUser($condition){ $table_num = getCurrentNewNum(); $table = "table_".$table_num); //生成指定表名 Get($condition,$table); // 在新的表中查找 }
copy code

 

    3、对于hash分表查询:

        典型user表 //单独拉出来讲,因为比较特别,第一次查找可能比较花时间,因为必须根据用户名确定去找哪个表
                可以这做:
                        $md5_val = md5($user_name);  //用crc32()应该也可以,但未尝试,如果尝试记得%u,使其不为负
                        $first_val = substr($md5_val, 0,1);//然后去取第一个值 
                        $decimal = hexdec($first_val); //十六进制转十进制
                        $table_num = $decimal%3 + 1; // 求余3,使得只有三张表,table_1,table_2,table_3
                      主要原理,利用user_name唯一性,导出md5唯一性,然后求余限制分表数量

copy code
insertUser($data){

    $table = "table_".$table_num); //生成指定表名
    insert($data,$table);  // 插入新的表中
}

GetUser($condition){  
    
    $table = "table_". $table_num ); // Generate the specified table name 
    Get( $condition , $table );   // Search in the new table 
}
copy code

    Hash sub-tables can also be used for id interval sub-tables, that is, modulo the id value.

 

Horizontal sub-tables, a typical disadvantage, are disasters for queries such as group by or order by.      

 

 

Related Links:

  http://www.phpddt.com/php/mysql-tables.html //Introduces the use of merge table

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326421895&siteId=291194637