(Turn) MYSQL district, sub-table, cluster

Forwarding: https://www.cnblogs.com/myvic/p/7711498.html

MYSQL district, sub-table, cluster

1. Partition

mysql database data files exist based on the situation, the default in / on the disk mysql / below the Data (through my. view cnf in datadir), a main table corresponds to the three documents, one is frm store table structure, a table data store is myd, 
a table index is stored myi. If the amount of data in a table is too large, then MYD , myi will become great, to find the data will become very slow, this time we can use the partition function of mysql, 
physically these correspond to a table the three documents, divided into many small pieces, so then, when we find a piece of data, do not all look up, just know this data in which one, and then a look at it on the line. 
If the data table is too large, it may be a disk does not fit, this time, we can distribute data to different disks to go inside

Two ways partition

a, transverse partition

What is the transverse partition it? Is sideways to the partition, for example to illustrate, if there 100W of data, divided into ten parts, prior 10W of data into the first partition, the second 10W of data into the second partition, and so on. That is the table into a very, Root merge to points table, a bit like oh. When an extracted data, this data contains all the fields in the table structure, that is to say the transverse partition, and does not change the structure of the table.

b, longitudinal partition

What is the vertical partition it? Is vertical to the partition, for example to illustrate the design of the user table, the beginning not considered good, but all the information individuals have put a table inside, so this table there will be a relatively large field such as personal profiles, and these brief introduction of it, maybe not a lot of people to see, so wait until it was time to look at, to look at the points table, they can put such a large field, separate.

mysql partitions which are provided a first, transverse partition and subdivided into a number of ways:

1.1 MySQL5.1 and above that support partitioning

Check whether to support the partition
 MySQL > Show the Variables like "Part%%" ;  
 + ------------------- + ------- +   
| variable_name | Value |   
+ + ------- + -------------------   
| have_partitioning | YES |   
+ ---------------- + ------- + ---   
Row in the SET ( 0.00 sec)

1.2 range partition

      This mode allows the data into different ranges. For example, a table may be divided into several years by partitions

the Create the Table t_range ( 
     the above mentioned id int ( 11),  
     Money int ( 11) unsigned not null , 
      DATE datetime 
  ) Partition by the Range (year ( DATE )) ( 
  Partition p2007 values less Within last ( 2008),  
  Partition P2008 values less Within last ( 2009),  
  partition p2009 values less Within last ( 2010 ) 
  partition P2010 values less Within last MAXVALUE   # MAXVALUE represents the maximum possible integer value 
  ); 

RANGE partitioning is particularly useful in the following situations:
 1), when the need to remove the "old" data on a partition,  only delete the partition. If you use the partitioning scheme above the most recent example given, you simply use "ALTER TABLE employees DROP PARTITION p0; "
  to delete all employees in the year 1991 has stopped working all the lines corresponding. For a large number of rows of the table, more than one run, such as "the Employees the WHERE the DELETE the FROM YEAR (Separated) <= 1990 ;" 
  such a DELETE query to be much more effective. 
2 ), contains want to use a date or time value, or from some other columns contain values of the series began to grow.
3 ), often run directly dependent on the columns for dividing a query table. 
  For example, when performing a as "the SELECT COUNT (*) the FROM the Employees the WHERE YEAR (Separated) = 2000 the GROUP BY store_id;" when such a query, 
  MySQL can quickly determine that only partition p2 needs to be scanned because the remaining partitions are not It may contain any records that match the WHERE clause

1.3 list partition

      This mode allows the system to segment the data by a predefined list of values.

Table t_list Create ( 
  A int ( . 11),  
  B int ( . 11 ) 
  ) (by Partition List (B) 
  Partition values in P0 ( 1,3,5,7,9),  
  Partition P1 in values ( 2,4,6, 8,0 ) 
); 
the LIST no similar partition such as "vALUES LESS THAN MAXVALUE" this contains definitions, other values. Any value to be matched must be found in the list of values.

1.4 hash partitioning

   This mode allows the table through one or more columns Key Hash calculated by the final partition of different values ​​corresponding to the Hash code data area. For example, you can create a table of primary key partition table.

 

CREATE TABLE employees (
    id INT NOT NULL,
    fname VARCHAR(30),
    lname VARCHAR(30),
    hired DATE NOT NULL DEFAULT '1970-01-01',
    separated DATE NOT NULL DEFAULT '9999-12-31',
    job_code INT,
    store_id INT
)
PARTITION BY HASH(store_id)
PARTITIONS 4;

 

