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分区表操作步骤的更多相关文章

  1. Ubuntu 安装 Zabbix 3.2详细步骤

    创建 zabbix 用户 因为zabbix 程序的守护进程需要非特权用户,所以需要创建一个 zabbix 用户,即使从 root 用户启动 zabbix 程序,也会自动切换到 zabbix 用户,所以 ...

  2. zabbix的简单操作(查看监控,自定义监控和钉钉监控报警)

    zabbix是一种监控软件,我用的是centos7.5版本 一:我已经添加好主机了,接下来就是看看怎么查看监控内容的 1.打开zabbix服务的web网页 2.检测最新数据,要在最新数据中筛选 3.查 ...

  3. zabbix的简单操作(proxy代理分布式监控)

    分布式监控 作用:分担压力,减轻负载,多机房监控 通过zabbix proxy的搭建,zabbix server可以远程从proxy获取到数据,这里的环境相当于zabbix server具有一个公网i ...

  4. [zabbix] zabbix数据采集频率、数据连续多次异常触发、告警次数、告警频率

    数据采集频率:1分钟采集一次 数据连续多次异常触发:连续三次异常才触发告警 告警次数:告警三次 告警频率:每隔10分钟告警一次 默认模板“Template App Zabbix Agent”监控项“A ...

  5. Sql Server系列:分区表操作

    1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一样的.使用分区表的主要目的是为改善大型表以及具有多个访问模式的表的可伸缩性和可管理性. 分区表是把数据按设 ...

  6. (转)SQLServer分区表操作

    原文地址:https://www.cnblogs.com/libingql/p/4087598.html 1. 分区表简介 分区表在逻辑上是一个表,而物理上是多个表.从用户角度来看,分区表和普通表是一 ...

  7. linux篇-Linux MBR分区、挂载操作步骤,逻辑卷扩容操作

    Linux  MBR分区.挂载操作步骤,逻辑卷扩容操作 服务器开机之后,能自动识别出硬盘,但是硬盘不能够存储数据,必须对硬盘进行分区.格式化.挂载后才能使用:linux主分区和拓展分区总数不能超过4个 ...

  8. ABP创建数据库操作步骤

    1 ABP创建数据库操作步骤 1.1 SimpleTaskSystem.Web项目中的Web.config文件修改数据库配置. <add name="Default" pro ...

  9. Altium Designer PCB双面板制作打印操作步骤

    Altium Designer PCB双面板制作打印操作步骤百度知道:http://jingyan.baidu.com/article/335530da83441c19cb41c3db.html?st ...

随机推荐

  1. 407. Plus One【LintCode java】

    Description Given a non-negative number represented as an array of digits, plus one to the number. T ...

  2. 孤荷凌寒自学python第七十七天开始写Python的第一个爬虫7

    孤荷凌寒自学python第七十七天开始写Python的第一个爬虫7 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 今天的学习仍然是在纯粹对docx模 ...

  3. spring 整合hibernate注解时候,出现“Unknown entity: com.ssh.entry.Admin; nested exception is org.hibernate.MappingException: Unknown entity: com.ssh.entry.Admin”异常的问题

    今天学习使用ssh框架的时候,出现一个异常,弄了好久才找到,在这记录一下,我的sb错误1.spring整合hibernate,取代*.hbm.xml配置文件   在applicationContext ...

  4. [LeetCode] 53. Maximum Subarray 解题思路

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  5. Html5 input placeholder 属性字体颜色修改。

    这篇文章主要介绍了有关HTML5 input placeholder 颜色修改方面的知识,需要的朋友可以参考下     Chrome支持input=[type=text]占位文本属性,但下列CSS样式 ...

  6. allocator类

    一.动态数组 [new的局限性] new将内存分配和对象构造组合在一起,同样delete将对象析构和内存释放组合在一起 我们分配单个对象时,通常希望将内存分配和对象初始化组合在一起(我们知道对象应有什 ...

  7. Pipeline组测试说明

    PIPELINE组测试报告 前言:我们组与学霸系统的其他两个小组共同合作开发,组成学霸系统的团体工作.作为学霸系统的一环,我们组起到承上启下的作用,因此,面向群体以及功能实现都是为给下一个组的工作做好 ...

  8. Python实用技巧

    1.改变工作目录 import os os.chdir('C:/Users/Mr.Zhao') 2.搜索制定目录下的文件 1 import glob 2 glob.glob('C:/User/Mr.Z ...

  9. lintcode-36-翻转链表 II

    36-翻转链表 II 翻转链表中第m个节点到第n个节点的部分 注意事项 m,n满足1 ≤ m ≤ n ≤ 链表长度 样例 给出链表1->2->3->4->5->null, ...

  10. noauth authentication required redis

    解决方案: 这是出现了认证的问题,是因为设置了认证密码. 127.0.0.1:6379> auth "yourpassword" 例如: