14. mysql Partition HASH && KEY

Source: Internet
Author: User

1. Hash partition PS:: Personally think that the hash partition is very good and powerful, simple and accurate distribution extremely evenly created instances:CREATE TABLEhash_emp (Tidint, TnameChar(255)) PARTITION byHASH (TID) partitions8; The TID for hash_emp is hashed and divided into 8 regions to query partition data distribution:SelectPartition_name,partition_expression,partition_description,table_rows fromInformation_schema.partitionswhereTable_schema= Schema() andtable_name= 'hash_emp'; +----------------+----------------------+-----------------------+------------+    |Partition_name|Partition_expression|Partition_description|Table_rows|    +----------------+----------------------+-----------------------+------------+    |P0|Tid| NULL                  |         0 |     |P1|Tid| NULL                  |         0 |     |P2|Tid| NULL                  |         0 |     |P3|Tid| NULL                  |         0 |     |P4|Tid| NULL                  |         0 |     |P5|Tid| NULL                  |         0 |     |P6|Tid| NULL                  |         0 |     |P7|Tid| NULL                  |         0 |     +----------------+----------------------+-----------------------+------------+Create 1 event to write data without interruption, test distribution:CreateEvent Hash_emp_event onScheduler every1Second doInsert  intoHash_empValues(NULL, now ()); SetGLOBAL Event_scheduler= 1;//turn on the scheduler to see the distribution of partition data again:+----------------+----------------------+-----------------------+------------+    |Partition_name|Partition_expression|Partition_description|Table_rows|    +----------------+----------------------+-----------------------+------------+    |P0|Tid| NULL                  |          A |     |P1|Tid| NULL                  |          the |     |P2|Tid| NULL                  |          the |     |P3|Tid| NULL                  |          the |     |P4|Tid| NULL                  |          the |     |P5|Tid| NULL                  |          the |     |P6|Tid| NULL                  |          the |     |P7|Tid| NULL                  |          the |     +----------------+----------------------+-----------------------+------------+It can be seen that the hash distribution is extremely uniform:;2. Key partition PS:: The so-called key partition refers to MySQL by default using the table's primary key or unique partition management to create an instance:CREATE TABLEkey_emp (Tidint, TnameChar(255)) PARTITION by KEY(TID) Partitions8; PS: Because it is similar to hash, it is not tested too much!!!3. Sub-partition PS:: As the name suggests is to re-build the partition on the partition PS:: Sub-partition support partition mode has a range||list, 2 of them can support a hash or list of sub-partitions to create an instance:CREATE TABLEzi_emp (Tidint, TnameChar(255)) PARTITION byRANGE (tid) subpartition byHASH (tid) subpartitions2(PARTITION p0ValuesLess Than (1990), PARTITION p1ValuesLess Than (2028), PARTITION p2ValuesLess than (MAXVALUE)); The zi_emp is divided into 3 range partitions, each partitioned into 2 sub-partitions, if so, with the following partition structure:+----------------+----------------------+-----------------------+------------+    |Partition_name|Partition_expression|Partition_description|Table_rows|    +----------------+----------------------+-----------------------+------------+    |P0|Tid| 1990                  |          0 |     |P0|Tid| 1990                  |          0 |     |P1|Tid| 2028                  |          0 |     |P1|Tid| 2028                  |          0 |     |P2|Tid|MAXVALUE|          0 |     |P2|Tid|MAXVALUE|          0 |     +----------------+----------------------+-----------------------+------------+That is , if the TID is less than 1990, then the data will be hashed into the 2 sub-partitions of the P0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.