[zabbix]zabbix分区表操作步骤
Q&A:
1.mul key:
. 如果Key是空的, 那么该列值的可以重复, 表示该列没有索引, 或者是一个非唯一的复合索引的非前导列
. 如果Key是PRI, 那么该列是主键的组成部分
. 如果Key是UNI, 那么该列是一个唯一值索引的第一列(前导列),并别不能含有空值(NULL)
. 如果Key是MUL, 那么该列的值可以重复, 该列是一个非唯一索引的前导列(第一列)或者是一个唯一性索引的组成部分但是可以含有空值NULL
2.mysql分区表概述:
参考:
https://dev.mysql.com/doc/refman/5.1/zh/partitioning.html 分区表分为四种:
range分区:基于属于一个给定连续区间的列值,把多行分配给分区
list分区:类似于按RANGE分区,区别在于LIST分区是基于列值匹配一个离散值集合中的某个值来进行选择
hash分区:基于用户定义的表达式的返回值来进行选择的分区,该表达式使用将要插入到表中的这些行的列值进行计算
key分区:类似于按HASH分区,区别在于KEY分区只支持计算一列或多列
常用的是range和list分区 无论使用何种类型的分区,分区总是在创建时就自动的顺序编号,且从0开始记录,记住这一点非常重要。当有一新行插入到一个分区表中时,就是使用这些分区编号来识别正确的分区。 注1:
如果想在已经建好的表上进行分区,如果使用alter添加分区的话,mysql会提示错误:
ERROR <HY000> Partition management on a not partitioned table is not possible
正确的方法是新建一个具有分区的表,结构一致,然后用insert into 分区表 select * from 原始表;
http://blog.sina.com.cn/s/blog_5f54f0be01015tag.html
预计过程:
1.停止mysql,/etc/init.d/mysql stop
2.记录下要修改的表的表结构,
3.删除表,
4.创建和之前表结构一致但带有分区表的数据表
5.创建procedure,修改crontab
问题,修改之后,一段时间之前的数据会被删除,但这些数据会不会被别的表引用;
测试过程:
http://zabbixzone.com/zabbix/partitioning-tables/
.如果只做历史表的每日分割,删除如下表
history
history_uint
history_log
history_str
history_text 他们的创建语法是:
root@InnerServer1:/home/silence/zabbix-2.2./database/mysql# mysql -uroot -pfengmao -e 'show create table zabbix.history\G show create table zabbix.history_uint\G show create table zabbix.history_log\G show create table zabbix.history_str\G show create table zabbix.history_text\G '
*************************** . row ***************************
Table: history
Create Table: CREATE TABLE `history` (
`itemid` bigint() unsigned NOT NULL,
`clock` int() NOT NULL DEFAULT '',
`value` double(,) NOT NULL DEFAULT '0.0000',
`ns` int() NOT NULL DEFAULT '',
KEY `history_1` (`itemid`,`clock`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
*************************** . row ***************************
Table: history_uint
Create Table: CREATE TABLE `history_uint` (
`itemid` bigint() unsigned NOT NULL,
`clock` int() NOT NULL DEFAULT '',
`value` bigint() unsigned NOT NULL DEFAULT '',
`ns` int() NOT NULL DEFAULT '',
KEY `history_uint_1` (`itemid`,`clock`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
*************************** . row ***************************
Table: history_log
Create Table: CREATE TABLE `history_log` (
`id` bigint() unsigned NOT NULL,
`itemid` bigint() unsigned NOT NULL,
`clock` int() NOT NULL DEFAULT '',
`timestamp` int() NOT NULL DEFAULT '',
`source` varchar() COLLATE utf8_bin NOT NULL DEFAULT '',
`severity` int() NOT NULL DEFAULT '',
`value` text COLLATE utf8_bin NOT NULL,
`logeventid` int() NOT NULL DEFAULT '',
`ns` int() NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `history_log_2` (`itemid`,`id`),
KEY `history_log_1` (`itemid`,`clock`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
*************************** . row ***************************
Table: history_str
Create Table: CREATE TABLE `history_str` (
`itemid` bigint() unsigned NOT NULL,
`clock` int() NOT NULL DEFAULT '',
`value` varchar() COLLATE utf8_bin NOT NULL DEFAULT '',
`ns` int() NOT NULL DEFAULT '',
KEY `history_str_1` (`itemid`,`clock`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
*************************** . row ***************************
Table: history_text
Create Table: CREATE TABLE `history_text` (
`id` bigint() unsigned NOT NULL,
`itemid` bigint() unsigned NOT NULL,
`clock` int() NOT NULL DEFAULT '',
`value` text COLLATE utf8_bin NOT NULL,
`ns` int() NOT NULL DEFAULT '',
PRIMARY KEY (`id`),
UNIQUE KEY `history_text_2` (`itemid`,`id`),
KEY `history_text_1` (`itemid`,`clock`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
因为分区表使用的列必须为primary key,或者表没有主键,所以需要修改如下表:
ALTER TABLE `history_log` DROP PRIMARY KEY, ADD PRIMARY KEY (`itemid`,`id`,`clock`);
ALTER TABLE `history_log` DROP KEY `history_log_2`;
ALTER TABLE `history_text` DROP PRIMARY KEY, ADD PRIMARY KEY (`itemid`,`id`,`clock`);
ALTER TABLE `history_text` DROP KEY `history_text_2`;
创建之前删除的相应的表,并给他们加上分区,类似如下形式:
CREATE TABLE `history` (
`itemid` bigint() unsigned NOT NULL,
`clock` int() NOT NULL DEFAULT '',
`value` double(,) NOT NULL DEFAULT '0.0000',
`ns` int() NOT NULL DEFAULT '',
KEY `history_1` (`itemid`,`clock`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
partition by range(clock) (
PARTITION p20140310 VALUES LESS THAN (UNIX_TIMESTAMP("2014-03-10 00:00:00"))
);
粘贴每日分区函数:
DELIMITER //
DROP PROCEDURE IF EXISTS `zabbix`.`create_zabbix_partitions` //
CREATE PROCEDURE `zabbix`.`create_zabbix_partitions` ()
BEGIN
CALL zabbix.create_next_partitions("zabbix","history");
CALL zabbix.create_next_partitions("zabbix","history_log");
CALL zabbix.create_next_partitions("zabbix","history_str");
CALL zabbix.create_next_partitions("zabbix","history_text");
CALL zabbix.create_next_partitions("zabbix","history_uint");
CALL zabbix.drop_old_partitions("zabbix","history");
CALL zabbix.drop_old_partitions("zabbix","history_log");
CALL zabbix.drop_old_partitions("zabbix","history_str");
CALL zabbix.drop_old_partitions("zabbix","history_text");
CALL zabbix.drop_old_partitions("zabbix","history_uint");
END //
DROP PROCEDURE IF EXISTS `zabbix`.`create_next_partitions` //
CREATE PROCEDURE `zabbix`.`create_next_partitions` (SCHEMANAME varchar(), TABLENAME varchar())
BEGIN
DECLARE NEXTCLOCK timestamp;
DECLARE PARTITIONNAME varchar();
DECLARE CLOCK int;
SET @totaldays = ;
SET @i = ;
createloop: LOOP
SET NEXTCLOCK = DATE_ADD(NOW(),INTERVAL @i DAY);
SET PARTITIONNAME = DATE_FORMAT( NEXTCLOCK, 'p%Y%m%d' );
SET CLOCK = UNIX_TIMESTAMP(DATE_FORMAT(DATE_ADD( NEXTCLOCK ,INTERVAL DAY),'%Y-%m-%d 00:00:00'));
CALL zabbix.create_partition( SCHEMANAME, TABLENAME, PARTITIONNAME, CLOCK );
SET @i=@i+;
IF @i > @totaldays THEN
LEAVE createloop;
END IF;
END LOOP;
END //
DROP PROCEDURE IF EXISTS `zabbix`.`drop_old_partitions` //
CREATE PROCEDURE `zabbix`.`drop_old_partitions` (SCHEMANAME varchar(), TABLENAME varchar())
BEGIN
DECLARE OLDCLOCK timestamp;
DECLARE PARTITIONNAME varchar();
DECLARE CLOCK int;
SET @mindays = ;
SET @maxdays = @mindays+;
SET @i = @maxdays;
droploop: LOOP
SET OLDCLOCK = DATE_SUB(NOW(),INTERVAL @i DAY);
SET PARTITIONNAME = DATE_FORMAT( OLDCLOCK, 'p%Y%m%d' );
CALL zabbix.drop_partition( SCHEMANAME, TABLENAME, PARTITIONNAME );
SET @i=@i-;
IF @i <= @mindays THEN
LEAVE droploop;
END IF;
END LOOP;
END //
DROP PROCEDURE IF EXISTS `zabbix`.`create_partition` //
CREATE PROCEDURE `zabbix`.`create_partition` (SCHEMANAME varchar(), TABLENAME varchar(), PARTITIONNAME varchar(), CLOCK int)
BEGIN
DECLARE RETROWS int;
SELECT COUNT() INTO RETROWS
FROM `information_schema`.`partitions`
WHERE `table_schema` = SCHEMANAME AND `table_name` = TABLENAME AND `partition_name` = PARTITIONNAME; IF RETROWS = THEN
SELECT CONCAT( "create_partition(", SCHEMANAME, ",", TABLENAME, ",", PARTITIONNAME, ",", CLOCK, ")" ) AS msg;
SET @sql = CONCAT( 'ALTER TABLE `', SCHEMANAME, '`.`', TABLENAME, '`',
' ADD PARTITION (PARTITION ', PARTITIONNAME, ' VALUES LESS THAN (', CLOCK, '));' );
PREPARE STMT FROM @sql;
EXECUTE STMT;
DEALLOCATE PREPARE STMT;
END IF;
END //
DROP PROCEDURE IF EXISTS `zabbix`.`drop_partition` //
CREATE PROCEDURE `zabbix`.`drop_partition` (SCHEMANAME varchar(), TABLENAME varchar(), PARTITIONNAME varchar())
BEGIN
DECLARE RETROWS int;
SELECT COUNT() INTO RETROWS
FROM `information_schema`.`partitions`
WHERE `table_schema` = SCHEMANAME AND `table_name` = TABLENAME AND `partition_name` = PARTITIONNAME; IF RETROWS = THEN
SELECT CONCAT( "drop_partition(", SCHEMANAME, ",", TABLENAME, ",", PARTITIONNAME, ")" ) AS msg;
SET @sql = CONCAT( 'ALTER TABLE `', SCHEMANAME, '`.`', TABLENAME, '`',
' DROP PARTITION ', PARTITIONNAME, ';' );
PREPARE STMT FROM @sql;
EXECUTE STMT;
DEALLOCATE PREPARE STMT;
END IF;
END //
DELIMITER ;
最终的形式:
root@InnerServer1:/app/zabbix/sbin# mysql -uroot -pfengmao -e 'show create table zabbix.history\G show create table zabbix.history_uint\G show create table zabbix.history_log\G show create table zabbix.history_str\G show create table zabbix.history_text\G '
*************************** . row ***************************
Table: history
Create Table: CREATE TABLE `history` (
`itemid` bigint() unsigned NOT NULL,
`clock` int() NOT NULL DEFAULT '',
`value` double(,) NOT NULL DEFAULT '0.0000',
`ns` int() NOT NULL DEFAULT '',
KEY `history_1` (`itemid`,`clock`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
/*!50100 PARTITION BY RANGE (clock)
(PARTITION p20140310 VALUES LESS THAN (1394380800) ENGINE = InnoDB,
PARTITION p20140312 VALUES LESS THAN (1394640000) ENGINE = InnoDB,
PARTITION p20140313 VALUES LESS THAN (1394726400) ENGINE = InnoDB,
PARTITION p20140314 VALUES LESS THAN (1394812800) ENGINE = InnoDB,
PARTITION p20140315 VALUES LESS THAN (1394899200) ENGINE = InnoDB,
PARTITION p20140316 VALUES LESS THAN (1394985600) ENGINE = InnoDB,
PARTITION p20140317 VALUES LESS THAN (1395072000) ENGINE = InnoDB,
PARTITION p20140318 VALUES LESS THAN (1395158400) ENGINE = InnoDB) */
*************************** . row ***************************
Table: history_uint
Create Table: CREATE TABLE `history_uint` (
`itemid` bigint() unsigned NOT NULL,
`clock` int() NOT NULL DEFAULT '',
`value` bigint() unsigned NOT NULL DEFAULT '',
`ns` int() NOT NULL DEFAULT '',
KEY `history_uint_1` (`itemid`,`clock`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
/*!50100 PARTITION BY RANGE (clock)
(PARTITION p20140310 VALUES LESS THAN (1394380800) ENGINE = InnoDB,
PARTITION p20140312 VALUES LESS THAN (1394640000) ENGINE = InnoDB,
PARTITION p20140313 VALUES LESS THAN (1394726400) ENGINE = InnoDB,
PARTITION p20140314 VALUES LESS THAN (1394812800) ENGINE = InnoDB,
PARTITION p20140315 VALUES LESS THAN (1394899200) ENGINE = InnoDB,
PARTITION p20140316 VALUES LESS THAN (1394985600) ENGINE = InnoDB,
PARTITION p20140317 VALUES LESS THAN (1395072000) ENGINE = InnoDB,
PARTITION p20140318 VALUES LESS THAN (1395158400) ENGINE = InnoDB) */
*************************** . row ***************************
Table: history_log
Create Table: CREATE TABLE `history_log` (
`id` bigint() unsigned NOT NULL,
`itemid` bigint() unsigned NOT NULL,
`clock` int() NOT NULL DEFAULT '',
`timestamp` int() NOT NULL DEFAULT '',
`source` varchar() COLLATE utf8_bin NOT NULL DEFAULT '',
`severity` int() NOT NULL DEFAULT '',
`value` text COLLATE utf8_bin NOT NULL,
`logeventid` int() NOT NULL DEFAULT '',
`ns` int() NOT NULL DEFAULT '',
PRIMARY KEY (`itemid`,`id`,`clock`),
KEY `history_log_1` (`itemid`,`clock`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
/*!50100 PARTITION BY RANGE (clock)
(PARTITION p20140310 VALUES LESS THAN (1394380800) ENGINE = InnoDB,
PARTITION p20140312 VALUES LESS THAN (1394640000) ENGINE = InnoDB,
PARTITION p20140313 VALUES LESS THAN (1394726400) ENGINE = InnoDB,
PARTITION p20140314 VALUES LESS THAN (1394812800) ENGINE = InnoDB,
PARTITION p20140315 VALUES LESS THAN (1394899200) ENGINE = InnoDB,
PARTITION p20140316 VALUES LESS THAN (1394985600) ENGINE = InnoDB,
PARTITION p20140317 VALUES LESS THAN (1395072000) ENGINE = InnoDB,
PARTITION p20140318 VALUES LESS THAN (1395158400) ENGINE = InnoDB) */
*************************** . row ***************************
Table: history_str
Create Table: CREATE TABLE `history_str` (
`itemid` bigint() unsigned NOT NULL,
`clock` int() NOT NULL DEFAULT '',
`value` varchar() COLLATE utf8_bin NOT NULL DEFAULT '',
`ns` int() NOT NULL DEFAULT '',
KEY `history_str_1` (`itemid`,`clock`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
/*!50100 PARTITION BY RANGE (clock)
(PARTITION p20140310 VALUES LESS THAN (1394380800) ENGINE = InnoDB,
PARTITION p20140312 VALUES LESS THAN (1394640000) ENGINE = InnoDB,
PARTITION p20140313 VALUES LESS THAN (1394726400) ENGINE = InnoDB,
PARTITION p20140314 VALUES LESS THAN (1394812800) ENGINE = InnoDB,
PARTITION p20140315 VALUES LESS THAN (1394899200) ENGINE = InnoDB,
PARTITION p20140316 VALUES LESS THAN (1394985600) ENGINE = InnoDB,
PARTITION p20140317 VALUES LESS THAN (1395072000) ENGINE = InnoDB,
PARTITION p20140318 VALUES LESS THAN (1395158400) ENGINE = InnoDB) */
*************************** . row ***************************
Table: history_text
Create Table: CREATE TABLE `history_text` (
`id` bigint() unsigned NOT NULL,
`itemid` bigint() unsigned NOT NULL,
`clock` int() NOT NULL DEFAULT '',
`value` text COLLATE utf8_bin NOT NULL,
`ns` int() NOT NULL DEFAULT '',
PRIMARY KEY (`itemid`,`id`,`clock`),
KEY `history_text_1` (`itemid`,`clock`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
/*!50100 PARTITION BY RANGE (clock)
(PARTITION p20140310 VALUES LESS THAN (1394380800) ENGINE = InnoDB,
PARTITION p20140312 VALUES LESS THAN (1394640000) ENGINE = InnoDB,
PARTITION p20140313 VALUES LESS THAN (1394726400) ENGINE = InnoDB,
PARTITION p20140314 VALUES LESS THAN (1394812800) ENGINE = InnoDB,
PARTITION p20140315 VALUES LESS THAN (1394899200) ENGINE = InnoDB,
PARTITION p20140316 VALUES LESS THAN (1394985600) ENGINE = InnoDB,
PARTITION p20140317 VALUES LESS THAN (1395072000) ENGINE = InnoDB,
PARTITION p20140318 VALUES LESS THAN (1395158400) ENGINE = InnoDB) */
root@InnerServer1:/app/zabbix/sbin#
[zabbix]zabbix分区表操作步骤的更多相关文章
- Ubuntu 安装 Zabbix 3.2详细步骤
创建 zabbix 用户 因为zabbix 程序的守护进程需要非特权用户,所以需要创建一个 zabbix 用户,即使从 root 用户启动 zabbix 程序,也会自动切换到 zabbix 用户,所以 ...
- zabbix的简单操作(查看监控,自定义监控和钉钉监控报警)
zabbix是一种监控软件,我用的是centos7.5版本 一:我已经添加好主机了,接下来就是看看怎么查看监控内容的 1.打开zabbix服务的web网页 2.检测最新数据,要在最新数据中筛选 3.查 ...
- zabbix的简单操作(proxy代理分布式监控)
分布式监控 作用:分担压力,减轻负载,多机房监控 通过zabbix proxy的搭建,zabbix server可以远程从proxy获取到数据,这里的环境相当于zabbix server具有一个公网i ...
- [zabbix] zabbix数据采集频率、数据连续多次异常触发、告警次数、告警频率
数据采集频率:1分钟采集一次 数据连续多次异常触发:连续三次异常才触发告警 告警次数:告警三次 告警频率:每隔10分钟告警一次 默认模板“Template App Zabbix Agent”监控项“A ...
- Sql Server系列:分区表操作
1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一样的.使用分区表的主要目的是为改善大型表以及具有多个访问模式的表的可伸缩性和可管理性. 分区表是把数据按设 ...
- (转)SQLServer分区表操作
原文地址:https://www.cnblogs.com/libingql/p/4087598.html 1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一 ...
- linux篇-Linux MBR分区、挂载操作步骤,逻辑卷扩容操作
Linux MBR分区.挂载操作步骤,逻辑卷扩容操作 服务器开机之后,能自动识别出硬盘,但是硬盘不能够存储数据,必须对硬盘进行分区.格式化.挂载后才能使用:linux主分区和拓展分区总数不能超过4个 ...
- ABP创建数据库操作步骤
1 ABP创建数据库操作步骤 1.1 SimpleTaskSystem.Web项目中的Web.config文件修改数据库配置. <add name="Default" pro ...
- Altium Designer PCB双面板制作打印操作步骤
Altium Designer PCB双面板制作打印操作步骤百度知道:http://jingyan.baidu.com/article/335530da83441c19cb41c3db.html?st ...
随机推荐
- Vuejs 基础与语法
Vue 实例 创建第一个实例 {{}} 被称之为插值表达式.可以用来进行文本插值. <!DOCTYPE html> <html lang="en"> < ...
- 【Coursera-ML-Notes】线性回归(下)
模型表示 多变量的线性回归也叫做"多元线性回归".首先还是先明确几个符号的含义. \(x{^{(i)}_j}\):第i个训练样本的第j个特征,比如面积,楼层,客厅数 \(x^{(i ...
- 直接管理内存——new和delete
一.运算符new 1. 使用new动态分配对象 在自由空间分配的内存是无名的,故new无法为其分配的对象命名,而是返回一个指向该对象的指针 int *pi = new int; //pi指向一个动态分 ...
- 20172330 2017-2018-1 《Java程序设计》第八周学习总结
学号 2017-2018-1 <程序设计与数据结构>第八周学习总结 教材学习内容总结 这一章主要是对多态性的学习: 由继承实现多态性 多态性引用能够随时间变化指向不同类型的对象. 对于多态 ...
- 用纯css改变下拉列表select框的默认样式(转)
用纯css改变下拉列表select框的默认样式 分享到 分类 JS学习 关键字 前端 发布 kris 2015-04-01 注意 转载须保留原文链接,译文链接,作者译者等信息. 在这 ...
- python正则表达式函数match()和search()的区别详解
match()和search()都是python中的正则匹配函数,那这两个函数有何区别呢? match()函数只检测RE是不是在string的开始位置匹配, search()会扫描整个string查找 ...
- 团队选题报告(i know)
一.团队成员及分工 团队名称:I know 团队成员: 陈家权:选题报告word撰写 赖晓连:ppt制作,原型设计 雷晶:ppt制作,原型设计 林巧娜:原型设计,博客随笔撰写 庄加鑫:选题报告word ...
- Alpha 冲刺报告(3/10)
Alpha 冲刺报告 队名:洛基小队 峻雄(组长) 已完成:开始编写角色的移动脚本 明日计划:继续学习并进行脚本编写 剩余任务:物品背包交互代码 困难:如何把各个模块的脚本整合起来 --------- ...
- Alpha 冲刺报告(4/10)
Alpha 冲刺报告(4/10) 队名:洛基小队 峻雄(组长) 已完成:继续行动脚本的编写 明日计划:尽量完成角色的移动 剩余任务:物品背包交互代码 困难:具体编码进展比较缓慢 ----------- ...
- 【Docker 命令】- start/stop/restart命令
docker start:启动一个或多少已经被停止的容器 docker stop:停止一个运行中的容器 docker restart :重启容器 语法: docker start [OPTIONS] ...