1. 程式人生 > >mysql分割槽實操

mysql分割槽實操

分成2步:

1.要新建與原表一樣的新表,在建立新表時(create)同時分割槽,不能直接對原表分割槽

2.將原表資料插入新表

INSERT INTO 目標表 SELECT * FROM 來源表;

CREATE TABLE met_shopv2_order_copy1  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `orderid` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL COMMENT '訂單ID',
  `type` int(1) NOT NULL COMMENT '訂單型別',
  `uid` int(11) NOT NULL COMMENT '使用者ID',
  `username` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '使用者名稱',
  `state` int(2) NOT NULL COMMENT '訂單狀態',
  `tel` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '電話',
  `email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '郵件',
  `message` varchar(1000) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '訂單留言',
  `address` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '收貨地址',
  `price` double NOT NULL COMMENT '商品金額',
  `cprice` double NOT NULL COMMENT '修改總金額',
  `tprice` double NOT NULL COMMENT '訂單總金額',
  `discount` double NOT NULL COMMENT '折扣價格',
  `discount_info` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '折扣資訊',
  `discount_use` int(1) NOT NULL COMMENT '修改總金額後是否可以繼續使用折扣',
  `freight` double NOT NULL COMMENT '運費',
  `cinfo` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '物流公司',
  `oinfo` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '物流單號',
  `paytype` int(1) NOT NULL COMMENT '付款方式',
  `payinfo` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '付款資訊',
  `invoice` int(1) NOT NULL COMMENT '是否需要發票',
  `invoice_info` varchar(500) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '發票資訊',
  `remark` varchar(1000) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '備註',
  `rtime` int(11) NOT NULL COMMENT '下訂單時間',
  `ptime` int(11) NOT NULL COMMENT '付款時間',
  `stime` int(11) NOT NULL COMMENT '發貨時間',
  `ctime` int(11) NOT NULL COMMENT '關閉訂單時間',
  `search` text CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '資訊搜尋欄位',
  `lang` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '所屬語言',
  PRIMARY KEY (id,ptime) USING BTREE,//原表中只有id是主鍵,但分表的話,是要求分表條件也是主鍵
INDEX `orderid`(`orderid`) USING BTREE, INDEX `idx_shop_order_uid`(`uid`) USING BTREE, INDEX `id`(`id`) USING BTREE, INDEX `uid`(`uid`) USING BTREE, INDEX `state`(`state`) USING BTREE ) ENGINE = MyISAM CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic PARTITION BY RANGE (ptime)//range後是表分割槽的條件欄位.
( PARTITION p_201803 VALUES LESS THAN (1522512000),//格式化過的時間 PARTITION p_201804 VALUES LESS THAN (1525104000), PARTITION p_201805 VALUES LESS THAN (1527782400), PARTITION p_Dec VALUES LESS THAN MAXVALUE ); INSERT INTO met_shopv2_order_copy1 SELECT * FROM met_shopv2_order;

總結:按月份進行的分表,按照月份內查詢資料的話,速度是會高於原表.

但同時查幾個月的資料時,原錶速度反而略高於分表後的資料.

參考資料: