MySQL 5.5 partitioning-truncate partitioning

Source: Internet
Author: User
Tags table definition

Last time we introduced:For MySQL 5.5Partitions: partitions in non-integer columns. This section describes partitions in MySQL 5.5.Truncate PartitionNext, let's take a look at this part.

Enhanced availability: truncate partitions

One of the most attractive features of partitioning is the ability to instantly remove a large number of records. DBAs like to store historical records in partition tables partitioned by date, so that they can regularly Delete outdated historical data, this method is quite useful. If the first partition stores the oldest history records, you can directly Delete the first partition and create a new partition at the end to save the latest history records, in this way, you can quickly clear historical records.

However, when you need to remove part of the data in the partition, it is not that simple. There is no problem in deleting the partition, but it is a headache if you are clearing the partition, to remove all data from a partition, but keep the partition itself, you can:

The DELETE statement is used, but we know that the performance of the DELETE statement is poor.

The drop partition statement is followed by an eorganize partitions statement to re-create a PARTITION, but this is much more costly than the previous method.

MySQL 5.5 introduces truncate partition, which is similar to the drop partition statement, but retains the PARTITION itself, which means that the PARTITION can be reused. Truncate partition should be a required tool in the DBA toolbox.

Fine-tuning function: TO_SECONDS

The partition enhancement package has a new function to process the DATE and DATETIME columns. Using the TO_SECONDS function, you can convert the DATE/time column to the number of seconds since 0 years, if you want to partition at intervals less than one day, this function can help you.

TO_SECONDS triggers partition trimming. Unlike TO_DAYS, TO_SECONDS can be used in turn, that is, FROM_DAYS. TO_SECONDS does not have such inverse functions, but it is not difficult to do DIY by yourself.

 
 
  1. drop function if exists from_seconds;    
  2. delimiter //    
  3. create function from_seconds (secs bigint)    
  4. returns DATETIME    
  5. begin   
  6.     declare days INT;    
  7.     declare secs_per_day INT;    
  8.     DECLARE ZH INT;    
  9.     DECLARE ZM INT;    
  10.     DECLARE ZS INT;    
  11.     set secs_per_day = 60 * 60 * 24;    
  12.     set days = floor(secs / secs_per_day);    
  13.     set secssecs = secs - (secs_per_day * days);    
  14.     set ZH = floor(secs / 3600);    
  15.     set ZM = floor(secs / 60) - ZH * 60;    
  16.     set ZS = secs - (ZH * 3600 + ZM * 60);    
  17.     return CAST(CONCAT(FROM_DAYS(days), ' ', ZH, ':', ZM, ':', ZS) as DATETIME);    
  18. end //    
  19. delimiter ;  

With these new weapons, we can confidently create a temporary partition less than one day, such:

 
 
  1. CREATE TABLE t2 (    
  2.   dt datetime    
  3. )    
  4. PARTITION BY RANGE (to_seconds(dt))    
  5. (    
  6.   PARTITION p01 VALUES LESS THAN (to_seconds('2009-11-30 08:00:00')) ,    
  7.   PARTITION p02 VALUES LESS THAN (to_seconds('2009-11-30 16:00:00')) ,    
  8.   PARTITION p03 VALUES LESS THAN (to_seconds('2009-12-01 00:00:00')) ,    
  9.   PARTITION p04 VALUES LESS THAN (to_seconds('2009-12-01 08:00:00')) ,    
  10.   PARTITION p05 VALUES LESS THAN (to_seconds('2009-12-01 16:00:00')) ,    
  11.   PARTITION p06 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` datetime DEFAULT NULL   
  18. ) ENGINE=MyISAM DEFAULT CHARSET=latin1    
  19. /*!50500 PARTITION BY RANGE (to_seconds(dt))    
  20. (PARTITION p01 VALUES LESS THAN (63426787200) ENGINE = MyISAM,    
  21. PARTITION p02 VALUES LESS THAN (63426816000) ENGINE = MyISAM,    
  22. PARTITION p03 VALUES LESS THAN (63426844800) ENGINE = MyISAM,    
  23. PARTITION p04 VALUES LESS THAN (63426873600) ENGINE = MyISAM,    
  24. PARTITION p05 VALUES LESS THAN (63426902400) ENGINE = MyISAM,    
  25. PARTITION p06 VALUES LESS THAN MAXVALUE ENGINE = MyISAM) */  

Because we do not use the COLUMNS keyword, we cannot use it because it does not support mixed COLUMNS and functions. The record value in the table definition is the calculation result of the TO_SECONDS function.

But we still want to thank the new function. We can reverse this value and convert it into a date that is easier to understand.

 
 
  1. select   
  2.   partition_name part,    
  3.   partition_expression expr,    
  4.   from_seconds(partition_description) descr,    
  5.   table_rows    
  6. FROM   
  7. INFORMATION_SCHEMA.partitions    
  8. WHERE   
  9.     TABLE_SCHEMA = 'test'   
  10.     AND TABLE_NAME='t2';    
  11. +------+----------------+---------------------+------------+    
  12. | part | expr           | descr               | table_rows |    
  13. +------+----------------+---------------------+------------+    
  14. | p01  | to_seconds(dt) | 2009-11-30 08:00:00 |          0 |    
  15. | p02  | to_seconds(dt) | 2009-11-30 16:00:00 |          0 |    
  16. | p03  | to_seconds(dt) | 2009-12-01 00:00:00 |          0 |    
  17. | p04  | to_seconds(dt) | 2009-12-01 08:00:00 |          0 |    
  18. | p05  | to_seconds(dt) | 2009-12-01 16:00:00 |          0 |    
  19. | p06  | to_seconds(dt) | 0000-00-00 00:00:00 |          0 |    
  20. +------+----------------+---------------------+------------+  

Summary:

MySQL 5.5 is definitely good news for partition users. Although it does not provide direct performance enhancement methods, if you evaluate performance based on the response time), it is easier to use enhancements, and the truncate partition command can save DBA a lot of time, sometimes for end users.

I will introduce the truncate partition of MySQL 5.5. I hope this introduction will be helpful to you!

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

Related Article

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.