1.5 key partition

      Hash above an extension mode, where the Hash Key generated by the system is MySQL.

1.6 sub-partitions

Sub-partition is the partition table is divided again each partition, subpartitions can either use the Greek HASH partitions, you can also use KEY partitions. This compound is also known as the partition ( Composite Partitioning ).

1, if a partition to create a child partition, other partitions also have sub-partitions

2, if you create a partition, the number of sub-partition each partition must have the same

3, sub-partitions of the same partition, the names are not identical, sub-partitions Hinako different partitions can be the same (5.1.50 NA

 

mysql> CREATE TABLE IF NOT EXISTS `sub_part` (  
 ->   `news_id` int(11) NOT NULL  COMMENT '新闻ID',  
 ->   `content` varchar(1000) NOT NULL DEFAULT '' COMMENT '新闻内容',  
 ->   `u_id`  int(11) NOT NULL DEFAULT 0s COMMENT '来源IP',  
 ->   `create_time` DATE NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '时间'  
 -> ) ENGINE=INNODB  DEFAULT CHARSET=utf8  
 -> PARTITION BY RANGE(YEAR(create_time))  
 -> SUBPARTITION BY HASH(TO_DAYS(create_time))(  
 -> PARTITION p0 VALUES LESS THAN (1990)(SUBPARTITION s0,SUBPARTITION s1,SUBPARTITION s2),  
 -> PARTITION p1 VALUES LESS THAN (2000)(SUBPARTITION s3,SUBPARTITION s4,SUBPARTITION good),  
 -> PARTITION p2 VALUES LESS THAN MAXVALUE(SUBPARTITION tank0,SUBPARTITION tank1,SUBPARTITION tank3)  
 -> );  
Query OK, 0 rows affected (0.07 sec)
The advantage of the partition

 1 , partition may be divided in a plurality of disks, a little more memory

 2 , according to a search condition, that is, where the latter condition, only to find the appropriate Find all searched without partition

 3 may be parallel, the large data relevant deal with. 

4, to spread data across multiple disks queries, to obtain greater query throughput

Partition Manager 1.7

a. delete partition

  alter table user drop partion p4

b. New Partition

 

the Add User Partition Table ALTER (P4 Partition values less Within last MAXVALUE); # new range partitioning 
ALTER list_part the Add Partition Table (P4 values in Partition (25,26,27))    # new list partition 
alter table hash_part add partition partitions 4; # hash repartitioning 
the ALTER the Table key_part the Add partition partitions 4; # Key repartition 
// sub-partitions add a new partition, though I did not specify sub-partitions, but the system will give a child partition named 
alter table sub1_part add partition (partition p3 values less than MAXVALUE);
 // the Range repartition   
the ALTER TABLE the User the REORGANIZE pARTITION P0, p1, P2, p3, P4 INTO (pARTITION P0 VALUES LESS THAN MAXVALUE);  
 // List repartition  
 TABLE list_part the REORGANIZE PARTITION P0 the ALTER, p1, P2, p3, P4 INTO (PARTITION in P0 VALUES (1,2,3,4,5 ));  
  # hash and key partition can not use REORGANIZE, the official website said very clearly

 

References: http: //blog.csdn.net/yongchao940/article/details/55266603

       http://www.cnblogs.com/mliudong/p/3625522.html

2. points table management

2.1 MySQL Cluster

Use mysql cluster, mysql proxy, mysql replication, drdb etc.

 

Some people may ask mysql cluster, the root partition table have anything to do? While on the score sheet it is not practical significance, but it is open to the role of the points table, what is the meaning of the cluster it? Reduce the burden to a database, it means reducing the number of sql sql queue queuing, 
for example: There are 10 sql request, if placed in a database server queue queue, he has to wait a very long time, if this 10 sql request, assigned to the queue line up five of the database server, a database server queue, only two, 
so the waiting time is not greatly shorten it? This is already evident. I it within the scope of the column to the part table; we clusters in the third part before described; 


advantages: scalability, no complicated operation of the plurality of part tables (php code) 
disadvantages: the amount of data from a single table is not changed , once operational time spent or so many large hardware overhead.

2.2 estimated in advance and there will be a large data frequently accessed tables will be divided into several tables

I 100 built in advance such a table, message_00, message_01, message_02 .......... message_98, message_99. Then to determine which tables inside the user's chat information into the user's ID,

You can use the hash of the way to get to be a way to get the remainder, many ways, everyone wants everyone in it. The following method to obtain a hash table:

 

<?php  
function get_hash_table($table,$userid) {  
 $str = crc32($userid);  
 if($str<0){  
 $hash = "0".substr(abs($str), 0, 1);  
 }else{  
 $hash = substr($str, 0, 2);  
 }  
  
 return $table."_".$hash;  
}  
  
echoget_hash_table ( 'Message', 'user18991');      // result message_10   
echo get_hash_table ( 'Message', 'user34523');     // results message_13 


advantages: Avoid a table appears millions of data, a shortened sql execution time 

disadvantages: when a rule to determine, breaking this rule will be very troublesome, hash algorithm above example I use crc32, if I do not want to use this algorithm, and after the switch to md5, make the same user messages are stored in different tables, 
   so that the data mess. Scalability is poor.

2.3 is achieved using the merge sub-table storage engine

merge sub-table, the sub-divided into a main table and the table, the master table is similar to a shell that encapsulates the logical sub-table, the data is actually stored in the child table.

mysql> CREATE TABLE IF NOT EXISTS `user1` (  
 ->   `id` int(11) NOT NULL AUTO_INCREMENT,  
 ->   `name` varchar(50) DEFAULT NULL,  
 ->   `sex` int(1) NOT NULL DEFAULT '0',  
 ->   PRIMARY KEY (`id`)  
 -> ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;  
Query OK, 0 rows affected (0.05 sec)  
  
mysql> CREATE TABLE IF NOT EXISTS `user2` (  
 ->   `id` int(11) NOT NULL AUTO_INCREMENT,  
 ->   `name` varchar(50) DEFAULT NULL,  
 ->   `sex` int(1) NOT NULL DEFAULT '0',  
 ->   PRIMARY KEY (`id`)  
 -> ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;  
Query OK, 0 rows affected (0.01 sec)  
  
mysql> INSERT INTO `user1` (`name`, `sex`) VALUES('张映', 0);  
Query OK, 1 row affected (0.00 sec)  
  
mysql> INSERT INTO `user2` (`name`, `sex`) VALUES('tank', 1);  
Query OK, 1 row affected (0.00 sec)  
  
mysql> CREATE TABLE IFEXISTS `alluser` the NOT (  
  ->` id` int (. 11) the NOT NULL the AUTO_INCREMENT,   
 -> `name` VARCHAR (50) the DEFAULT  NULL ,   
 ->` sex` int (. 1) the NOT NULL  the DEFAULT '0',   
 ->    the INDEX (ID)  
  ->) the TYPE = the MERGE the UNION = (user1, user2) the INSERT_METHOD = LAST the AUTO_INCREMENT =. 1 ;   
Query the OK , 0 rows affected,. 1 warning (0.00 sec) 
create a master table when there is the INSERT_METHOD, indicating insert mode, taking value can be: 0 not permit insertion; fIRST UNION inserted into the first table; lAST UNION inserted into the last table. 
By the time the main table query, equivalent to all the sub-table queries together. This does not reflect the advantages of sub-tables, queries or suggestions child table. 
 
Pros: good scalability, and changes to the program code is not very big 

drawback: the effect of this method than the second to almost, query performance is not high

Reference: http: //blog.51yip.com/mysql/949.html

3. Cluster

MySQL Proxy is such an intermediate layer proxy, simply put, MySQL Proxy is a connection pool, responsible for forwarding the connection request the foreground application to the backend database, and by using lua script, you can implement complex connection control and filtering, in order to achieve separate read and write and load 
balancing. For applications, MySQL Proxy is completely transparent to the application only needs to connect to the listening port to MySQL Proxy. Of course, such a machine could be proxy single point of failure, but it can use the machine as a plurality of redundant proxy, the connection pool configuration application server 
configured to connect the plurality of parameters to the proxy.

Reference: http: //www.cnblogs.com/phpstudy2015-6/p/6706465.html

        http://blog.51yip.com/mysql/399.html

Guess you like

Origin www.cnblogs.com/sz-xioabai/p/11666952.html