mysql查询系统年月_mysql每个月自动创建一张表,以年月做为表名,如何进行联合查询...
展开全部
我正好有楼主类似的需求,每个季e5a48de588b63231313335323631343130323136353331333335343362度为几个表增加一个分区,表的基本名称是在一个叫设备类型的表里,每天计划执行一个过程,在过程里从系统表中判断是否已经创建了相关的分区,如果没创建就创建它
楼主可以参考一下,记得在my.ini 文件里配置event_scheduler=on/**
定时每天检查各个设备类型的历史数据表,如果历史数据表的所在分区已经
接近当前日期,则为此设备类型追加分区
*/
-- 得到按月分区的日期值
delimiter ;
drop function if exists fnGetPartitionDateForMonth;
delimiter ;;
create function fnGetPartitionDateForMonth() returns INT
begin
declare v_today datetime default date_add(now(), INTERVAL 2 month);
return year(v_today) * 100 + month(v_today);
end;;
-- 得到按季度分区的日期值
delimiter ;
drop function if exists fnGetPartitionDateForQuarter;
delimiter ;;
create function fnGetPartitionDateForQuarter() returns int
begin
declare v_today datetime default date_add(now(), interval 3 month);
declare v_month int;
set v_month = month(v_today);
if v_month = 1 or v_month = 2 or v_month = 3 then
set v_today = DATE_ADD(v_today, INTERVAL (4 - v_month) month);
elseif v_month = 4 or v_month = 5 or v_month = 6 THEN
set v_today = DATE_ADD(v_today, INTERVAL (7 - v_month) month);
elseif v_month = 7 or v_month = 8 or v_month = 9 THEN
set v_today = date_add(v_today, INTERVAL (10 - v_month) month);
ELSE
set v_today = date_add(v_today, INTERVAL (13 - v_month) month);
end if;
return year(v_today) * 100 + month(v_today);
end;;
-- 得到按半年分区的日期值
delimiter ;
drop function if exists fnGetPartitionDateForHalfYear;
delimiter ;;
create function fnGetPartitionDateForHalfYear() returns int
begin
declare v_today datetime default date_add(now(), interval 6 month);
declare v_month int;
set v_month = month(v_today);
if v_month <= 6 THEN
set v_today = date_add(v_today, INTERVAL (7 - v_month) month);
else
set v_today = DATE_ADD(v_today, INTERVAL (13 - v_month) month);
end if;
return year(v_today) * 100 + month(v_today);
end;;
-- 维护按年分区
delimiter ;
drop function if exists fnGetPartitionDateForYear;
delimiter ;;
create function fnGetPartitionDateForYear() returns int
begin
declare v_today datetime default date_add(now(), INTERVAL 2 year);
return year(v_today) * 100;
end;;
delimiter ;
drop procedure if exists spMaintainPartitions;
delimiter ;;
create procedure spMaintainPartitions()
BEGIN
declare v_sql varchar(2000);
declare v_cnt int;
declare v_deviceTypeId int;
declare v_tablename varchar(50);
declare v_tablename_analog varchar(50);
declare v_tablename_digital varchar(50);
declare v_partitionType int;
declare v_fileDir varchar(1000);
declare v_tablenames varchar(1000) default '';
declare v_intDate int;
declare v_partitionName varchar(100);
declare done int default 0;
declare c_deviceType cursor
for select Id, TableName, PartitionType, DataFileDir
from tbDeviceType
where Generated = 1;
declare continue handler for not found set done = 1;
insert into tbPartitionMaintainLog(`CreatedDateTime`, `LogContent`)
Values(Now(), 'spMaintainPartitions start......');
open c_deviceType;
deviceType_loop: LOOP
fetch c_deviceType into v_deviceTypeId, v_tablename, v_partitionType, v_fileDir;
set v_fileDir = replace(v_fileDir, '\\', '/');
if locate(':', v_fileDir) > 0 and locate(':/', v_fileDir) = 0 then
set v_fileDir = replace(v_fileDir, ':', ':/');
end if;
if done = 1 then
leave deviceType_loop;
end if;
set v_intDate = null;
if v_partitionType = 1 then
set v_intDate = fnGetPartitionDateForMonth();
ELSEIF v_partitionType = 2 THEN
set v_intDate = fnGetPartitionDateForQuarter();
ELSEIF v_partitionType = 3 then
set v_intDate = fnGetPartitionDateForHalfYear();
elseif v_partitionType = 4 then
set v_intDate = fnGetPartitionDateForYear();
end if;
if v_intDate is null then
insert into tbPartitionMaintainLog(`CreatedDateTime`, `LogContent`)
values(Now(), Concat('DeviceTypeId = ', cast(v_deviceTypeId As char(10)), ' did not define paritition schedule'));
else
set v_partitionName = concat('p', cast(v_intDate as char(6)));
-- 模拟量表
set v_tablename_analog = concat(v_tablename, '_Analog');
select count(*) into v_cnt
from information_schema.`TABLES` where `TABLE_SCHEMA` = database() and `table_name` = v_tablename_analog;
if v_cnt > 0 then
select count(*) into v_cnt
from
information_schema.`PARTITIONS`
where
TABLE_SCHEMA = database() and table_name = v_tablename_analog and partition_name = v_partitionName;
if v_cnt = 0 then
set v_sql = CONCAT('alter table ', v_tablename_analog, ' add partition (partition ', v_partitionName, ' values less than (', cast(v_intDate as char(6)), ') data directory = ''', v_fileDir, ''' index directory = ''', v_fileDir , ''')');
insert into tbPartitionMaintainLog(`CreatedDateTime`, `LogContent`)
Values(Now(), concat('sql = ''', v_sql));
set @sql = v_sql;
prepare cmd from @sql;
execute cmd;
deallocate prepare cmd;
insert into tbPartitionMaintainLog(`CreatedDateTime`, `LogContent`)
values(Now(), concat('execute complete: ', v_sql));
end if;
end if;
-- 数字量表
set v_tablename_digital = concat(v_tablename, '_Digital');
select count(*) into v_cnt
from information_schema.`TABLES` where `TABLE_SCHEMA` = database() and `table_name` = v_tablename_digital;
if v_cnt > 0 then
select count(*) into v_cnt
from
information_schema.`PARTITIONS`
where
TABLE_SCHEMA = database() and table_name = v_tablename_digital and partition_name = v_partitionName;
if v_cnt = 0 then
set v_sql = CONCAT('alter table ', v_tablename_digital, ' add partition (partition ', v_partitionName, ' values less than (', cast(v_intDate as char(6)), ') data directory = ''', v_fileDir, ''' index directory = ''', v_fileDir , ''')');
insert into tbPartitionMaintainLog(`CreatedDateTime`, `LogContent`)
Values(Now(), concat('sql = ''', v_sql));
set @sql = v_sql;
prepare cmd from @sql;
execute cmd;
deallocate prepare cmd;
insert into tbPartitionMaintainLog(`CreatedDateTime`, `LogContent`)
values(Now(), concat('execute complete: ', v_sql));
end if;
end if;
end if;
end loop deviceType_loop;
close c_deviceType;
END;;
delimiter ;
drop event if exists e_DataPartitionMaintain;
create event e_DataPartitionMaintain
on SCHEDULE every 60 Second
on completion PRESERVE
do call spMaintainPartitions();
set global event_scheduler = on;
mysql查询系统年月_mysql每个月自动创建一张表,以年月做为表名,如何进行联合查询...相关推荐
- mysql数据库用户建立_Mysql数据库之创建用户
注:在Mysql数据库中SQL语句默认是以":"结尾.输入命令时,关键字可以大写也可以小写,但必须保持一致,本人比较倾向使用小写,个人习惯吧. 在Mysql中创建数据库用户主要有三 ...
- MySQL - 表的增删改查(约束+聚合、联合查询)
本节目标: 数据库约束 表的关系 新增: 删除 修改 查询 1.数据库约束 1.1约束类型 NOT NULL -指示某列不能存储 NULL值. UNIQUE -保证某列的每行必须有唯一的值. DEFA ...
- MySQL 表的增删改查(进阶篇②)· 联合查询 内连接 外连接 · 自连接 · 子查询 exists · 合并查询 union
接进阶篇①,我们继续学习. 一.联合查询 1.1 内连接 1.2 外连接 1.3 内连接和左右外连接的区别 二.自连接 三.子查询 3.1 单行子查询 3.2 多行子查询 使用 in 范围匹配多行 另 ...
- mysql定时增量备份_Mysql日常自动备份和增量备份脚本
序 你是否在寻找一个MySQL备份脚本? 适合对象 本文是在Linux下,mysql 4.1.14版本下测试的,经过适当修改可能适合mysql 4.0,5.0及其其他版本. 本文适合于没有启动复制功能 ...
- mysql备份脚本+关_mysql数据库自动定期备份的脚本
mysql数据库自动定期备份的脚本 说明: 保留每天备份的数据是件不太现实的事,做好的做法是保留前三天的 备份数据.把备份的数据打包并压缩,文件名以系统时间命名,打包后的 备份文件放在一个特定的文件夹 ...
- mysql 超长 java 问题_MySql超长自动截断实例详解
MySql超长自动截断实例详解 小伙伴问到一个问题,为啥在项目中调用插入或者更新语句时超长的字无法自动截断,而在navicat中直接执行是可以自动截断的? 如下 CREATE TABLE `p_app ...
- mysql timestamp 更新操作_MySQL timestamp自动更新时间
在mysql中timestamp数据类型是一个比较特殊的数据类型,他可以自动在你不使用程序更新情况下,插入或者修改记录会自动更新timestamp的值. 1.ON UPDATE CURRENT_TIM ...
- c 创建mysql实体模型_EntityFrameworkCore 根据实体类自动创建数据库
1.首先新建 Asp.Net Core WebApi 项目 2.添加一下引用 : 2.1 Pomelo.EntityFrameworkCore.MySql(我用的Mysql 根据自己情况引用就行) ...
- mysql超过1W条查询不显示_mysql 被写入1W数据后无法select*from table,但是加入limit参数后可以查询...
数据库表结构 innodb SELECT 查询错误提示 ERROR 2013 (HY000): Lost connection to MySQL server during query 我觉得可能是 ...
最新文章
- 怎么使用CorelDRAW 中的默认调色板
- 多线程实现的二种方式
- 如何自学python爬虫-Python爬虫:零基础该如何学习爬虫
- 笔记-项目管理基础知识-项目管理办公室(PMO)
- java 找出list中相同数据_Java获取List中相同的数据
- 如何找到SAP CRM WebClient UI error message的来源
- 访问 GitHub 的速度很慢?试试这几种方法
- HDU 4990 Ordered Subsequence --数据结构优化DP
- cmd imp导入dmp文件_cmd 导入oracle数据的dmp文件
- C++之禁用特定告警
- C#中如何获取一个二维数组的两维长度,即行数和列数?
- 多通道ECG心率监测系统
- 小象学院python网课值得吗-2018最新小象学院Python数据分析视频教程升级版第2期...
- 非线性最小二乘求解方法总结
- 给zabbix更换nagios图标
- Mybatis什么时候用resultMap,什么时候用resultType
- pikachu漏洞平台通关系列导览(所有关卡已完结)
- 前后端对接及接口管理平台浅析
- 大学四年·写于离校前
- 最新公布!“中国开发者大调查”第二批中奖名单出炉啦
热门文章
- 亚马逊aws深度学习_AWS速查表:Amazon Web Services入门时首先要学习的5件事
- ansj 自定义 停用词_构造自定义停用词列表的快速提示
- ibm cloud怎么使用_使用VueJS,FeathersJS和GraphQL快速入门IBM Cloud
- 没有事情,错误1503_为什么依靠用户报告错误是您做过的最愚蠢的事情
- 接口样板_完整的AWS Web样板
- python读取xlsx文件年月日变成数字_python转化excel数字日期为标准日期操作
- 基于Smadja算法的搭配词自动提取实践
- Linux01-学习记录
- Maven中dependencyManagement标签和dependencies的区别
- numpy.tile作用,语法,参数分析以及举例