使用变量的unix_时间戳进行mysql分区

使用变量的unix_时间戳进行mysql分区,mysql,database-partitioning,Mysql,Database Partitioning,鉴于此: delimiter // create procedure setup() begin declare d datetime; set d = rounddate(now()); create table s_time (req_id int not null, ser_id int not null, hel_id int not null,

鉴于此:

delimiter //
create procedure setup()
begin
  declare d datetime;
  set d = rounddate(now());

  create table s_time (req_id int not null,
                       ser_id int not null,
                       hel_id int not null,
                       posted int unsigned not null,
                       completed int unsigned not null default 0
                      )
  partition by range (completed) (partition p0 values less than ( unix_timestamp(d) ),
                                  partition p1 values less than ( unix_timestamp(d + interval 1 day) )
                                 );
end//
我得到:

错误1064(42000):在(子)分区函数中不允许使用常数、随机或时区相关表达式


有没有办法让它工作,或者我必须使用硬编码字符串作为输入。ie用法:
unix\u timestamp('2012-07-07 00:00:00')

不起作用的原因不太清楚,我怀疑文档或错误消息中存在错误。在我看来,你收到的错误是不恰当的

分区中不允许使用以下构造 表达方式:

  • 存储过程、存储函数、UDF或插件
  • 声明变量或用户变量
这解释了表定义失败的原因

现在,如何去掉变量?如果尝试从分区定义中删除声明的变量:

创建表s\u时间(
已完成整型无符号非空默认值0
)
按范围划分(已完成)(
分区p0值小于(UNIX_TIMESTAMP())
);
您将得到相同的错误。然而,缔约国:

也可以在小于子句的值中使用表达式。 但是,MySQL必须能够计算表达式的返回值
作为一个小于(的解决方案的一部分,我发现了这一点

delimiter //
create procedure setup()
begin
  declare d, d2 int;
  set d = unix_timestamp();
  set d2 = unix_timestamp(now() + interval 1 day);

  create table s_time (req_id int not null,
                       ser_id int not null,
                       hel_id int not null,
                       posted int unsigned not null,
                       completed int unsigned not null default 0
                      );

  SET @stmt = concat('alter table s_time PARTITION BY RANGE (completed) (
                      partition p0 values less than (', d, '),
                      partition p1 values less than (', d2, '))');
  PREPARE pStmt FROM @stmt;
  EXECUTE pStmt;
  DEALLOCATE PREPARE pStmt;

end//
delimiter ;
call setup();

您的
s_time
表是否应该在您的过程结束后继续存在?另外,您是否希望分区自动重新组织?换句话说,现在,
p0
分区包含到目前为止的所有记录。从现在起24小时内,
p0
是否将包含到“从现在起24小时内”为止的所有记录?此过程只是自动设置表。该表将用作滚动分区。24小时后,第一个分区将被删除,另一个分区将被添加。我想知道是否有办法不硬编码前两个分区。我想我可以制作另一个过程来执行前两个初始滚动,并在t之后调用他用硬编码的值创建表的一部分。