1. MySQL版本和配置文件
采用percona5.5, 配置文件:
[root@gazelle ~]# cat /etc/my.cnf
[mysqld]
socket=/var/lib/mysql/mysql.sock
user=mysql
symbolic-links=0
character-set-server = utf8
datadir=/home/mysqldata
skip-external-locking
skip-name-resolve
#network
connect_timeout =60
wait_timeout = 28800
max_connections =5000
back_log = 300
max_allowed_packet =64M
max_connect_errors =1000
#limits
tmp_table_size =512M
max_heap_table_size =256M
table-cache = 4096
key_buffer_size = 128M
sort-buffer-size = 16M
join-buffer-size = 16M
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
thread-cache-size = 16
thread-concurrency = 24
query-cache-size = 4096M
query-cache-limit = 4M
#innodb
#innodb_data_file_path=ibdata1:128M:ibdata2:128M:autoextend:max:4096M
innodb_file_per_table = 1
innodb_file_io_threads = 4
innodb_open_files = 2048
innodb_status_file =1
innodb_additional_mem_pool_size =128M
innodb_thread_concurrency = 16
innodb_max_dirty_pages_pct = 90
innodb_buffer_pool_size =14G
innodb_flush_method =O_DIRECT
#innodb_io_capacity =2000
innodb_flush_log_at_trx_commit=2
innodb_support_xa =0
innodb_log_file_size =512M
innodb_log_buffer_size =128M
#log-queries-not-using-indexes
# other stuff
event_scheduler =1
query_cache_type =0
#log
log_warnings
slow_query_log=1
long_query_time=20
slow_query_log_file=/var/log/mysql/slow-queries.log
log-error=/var/log/mysql/mysqld-error.log
[mysqld_safe]
pid-file=/var/run/mysqld/mysqld.pid
2. 表结构修改(参考itnihao的书)
ALTER TABLE housekeeper ENGINE = BLACKHOLE;
ALTER TABLE history_text DROP primary key, ADD index (id), DROP index history_text_2, ADD index history_text_2 (itemid, id);
ALTER TABLE history_logDROP primary key, ADD index (id), DROP index history_log_2, ADD index history_log_2 (itemid, id);
3. 表分区
/*
auto patition
*/
DROP PROCEDURE IF EXISTS `partition_create` ;
DELIMITER //
CREATE PROCEDURE `partition_create` (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("partition_create(", 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//
DELIMITER ;
DROP PROCEDURE IF EXISTS `partition_drop` ;
DELIMITER $$
CREATE PROCEDURE `partition_drop` (SCHEMANAME VARCHAR(64), TABLENAME VARCHAR(64), DELETE_BELOW_PARTITION_DATE BIGINT)
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE drop_part_name VARCHAR(16);
DECLARE myCursor CURSOR FOR
SELECT partition_name
FROM information_schema.partitions
WHERE table_schema = SCHEMANAME AND TABLE_NAME = TABLENAME AND CAST(SUBSTRING(partition_name FROM 2) AS UNSIGNED) < DELETE_BELOW_PARTITION_DATE;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
SET @alter_header = CONCAT("ALTER TABLE ", SCHEMANAME,".", TABLENAME," DROP PARTITION ");
SET @drop_partitions ="";
/*
Start looping through all the partitions that are too old.
*/
OPEN myCursor;
read_loop: LOOP
FETCH myCursor INTO drop_part_name;
IF done THEN
LEAVE read_loop;
END IF;
SET @drop_partitions =IF(@drop_partitions ="", drop_part_name, CONCAT(@drop_partitions,",", drop_part_name));
END LOOP;
IF @drop_partitions !=""THEN
/*
1. Build the SQL to drop all the necessary partitions.
2. Run the SQL to drop the partitions.
3. Print out the table partitions that were deleted.
*/
SET @full_sql = CONCAT(@alter_header, @drop_partitions,";");
PREPARE STMT FROM @full_sql;
EXECUTE STMT;
DEALLOCATE PREPARE STMT;
SELECT CONCAT(SCHEMANAME,".", TABLENAME) AS `table`, @drop_partitions AS `partitions_deleted`;
ELSE
/*
No partitions are being deleted, so print out "N/A" (Not
applicable) to indicatethat no changes were made.
*/
SELECT CONCAT(SCHEMANAME,".", TABLENAME) AS `table`, "N/A" AS `partitions_deleted`;
END IF;
END$$
DELIMITER ;
DROP PROCEDURE IF EXISTS `partition_maintenance` ;
DELIMITER $$
CREATE PROCEDURE `partition_maintenance` (SCHEMA_NAME VARCHAR(32),TABLE_NAME VARCHAR(32), KEEP_DATA_DAYS INT, HOURLY_INTERVAL INT, CREATE_NEXT_INTERVALS INT)
BEGIN
DECLARE OLDER_THAN_PARTITION_DATE VARCHAR(16);
DECLARE PARTITION_NAME VARCHAR(16);
DECLARE LESS_THAN_TIMESTAMP INT;
DECLARE CUR_TIME INT;
CALL partition_verify(SCHEMA_NAME,TABLE_NAME, HOURLY_INTERVAL);
SET CUR_TIME = UNIX_TIMESTAMP(DATE_FORMAT(NOW(),'%Y-%m-%d 00:00:00'));
IF DATE(NOW()) = '2014-04-01' THEN
SET CUR_TIME = UNIX_TIMESTAMP(DATE_FORMAT(DATE_ADD(NOW(),INTERVAL 1 DAY),'%Y-%m-%d 00:00:00'));
END IF;
SET @__interval = 1;
create_loop: LOOP
IF @__interval > CREATE_NEXT_INTERVALS THEN
LEAVE create_loop;
END IF;
SET LESS_THAN_TIMESTAMP = CUR_TIME +(HOURLY_INTERVAL * @__interval *3600);
SET PARTITION_NAME = FROM_UNIXTIME(CUR_TIME + HOURLY_INTERVAL *(@__interval -1)*3600,'p%Y%m%d%H00');
CALL partition_create(SCHEMA_NAME,TABLE_NAME, PARTITION_NAME, LESS_THAN_TIMESTAMP);
SET @__interval = @__interval +1;
END LOOP;
SET OLDER_THAN_PARTITION_DATE=DATE_FORMAT(DATE_SUB(NOW(),INTERVAL KEEP_DATA_DAYS DAY),'%Y%m%d0000');
CALL partition_drop(SCHEMA_NAME,TABLE_NAME, OLDER_THAN_PARTITION_DATE);
END$$
DELIMITER ;
DROP PROCEDURE IF EXISTS `partition_verify` ;
DELIMITER $$
CREATE PROCEDURE `partition_verify` (SCHEMANAME VARCHAR(64), TABLENAME VARCHAR(64), HOURLYINTERVAL INT(11))
BEGIN
DECLARE PARTITION_NAME VARCHAR(16);
DECLARE RETROWS INT(11);
DECLARE FUTURE_TIMESTAMP TIMESTAMP;
SELECT COUNT(1) INTO RETROWS
FROM information_schema.partitions
WHERE table_schema = SCHEMANAME AND TABLE_NAME= TABLENAME AND partition_name IS NULL;
IF RETROWS = 1 THEN
SET FUTURE_TIMESTAMP = TIMESTAMPADD(HOUR, HOURLYINTERVAL, CONCAT(CURDATE()," ",'00:00:00'));
SET PARTITION_NAME = DATE_FORMAT(CURDATE(),'p%Y%m%d%H00');
SET @__PARTITION_SQL = CONCAT("ALTER TABLE ", SCHEMANAME,".", TABLENAME," PARTITION BY RANGE(`clock`)");
SET @__PARTITION_SQL = CONCAT(@__PARTITION_SQL,"(PARTITION ", PARTITION_NAME," VALUES LESS THAN (", UNIX_TIMESTAMP(FUTURE_TIMESTAMP),"));");
PREPARE STMT FROM @__PARTITION_SQL;
EXECUTE STMT;
DEALLOCATE PREPARE STMT;
END IF;
END$$
DELIMITER ;
DROP PROCEDURE IF EXISTS `partition_maintenance_all` ;
DELIMITER $$
CREATE PROCEDURE `partition_maintenance_all`(SCHEMA_NAME VARCHAR(32))
BEGIN
CALL partition_maintenance(SCHEMA_NAME, 'history', 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, 'history_log', 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, 'history_str', 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, 'history_text', 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, 'history_uint', 28, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, 'trends', 730, 24, 14);
CALL partition_maintenance(SCHEMA_NAME, 'trends_uint', 730, 24, 14);
END$$
DELIMITER ;
CALL partition_maintenance_all('zabbix');
# 1 1 * * * mysql -uzabbix -pzabbix zabbix -e "CALL partition_maintenance_all('zabbix')"
网友评论