溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

zabbix db partition

發(fā)布時(shí)間:2020-07-25 17:41:10 來(lái)源:網(wǎng)絡(luò) 閱讀:2227 作者:菜菜光 欄目:系統(tǒng)運(yùn)維
 在做zabbix的性能優(yōu)化時(shí),有時(shí)候在db的數(shù)據(jù)量比較大的時(shí)候,需要對(duì)表進(jìn)行partition操作,這樣可以在數(shù)據(jù)查詢減少用時(shí)。并且由于使用了partition,我們可以自己實(shí)現(xiàn)歷史數(shù)據(jù)的刪除操作,這樣就可以禁用zabbix的housekeeping功能。

簡(jiǎn)單的說(shuō)下再2.0.x版本的zabbix中進(jìn)行partition的操作:
1.備份數(shù)據(jù),如果使用proxy的結(jié)構(gòu)的話,調(diào)整ProxyOfflineBuffer,加大數(shù)據(jù)在offline情況的緩存時(shí)間,這樣在partition調(diào)整完后數(shù)據(jù)會(huì)自動(dòng)補(bǔ)充。

確定需要partition的表,并更改表結(jié)構(gòu)(使用clock進(jìn)行parttition,partition key必須是primarykey的一部分),涉及的表主要是存儲(chǔ)歷史相關(guān)數(shù)據(jù)的表:history,history_uint,history_text,history_log,history_str

表結(jié)構(gòu)更改:

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`;

剩下的history_str,history,history_uint 3個(gè)表不需要做更改。

2.按clock進(jìn)行分區(qū)

例子:

ALTER TABLE `history_uint` PARTITION BY RANGE( clock ) (
PARTITION p20140101 VALUES LESS THAN (UNIX_TIMESTAMP("2014-01-02 00:00:00")),
PARTITION p20140102 VALUES LESS THAN (UNIX_TIMESTAMP("2014-01-03 00:00:00")),
PARTITION p20140103 VALUES LESS THAN (UNIX_TIMESTAMP("2014-01-04 00:00:00")),
PARTITION p20140104 VALUES LESS THAN (UNIX_TIMESTAMP("2014-01-05 00:00:00")),
PARTITION p20140105 VALUES LESS THAN (UNIX_TIMESTAMP("2014-01-06 00:00:00")),
PARTITION p20140106 VALUES LESS THAN (UNIX_TIMESTAMP("2014-01-07 00:00:00"))
);
把需要分區(qū)的表都進(jìn)行相同的操作。
3.設(shè)置存儲(chǔ)規(guī)則,并使用cronjob來(lái)實(shí)現(xiàn)自動(dòng)partition操作
mysql -u xxx -pxxx xxx < ./partition.sql
其中partition.sql的內(nèi)容如下:
DELIMITER //
DROP PROCEDURE IF EXISTS `zabbix_server`.`create_zabbix_partitions` //
CREATE PROCEDURE `zabbix_server`.`create_zabbix_partitions` ()
BEGIN
CALL zabbix_server.create_next_partitions("zabbix_server","history");
CALL zabbix_server.create_next_partitions("zabbix_server","history_log");
CALL zabbix_server.create_next_partitions("zabbix_server","history_str");
CALL zabbix_server.create_next_partitions("zabbix_server","history_text");
CALL zabbix_server.create_next_partitions("zabbix_server","history_uint");
CALL zabbix_server.drop_old_partitions("zabbix_server","history");
CALL zabbix_server.drop_old_partitions("zabbix_server","history_log");
CALL zabbix_server.drop_old_partitions("zabbix_server","history_str");
CALL zabbix_server.drop_old_partitions("zabbix_server","history_text");
CALL zabbix_server.drop_old_partitions("zabbix_server","history_uint");
END //
DROP PROCEDURE IF EXISTS `zabbix_server`.`create_next_partitions` //
CREATE PROCEDURE `zabbix_server`.`create_next_partitions` (SCHEMANAME varchar(64), TABLENAME varchar(64))
BEGIN
DECLARE NEXTCLOCK timestamp;
DECLARE PARTITIONNAME varchar(16);
DECLARE CLOCK int;
SET @totaldays = 7;
SET @i = 1;
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 1 DAY),'%Y-%m-%d 00:00:00'));
CALL zabbix_server.create_partition( SCHEMANAME, TABLENAME, PARTITIONNAME, CLOCK );
SET @i=@i+1;
IF @i > @totaldays THEN
LEAVE createloop;
END IF;
END LOOP;
END //
DROP PROCEDURE IF EXISTS `zabbix_server`.`drop_old_partitions` //
CREATE PROCEDURE `zabbix_server`.`drop_old_partitions` (SCHEMANAME varchar(64), TABLENAME varchar(64))
BEGIN
DECLARE OLDCLOCK timestamp;
DECLARE PARTITIONNAME varchar(16);
DECLARE CLOCK int;
SET @mindays = 30;
SET @maxdays = @mindays+4;
SET @i = @maxdays;
droploop: LOOP
SET OLDCLOCK = DATE_SUB(NOW(),INTERVAL @i DAY);
SET PARTITIONNAME = DATE_FORMAT( OLDCLOCK, 'p%Y%m%d' );
CALL zabbix_server.drop_partition( SCHEMANAME, TABLENAME, PARTITIONNAME );
SET @i=@i-1;
IF @i <= @mindays THEN
LEAVE droploop;
END IF;
END LOOP;
END //
DROP PROCEDURE IF EXISTS `zabbix_server`.`create_partition` //
CREATE PROCEDURE `zabbix_server`.`create_partition` (SCHEMANAME varchar(64), TABLENAME varchar(64), PARTITIONNAME varchar(64), CLOCK int)
BEGIN
DECLARE RETROWS int;
SELECT COUNT(1) INTO RETROWS
FROM `information_schema`.`partitions`
WHERE `table_schema` = SCHEMANAME AND `table_name` = TABLENAME AND `partition_name` = PARTITIONNAME;
                                     
IF RETROWS = 0 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_server`.`drop_partition` //
CREATE PROCEDURE `zabbix_server`.`drop_partition` (SCHEMANAME varchar(64), TABLENAME varchar(64), PARTITIONNAME varchar(64))
BEGIN
DECLARE RETROWS int;
SELECT COUNT(1) INTO RETROWS
FROM `information_schema`.`partitions`
WHERE `table_schema` = SCHEMANAME AND `table_name` = TABLENAME AND `partition_name` = PARTITIONNAME;
                                     
IF RETROWS = 1 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 ;
查看存儲(chǔ)過(guò)程:
select name from mysql.proc;
show create procedure create_partition;
cronjob形式的調(diào)用:
mysql  -B -xxx -pxxx xxx -e "CALL create_zabbix_partitions();"
4.測(cè)試partition的結(jié)果:
1)查看表結(jié)構(gòu)
CREATE TABLE `history` (
  `itemid` bigint(20) unsigned NOT NULL,
  `clock` int(11) NOT NULL DEFAULT '0',
  `value` double(16,4) NOT NULL DEFAULT '0.0000',
  `ns` int(11) NOT NULL DEFAULT '0',
  KEY `history_1` (`itemid`,`clock`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
/*!50100 PARTITION BY RANGE ( clock)
(PARTITION p20140101 VALUES LESS THAN (1388592000) ENGINE = InnoDB,
PARTITION p20140102 VALUES LESS THAN (1388678400) ENGINE = InnoDB,
PARTITION p20140103 VALUES LESS THAN (1388764800) ENGINE = InnoDB,
PARTITION p20140104 VALUES LESS THAN (1388851200) ENGINE = InnoDB,
PARTITION p20140105 VALUES LESS THAN (1388937600) ENGINE = InnoDB,
PARTITION p20140106 VALUES LESS THAN (1389024000) ENGINE = InnoDB,
PARTITION p20140107 VALUES LESS THAN (1389110400) ENGINE = InnoDB,
PARTITION p20140108 VALUES LESS THAN (1389196800) ENGINE = InnoDB,
PARTITION p20140109 VALUES LESS THAN (1389283200) ENGINE = InnoDB,
PARTITION p20140110 VALUES LESS THAN (1389369600) ENGINE = InnoDB) */
2)explain查看執(zhí)行計(jì)劃

explain partitions xxx

5.關(guān)閉housekeeping,并驗(yàn)證host update percent的情況
1)DisableHousekeeping=1

2)

select b.hostname ,c.ip,a.update_percent as uppercent from ( select b.hostid,ROUND(IFNULL(a.aa,0)*100/b.bb,2) as update_percent from  (select hostid,count(*) as aa from items where lastclock > UNIX_TIMESTAMP()-1800 and delay < 900   and hostid in (select hostid from hosts where status=0)   and status = 0 group by hostid ) a RIGHT JOIN (select hostid,count(*) as bb from items where delay < 900 and status = 0 and hostid in (select hostid from hosts where status=0) group by hostid) b  ON a.hostid=b.hostid)a,(select hostid,lower(host) as hostname from hosts where status=0)b, (select hostid,ip from interface where type='1')c where a.hostid=b.hostid and b.hostid=c.hostid  order by uppercent;




向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI