知乎專欄 | 多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者” |
CREATE TABLE `logging` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `tag` ENUM('unknow','www','user','admin') NOT NULL DEFAULT 'unknow' COMMENT '日誌標籤', `time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '產生時間', `facility` ENUM('bank','unionpay','sms','email') NOT NULL COMMENT '類別', `priority` ENUM('info','warning','error','critical','exception','debug') NOT NULL COMMENT '級別', `message` VARCHAR(512) NOT NULL COMMENT '內容', PRIMARY KEY (`id`) ) COMMENT='日誌表' COLLATE='utf8_general_ci' ENGINE=InnoDB AUTO_INCREMENT=2;
分區日誌表
delimiter $$ CREATE TABLE `logging` ( `tag` enum('unknow','login','info','admin','cron','manual') NOT NULL DEFAULT 'unknow' COMMENT '日誌標籤', `asctime` datetime NOT NULL COMMENT '產生時間', `facility` enum('account','bank','unionpay','sms','email','unknow') NOT NULL DEFAULT 'unknow' COMMENT '類別', `priority` enum('info','warning','error','critical','exception','debug') NOT NULL DEFAULT 'debug' COMMENT '級別', `message` varchar(512) NOT NULL COMMENT '內容', `operator` varchar(50) NOT NULL DEFAULT 'computer' COMMENT '操作者' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 /*!50100 PARTITION BY RANGE (YEAR(asctime)) SUBPARTITION BY HASH (MONTH(asctime)) (PARTITION p0 VALUES LESS THAN (1990) ENGINE = InnoDB, PARTITION p1 VALUES LESS THAN (2000) ENGINE = InnoDB, PARTITION p2 VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */$$
分表+分區,每年分表一次,每個分區中保存一個月的數據
delimiter $$ CREATE TABLE `logging_2013` ( `tag` enum('unknow','login','info','admin','cron','manual') NOT NULL DEFAULT 'unknow' COMMENT '日誌標籤', `asctime` datetime NOT NULL COMMENT '產生時間', `facility` enum('account','bank','unionpay','sms','email','unknow') NOT NULL DEFAULT 'unknow' COMMENT '類別', `priority` enum('info','warning','error','critical','exception','debug') NOT NULL DEFAULT 'debug' COMMENT '級別', `message` varchar(512) NOT NULL COMMENT '內容', `operator` varchar(50) NOT NULL DEFAULT 'computer' COMMENT '操作者' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 /*!50100 PARTITION BY LIST (MONTH(asctime)) SUBPARTITION BY KEY (facility) (PARTITION part0 VALUES IN (1) ENGINE = InnoDB, PARTITION part1 VALUES IN (2) ENGINE = InnoDB, PARTITION part2 VALUES IN (3) ENGINE = InnoDB, PARTITION part3 VALUES IN (4) ENGINE = InnoDB, PARTITION part4 VALUES IN (5) ENGINE = InnoDB, PARTITION part5 VALUES IN (6) ENGINE = InnoDB, PARTITION part6 VALUES IN (7) ENGINE = InnoDB, PARTITION part7 VALUES IN (8) ENGINE = InnoDB, PARTITION part8 VALUES IN (9) ENGINE = InnoDB, PARTITION part9 VALUES IN (10) ENGINE = InnoDB, PARTITION part10 VALUES IN (11) ENGINE = InnoDB, PARTITION part11 VALUES IN (12) ENGINE = InnoDB) */$$
命名分區
delimiter $$ CREATE TABLE `logging_2012` ( `tag` enum('unknow','login','info','admin','cron','manual') NOT NULL DEFAULT 'unknow' COMMENT '日誌標籤', `asctime` datetime NOT NULL COMMENT '產生時間', `facility` enum('account','bank','unionpay','sms','email','unknow') NOT NULL DEFAULT 'unknow' COMMENT '類別', `priority` enum('info','warning','error','critical','exception','debug') NOT NULL DEFAULT 'debug' COMMENT '級別', `message` varchar(512) NOT NULL COMMENT '內容', `operator` varchar(50) NOT NULL DEFAULT 'computer' COMMENT '操作者' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 /*!50100 PARTITION BY LIST (MONTH(asctime)) SUBPARTITION BY KEY (facility) (PARTITION January VALUES IN (1) ENGINE = InnoDB, PARTITION February VALUES IN (2) ENGINE = InnoDB, PARTITION March VALUES IN (3) ENGINE = InnoDB, PARTITION April VALUES IN (4) ENGINE = InnoDB, PARTITION May VALUES IN (5) ENGINE = InnoDB, PARTITION June VALUES IN (6) ENGINE = InnoDB, PARTITION July VALUES IN (7) ENGINE = InnoDB, PARTITION August VALUES IN (8) ENGINE = InnoDB, PARTITION September VALUES IN (9) ENGINE = InnoDB, PARTITION October VALUES IN (10) ENGINE = InnoDB, PARTITION November VALUES IN (11) ENGINE = InnoDB, PARTITION December VALUES IN (12) ENGINE = InnoDB) */$$