Non-integer column partitions in MySQL 5.5

Source: Internet
Author: User

Last time we introduced:For MySQL 5.5Detailed description of multiple partition columns. This article describes how to partition MySQL 5.5Non-integer column PartitionNext, let's take a look at this part!

Non-integer column Partition

Anyone who uses too many partitions should have encountered many problems. Especially for non-integer column partitions, MySQL 5.1 can only process integer column partitions. If you want to partition on a date or string column, you have to use the function to convert it.

MySQL 5.5 adds two types of partitioning methods, RANG and LIST partitioning, and adds a COLUMNS keyword to the new function. Suppose we have a table like this:

 
 
  1. CREATE TABLE expenses (    
  2.   expense_date DATE NOT NULL,    
  3.   category VARCHAR(30),    
  4.   amount DECIMAL (10,3)    
  5. );  

If you want to use the partition type in MySQL 5.1, you must convert the type to an integer and use an additional table to search for it in MySQL 5.5, you do not need to convert the type, for example:

 
 
  1. ALTER TABLE expenses    
  2. PARTITION BY LIST COLUMNS (category)    
  3. (    
  4.   PARTITION p01 VALUES IN ( 'lodging', 'food'),    
  5.   PARTITION p02 VALUES IN ( 'flights', 'ground transportation'),    
  6.   PARTITION p03 VALUES IN ( 'leisure', 'customer entertainment'),    
  7.   PARTITION p04 VALUES IN ( 'communications'),    
  8.   PARTITION p05 VALUES IN ( 'fees')    
  9. );  

In addition to easier reading, such partition statements clearly organize and manage data. The preceding example only partitions the category column.

Another headache for using partitions in MySQL 5.1 is the date type, that is, date columns. You cannot use them directly. You must use YEAR or TO_DAYS to convert these columns, for example:

 
 
  1. /* In MySQL 5.1 */
  2. Create table t2
  3. (
  4. Dt DATE
  5. )
  6. Partition by range (TO_DAYS (dt ))
  7. (
  8. PARTITION p01 values less than (TO_DAYS ('2017-01-01 ')),
  9. PARTITION p02 values less than (TO_DAYS ('2017-01-01 ')),
  10. PARTITION p03 values less than (TO_DAYS ('2017-01-01 ')),
  11. PARTITION p04 values less than (MAXVALUE ));
  12.  
  13. Show create table t2 \ G
  14. * *************************** 1. row ***************************
  15. Table: t2
  16. Create Table: create table 't2 '(
  17. 'Dt' date DEFAULT NULL
  18. ) ENGINE = MyISAM default charset = latin1
  19. /*! 50100 partition by range (TO_DAYS (dt ))
  20. (PARTITION p01 values less than (733042) ENGINE = MyISAM,
  21. PARTITION p02 values less than (733407) ENGINE = MyISAM,
  22. PARTITION p03 values less than (733773) ENGINE = MyISAM,
  23. PARTITION p04 values less than maxvalue engine = MyISAM )*/

It looks terrible. Of course there are some alternatives, but there are indeed a lot of troubles. It is confusing to define a partition using YEAR or TO_DAYS. You have to use bare columns when querying, because the query with a function cannot identify partitions.

But in MySQL 5.5, the situation has changed a lot. Now we can partition directly in the date column, and the method is also very simple.

 
 
  1. /* In MySQL 5.5 */
  2. Create table t2
  3. (
  4. Dt DATE
  5. )
  6. Partition by range columns (dt)
  7. (
  8. PARTITION p01 values less than ('2017-01-01 '),
  9. PARTITION p02 values less than ('2017-01-01 '),
  10. PARTITION p03 values less than ('2017-01-01 '),
  11. PARTITION p04 values less than (MAXVALUE ));
  12.  
  13. Show create table t2 \ G
  14. * *************************** 1. row ***************************
  15. Table: t2
  16. Create Table: create table 't2 '(
  17. 'Dt' date DEFAULT NULL
  18. ) ENGINE = MyISAM default charset = latin1
  19. /*! 50500 partition by range columns (dt)
  20. (PARTITION p01 values less than ('1970-01-01 ') ENGINE = MyISAM,
  21. PARTITION p02 values less than ('2017-01-01 ') ENGINE = MyISAM,
  22. PARTITION p03 values less than ('1970-01-01 ') ENGINE = MyISAM,
  23. PARTITION p04 values less than (MAXVALUE) ENGINE = MyISAM )*/

Here, there is no conflict between function definitions and column queries, because the values are defined by columns and the values we insert in the definitions are retained.

Here is an introduction to the non-integer column partition of MySQL 5.5. I hope this introduction will be helpful to you!

Source: http://lujia35.iteye.com/blog/718899.

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.