bitscn.com
rt不用多说,同行一看就知道!
mysql分区有很多种,这里我只讨论按时间——按天分区!
在操作之前,你仍然有必要执行如下命令,查看你的mysql版本是否支持partition功能:
sql代码
show variables like '%partition%'
如果系统输出如下结果,那么恭喜你,mysql支持分区!
sql代码
variable_name value
have_partitioning yes
sql代码
create table quarterly_report_status (
report_id int not null,
report_status varchar(20) not null,
report_updated timestamp not null default current_timestamp on update current_timestamp
)
partition by range ( unix_timestamp(report_updated) ) (
partition p0 values less than ( unix_timestamp('2011-01-01 00:00:00') ),
partition p1 values less than ( unix_timestamp('2011-02-01 00:00:00') ),
partition p2 values less than ( unix_timestamp('2011-03-01 00:00:00') ),
partition p3 values less than ( unix_timestamp('2011-04-01 00:00:00') ),
partition p4 values less than ( unix_timestamp('2011-05-01 00:00:00') ),
partition p5 values less than ( unix_timestamp('2011-06-01 00:00:00') ),
partition p6 values less than ( unix_timestamp('2011-07-01 00:00:00') ),
partition p7 values less than ( unix_timestamp('2011-08-01 00:00:00') ),
partition p8 values less than ( unix_timestamp('2011-09-01 00:00:00') ),
partition p9 values less than (maxvalue)
);
官方文档说从mysql 5.1.43开始,除了timestamp 外,其他日期类型都不接受!
偶虽然没有尝试,至于你信不信,反正我是信了!
explain partitions sql测试,看看是否有效:
sql代码
explain partitions select * from quarterly_report_status q where q.report_updated 输出:
| id | select_type | table | partitions | type | possible_keys | key | key_len| ref | rows | extra |
| 1 | simple | q | p0,p1 | all | null | null | null | null | 2 | using where |
1 row in set (0.00 sec)
非常棒,完美!
————————————————————————————————————————————————————
【转】相关函数:from_unixtime(date)
date为需要处理的参数(该参数是unix 时间戳),可以是字段名,也可以直接是unix 时间戳字符串
后面的 '%y%m%d' 主要是将返回值格式化(可有可无)
例如:
mysql>select from_unixtime( 1249488000, '%y%m%d' )
->20071120
mysql>select from_unixtime( 1249488000, '%y年%m月%d' )
->2007年11月20
unix_timestamp() 是与之相对正好相反的时间函数
unix_timestamp(), unix_timestamp(date)
若无参数调用,则返回一个 unix timestamp ('1970-01-01 00:00:00' gmt 之后的秒数) 作为无符号整数。若用date 来调用 unix_timestamp(),它会将参数值以'1970-01-01 00:00:00' gmt后的秒数的形式返回。date 可以是一个 date 字符串、一个 datetime字符串、一个 timestamp或一个当地时间的yymmdd 或yyymmdd格式的数字。
例如:
mysql> select unix_timestamp() ; (执行使得时间:2009-08-06 10:10:40)
->1249524739
mysql> select unix_timestamp('2009-08-06') ;
->1249488000
select * from `student` where regtime > unix_timestamp( curdate( ) )
作者“极客先生”
bitscn.com