如何按自动增量和日期来组织分区?

问题描述 投票:0回答:1

我有一个在对手之间进行战斗的游戏。

关于战斗的信息存储在一张桌子中。有关战斗过程的信息存储在另一个表中。

[几天前的战斗信息并不需要任何人,而且必须便宜地销毁。我决定在分区和调度程序的帮助下进行此操作。

一切正常,但是解决方案本身仍然令人困惑。也许我发明了自行车,还有一个更优雅的解决方案。

请告诉他一个简单的解决方案。

谢谢。

战斗

CREATE TABLE `battle` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `attacker_id` int(11) NOT NULL DEFAULT 0,
  `defender_id` int(11) NOT NULL DEFAULT 0,
  ...
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=N DEFAULT CHARSET=utf8mb4
 PARTITION BY RANGE (`id`)
(PARTITION `d200412` VALUES LESS THAN (2004130000000000000) ENGINE = InnoDB,
 PARTITION `d200413` VALUES LESS THAN (2004140000000000000) ENGINE = InnoDB,
 PARTITION `d200414` VALUES LESS THAN (2004150000000000000) ENGINE = InnoDB)

战斗框架

CREATE TABLE `battle_data` (
   `battle_id` bigint(20) NOT NULL DEFAULT 0,
   `frame` tinyint(4) NOT NULL DEFAULT 0,
   `events` blob NOT NULL,
   `add` int(11) NOT NULL DEFAULT 0,
   PRIMARY KEY (`battle_id`,`frame`) USING BTREE
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
  PARTITION BY RANGE (`battle_id`)
 (PARTITION `d200412` VALUES LESS THAN (2004130000000000000) ENGINE = InnoDB,
  PARTITION `d200413` VALUES LESS THAN (2004140000000000000) ENGINE = InnoDB,
  PARTITION `d200414` VALUES LESS THAN (2004150000000000000) ENGINE = InnoDB)

计划程序

CREATE DEFINER=`main`@`%` EVENT `battle` ON SCHEDULE EVERY 1 DAY STARTS '2019-07-10 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN

SET @id_base = '%y%m%d';
SET @id_tail = '0000000000000';

SET @add_name = DATE_FORMAT(CURDATE(), @id_base);
SET @add_less = DATE_FORMAT(CURDATE() + INTERVAL 1 DAY, @id_base);

SET @drop_name = DATE_FORMAT(CURDATE() - INTERVAL 3 DAY, @id_base);

SET @sql = CONCAT('ALTER TABLE `battle` ADD PARTITION IF NOT EXISTS (PARTITION d', @add_name, ' VALUES LESS THAN (', @add_less, @id_tail, '))');
PREPARE query FROM @sql; EXECUTE query; DEALLOCATE PREPARE query;

SET @sql = CONCAT('ALTER TABLE `battle` DROP PARTITION IF EXISTS d', @drop_name);
PREPARE query FROM @sql; EXECUTE query; DEALLOCATE PREPARE query;

SET @sql = CONCAT('ALTER TABLE `battle_data` ADD PARTITION IF NOT EXISTS (PARTITION d', @add_name, ' VALUES LESS THAN (', @add_less, @id_tail, '))');
PREPARE query FROM @sql; EXECUTE query; DEALLOCATE PREPARE query;

SET @sql = CONCAT('ALTER TABLE `battle_data` DROP PARTITION IF EXISTS d', @drop_name);
PREPARE query FROM @sql; EXECUTE query; DEALLOCATE PREPARE query;

SET @sql = CONCAT('ALTER TABLE `battle` AUTO_INCREMENT = ', @add_name, @id_tail);
PREPARE query FROM @sql; EXECUTE query; DEALLOCATE PREPARE query;

END
mysql mariadb database-partitioning
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.