標籤:zabbix mysql最佳化
SELECT table_name AS "Tables", round(((data_length + index_length) / 1024 / 1024), 2) "Size in MB" FROM information_schema.TABLES WHERE table_schema = ‘zabbix‘ ORDER BY (data_length + index_length) DESC limit 10; (查看 mysql資料庫表大小)
一 、轉換分區表
ALTER TABLE history PARTITION BY range( clock) (PARTITION P20160301 VALUES LESS THAN (UNIX_TIMESTAMP(‘2016-03-01‘)));ALTER TABLE history_log PARTITION BY range( clock) (PARTITION P20160301 VALUES LESS THAN (UNIX_TIMESTAMP(‘2016-03-01‘)));ALTER TABLE history_str PARTITION BY range( clock) (PARTITION P20160301 VALUES LESS THAN (UNIX_TIMESTAMP(‘2016-03-01‘)));ALTER TABLE history_text PARTITION BY range( clock) (PARTITION P20160301 VALUES LESS THAN (UNIX_TIMESTAMP(‘2016-03-01‘)));ALTER TABLE history_uint PARTITION BY range( clock) (PARTITION P20160301 VALUES LESS THAN (UNIX_TIMESTAMP(‘2016-03-01‘)));ALTER TABLE trends PARTITION BY range( clock) (PARTITION P20160301 VALUES LESS THAN (UNIX_TIMESTAMP(‘2016-03-01‘)));ALTER TABLE trends_uint PARTITION BY range( clock) (PARTITION P20160301 VALUES LESS THAN (UNIX_TIMESTAMP(‘2016-03-01‘)));
二、執行指令碼
DELIMITER //DROP PROCEDURE IF EXISTS `zabbix`.`create_zabbix_partitions` //CREATE PROCEDURE `zabbix`.`create_zabbix_partitions` ()BEGINCALL 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.create_next_partitions("zabbix","trends");CALL zabbix.create_next_partitions("zabbix","trends_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");CALL zabbix.drop_old_partitions("zabbix","trends");CALL zabbix.drop_old_partitions("zabbix","trends_uint");END //DROP PROCEDURE IF EXISTS `zabbix`.`create_next_partitions` //CREATE PROCEDURE `zabbix`.`create_next_partitions` (SCHEMANAME varchar(64), TABLENAME varchar(64))BEGINDECLARE NEXTCLOCK timestamp;DECLARE PARTITIONNAME varchar(16);DECLARE CLOCK int;SET @totaldays = 7;SET @i = 1;createloop: LOOPSET 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.create_partition( SCHEMANAME, TABLENAME, PARTITIONNAME, CLOCK );SET @[email protected]+1;IF @i > @totaldays THENLEAVE createloop;END IF;END LOOP;END //DROP PROCEDURE IF EXISTS `zabbix`.`drop_old_partitions` //CREATE PROCEDURE `zabbix`.`drop_old_partitions` (SCHEMANAME varchar(64), TABLENAME varchar(64))BEGINDECLARE OLDCLOCK timestamp;DECLARE PARTITIONNAME varchar(16);DECLARE CLOCK int;SET @mindays = 30;SET @maxdays = @mindays+4;SET @i = @maxdays;droploop: LOOPSET OLDCLOCK = DATE_SUB(NOW(),INTERVAL @i DAY);SET PARTITIONNAME = DATE_FORMAT( OLDCLOCK, ‘p%Y%m%d‘ );CALL zabbix.drop_partition( SCHEMANAME, TABLENAME, PARTITIONNAME );SET @[email protected];IF @i <= @mindays THENLEAVE droploop;END IF;END LOOP;END //DROP PROCEDURE IF EXISTS `zabbix`.`create_partition` //CREATE PROCEDURE `zabbix`.`create_partition` (SCHEMANAME varchar(64), TABLENAME varchar(64), PARTITIONNAME varchar(64), CLOCK int)BEGINDECLARE RETROWS int;SELECT COUNT(1) INTO RETROWSFROM `information_schema`.`partitions`WHERE `table_schema` = SCHEMANAME AND `table_name` = TABLENAME AND `partition_name` = PARTITIONNAME; IF RETROWS = 0 THENSELECT 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(64), TABLENAME varchar(64), PARTITIONNAME varchar(64))BEGINDECLARE RETROWS int;SELECT COUNT(1) INTO RETROWSFROM `information_schema`.`partitions`WHERE `table_schema` = SCHEMANAME AND `table_name` = TABLENAME AND `partition_name` = PARTITIONNAME; IF RETROWS = 1 THENSELECT 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 ;
將上面代碼儲存partition.sql 檔案中
執行
/apps/mysql/bin/mysql -uroot [email protected] zabbix < /apps/mysql/partition.sql/apps/mysql/bin/mysql -B zabbix -uroot [email protected] -e "CALL create_zabbix_partitions();"
三、加入 crontab
30 23 * * * /apps/mysql/bin/mysql -uroot [email protected] zabbix < /apps/mysql/partition.sql59 23 * * * /apps/mysql/bin/mysql -B zabbix -uroot [email protected] -e "CALL create_zabbix_partitions();"
zabbix mysql調優