mysql的分区表

server/2024/9/22 16:25:02/

1.SQL表创建

下面以时间范围进行创建(每月一个分区,表中创建了四个月的分区)

创建:CREATE TABLE test_table (  id INT NOT NULL AUTO_INCREMENT,  content VARCHAR(255),  create_time DATETIME NOT NULL,PRIMARY KEY (id, create_time) 
) PARTITION BY RANGE (TO_DAYS(create_time)) (  PARTITION p20240601 VALUES LESS THAN (TO_DAYS('2024-06-01')),  PARTITION p20240701 VALUES LESS THAN (TO_DAYS('2024-07-01')),  PARTITION p20241801 VALUES LESS THAN (TO_DAYS('2024-08-01')),  PARTITION p20240901 VALUES LESS THAN (TO_DAYS('2024-09-01'))
);  查询分区详情:
SELECT *
FROM INFORMATION_SCHEMA.PARTITIONS 
WHERE TABLE_NAME = 'test_table';

2、mapper文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="*.infrastructure.mapper.TestTableMapper"><resultMap id="TestTable" type="*.domain.entity.TestTable"><id column="id" property="id" typeHandler="org.apache.ibatis.type.LongTypeHandler"/><result property="content" column="content" jdbcType="VARCHAR"/><result property="createTime" column="create_time" jdbcType="TIMESTAMP"typeHandler="org.apache.ibatis.type.LocalDateTimeTypeHandler"/></resultMap><!-- 创建新分区 --><update id="createNewPartition">ALTER TABLE TEST_TABLEADD PARTITION (  PARTITION ${partitionName} VALUES LESS THAN (TO_DAYS(#{lessThanValue})))</update><!-- 删除旧分区 --><update id="dropPartition">ALTER TABLE TEST_TABLEDROPPARTITION${partitionName}</update><!--查询是否存在分区--><select id="exitsPartition" resultType="boolean">SELECT COUNT(1) > 0FROM INFORMATION_SCHEMA.PARTITIONSWHERE TABLE_NAME = 'TEST_TABLE'AND PARTITION_NAME = #{partitionName}</select></mapper>

3、service

package *.domain.service;import *.domain.entity.TestTable;
import *.infrastructure.repo.TestTableRepository;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Random;@Service
public class TestTableService {@AutowiredTestTableRepository repository;// 插入数据,如果分区不存在,就创建分区,重新插入public void insert() {TestTable testTable = new TestTable();testTable.setContent("test");Random random = new Random();int i = Math.abs(random.nextInt()) % 365;LocalDateTime dateTime = LocalDateTime.now().minusDays(i);testTable.setCreateTime(dateTime);try {repository.getBaseMapper().insert(testTable);} catch (DataAccessException e) {LocalDate nextMonthFirstDay = YearMonth.from(dateTime).plusMonths(1).atDay(1);String lessThanValue = nextMonthFirstDay.format(DateTimeFormatter.ISO_DATE);String partitionName = "p" + lessThanValue.replaceAll("-", "");// 创建分区时加锁,如果是多节点,需要分布式锁synchronized (this) {if (!repository.getBaseMapper().exitsPartition(partitionName)) {repository.getBaseMapper().createNewPartition(partitionName, lessThanValue);}}repository.getBaseMapper().insert(testTable);}}// 创建分区public void createNewPartition(String partitionName, String lessThanValue) {repository.getBaseMapper().createNewPartition(partitionName, lessThanValue);}// 删除分区public void dropPartition(String partitionName) {repository.getBaseMapper().dropPartition(partitionName);}
}


http://www.ppmy.cn/server/118271.html

相关文章

Flask中的钩子函数

在Flask中&#xff0c;钩子函数&#xff08;Hook Functions&#xff09;或称为回调函数&#xff08;Callback Functions&#xff09;是特殊的函数&#xff0c;它们在Flask的请求处理流程中的特定点被自动调用。这些钩子函数允许你在请求被处理之前或之后、视图函数执行之前或之…

9.11-kubeadm方式安装k8s

一、安装环境 编号主机名称ip地址1k8s-master192.168.2.662k8s-node01192.168.2.773k8s-node02192.168.2.88 二、前期准备 1.设置免密登录 [rootk8s-master ~]# ssh-keygen [rootk8s-master ~]# ssh-copy-id root192.168.2.77 [rootk8s-master ~]# ssh-copy-id root192.168…

P2P应用

当谈论P2P&#xff08;点对点&#xff09;应用程序时&#xff0c;我们实际上是在讨论一种网络架构和通信模式&#xff0c;它允许设备&#xff08;或节点&#xff09;直接连接并共享资源&#xff0c;而无需传统的客户端-服务器模型。P2P应用程序在许多领域都有广泛的应用&#x…

RocketMQ 消费方式

在消息传递系统中&#xff0c;“推&#xff08;Push&#xff09;”和“拉&#xff08;Pull&#xff09;”是两种不同的消息消费方式&#xff0c;RocketMQ 也支持这两种模式。下面是对这两种模式的详细解释&#xff1a; 1. 推模式&#xff08;Push Model&#xff09; 模式简介…

少儿编程小游戏 | Scratch 火柴人团队冒险:重装上阵

在线玩&#xff1a;Scratch多人游戏-《火柴人团队冒险&#xff1a;重装上阵》免费下载-小虎鲸Scratch资源站 在少儿编程的世界中&#xff0c;团队合作和冒险精神是激发孩子创造力的重要元素。今天我们将为大家介绍一款Scratch平台上的经典少儿编程小游戏——《火柴人团队冒险&a…

Reactive 编程-Vert.x

Reactive 编程与 Vert.x&#xff1a;高效异步 Java 微服务框架 一、什么是 Reactive 编程&#xff1f; Reactive 编程是一种异步编程范式&#xff0c;专注于数据流和事件的传播处理。与传统的阻塞式编程不同&#xff0c;Reactive 编程能够更好地处理高并发和异步操作&#xf…

SSMP+ajax实现广告系统的分页效果

文章目录 1.案例需求2.编程思路3.案例源码4.小结 1.案例需求 使用SSMPajax实现广告系统的分页效果&#xff0c;效果图如下&#xff1a; 2.编程思路 mapper层&#xff1a;定义一个接口&#xff0c;继承自BaseMapper&#xff0c;指定泛型为AdvInfo&#xff0c;这样MyBatis Pl…

C语言——数组,指针,指针数组,数组指针

零、存储单元和地址 计算机在保存数据时&#xff0c;把数据放在一个个存储单元中&#xff0c;存储单元可以理解为一个个小房间。 地址就是存储单元&#xff08;小房间&#xff09;的房间号&#xff0c;且这个房间号是唯一的。 详细请学习计算机组成原理3.1 一、变量a int a…