MySQL的经典用法(四)----大幅度提高性能方案,分区表

下面就笔者工作中遇到的时间问题,模拟一下情况对大家进行介绍.
    工作中仍然是海量数据出现的情况.每年大概会有几亿条记录.而且数据的时效性比较强.但历史数据仍然要求保留.这个时候经过分析和研究,最终决定通过时间字段进行分区.下面是分区表的创建代码.读者门在插入了不同年份时间段以后,可以时间字段为条件进行查询,可以看到数据库扫描过的区段.
Sql代码
CREATE TABLE part_data  
      (c1 int default NULL,  
  c2 varchar(30) default NULL,  
  c3 date default NULL)  
  partition by range (to_days(c3))  
(PARTITION p0 VALUES LESS THAN (to_days('1995-01-01')),  
PARTITION p1 VALUES LESS THAN (to_days('1996-01-01')) ,  
PARTITION p2 VALUES LESS THAN (to_days('1997-01-01')) ,  
PARTITION p3 VALUES LESS THAN (to_days('1998-01-01')) ,  
PARTITION p4 VALUES LESS THAN (to_days('1999-01-01')) ,  
PARTITION p5 VALUES LESS THAN (to_days('2000-01-01')) ,  
PARTITION p6 VALUES LESS THAN (to_days('2001-01-01')) ,  
PARTITION p7 VALUES LESS THAN (to_days('2002-01-01')) ,  
PARTITION p8 VALUES LESS THAN (to_days('2003-01-01')) ,  
PARTITION p9 VALUES LESS THAN (to_days('2004-01-01')) ,  
PARTITION p10 VALUES LESS THAN (to_days('2010-01-01')),  
PARTITION p11 VALUES LESS THAN MAXVALUE ); 

CREATE TABLE part_data
      (c1 int default NULL,
  c2 varchar(30) default NULL,
  c3 date default NULL)
  partition by range (to_days(c3))
(PARTITION p0 VALUES LESS THAN (to_days('1995-01-01')),
PARTITION p1 VALUES LESS THAN (to_days('1996-01-01')) ,
PARTITION p2 VALUES LESS THAN (to_days('1997-01-01')) ,
PARTITION p3 VALUES LESS THAN (to_days('1998-01-01')) ,
PARTITION p4 VALUES LESS THAN (to_days('1999-01-01')) ,
PARTITION p5 VALUES LESS THAN (to_days('2000-01-01')) ,
PARTITION p6 VALUES LESS THAN (to_days('2001-01-01')) ,
PARTITION p7 VALUES LESS THAN (to_days('2002-01-01')) ,
PARTITION p8 VALUES LESS THAN (to_days('2003-01-01')) ,
PARTITION p9 VALUES LESS THAN (to_days('2004-01-01')) ,
PARTITION p10 VALUES LESS THAN (to_days('2010-01-01')),
PARTITION p11 VALUES LESS THAN MAXVALUE );

分区查询,可以查看扫描过的区段.当然要加上where子句,以c3时间为条件进行检索.若不使用时间字段,分区将失去作用.
Sql代码
explain partitions select * from part_data\G 

你可能感兴趣的:(sql,mysql,工作)