1. 程式人生 > 資料庫 >MySQL分割槽欄位列有必要再單獨建索引嗎?

MySQL分割槽欄位列有必要再單獨建索引嗎?

前言

大家都知道對於分割槽欄位必須是主鍵的一部分,那麼建了複合主鍵之後,是否需要對分許欄位再單獨新增一個索引呢?有沒有效果?來驗證一下,下面話不多說了,來一起看看詳細的介紹吧。

1、新建表effect_new(以建立時間按月分割槽)

CREATE TABLE `effect_new` (
 `id` bigint(20) NOT NULL AUTO_INCREMENT,`type` tinyint(4) NOT NULL DEFAULT '0',`timezone` varchar(10) DEFAULT NULL,`date` varchar(10) NOT NULL,`hour` varchar(2) DEFAULT NULL,`position` varchar(200) DEFAULT NULL,`country` varchar(32) NOT NULL,`create_time` datetime NOT NULL DEFAULT '1970-01-01 00:00:00',PRIMARY KEY (`id`,`create_time`),KEY `index_date_hour_coun` (`date`,`hour`,`country`)
) ENGINE=InnoDB AUTO_INCREMENT=983041 DEFAULT CHARSET=utf8
PARTITION BY RANGE (TO_DAYS (`create_time`))
(PARTITION p0 VALUES LESS THAN (736754) ENGINE = InnoDB,PARTITION p1 VALUES LESS THAN (736785) ENGINE = InnoDB,PARTITION p2 VALUES LESS THAN (736815) ENGINE = InnoDB,PARTITION p3 VALUES LESS THAN (736846) ENGINE = InnoDB,PARTITION p4 VALUES LESS THAN (736876) ENGINE = InnoDB,PARTITION p5 VALUES LESS THAN (736907) ENGINE = InnoDB,PARTITION p6 VALUES LESS THAN (736938) ENGINE = InnoDB,PARTITION p7 VALUES LESS THAN (736968) ENGINE = InnoDB,PARTITION p8 VALUES LESS THAN (736999) ENGINE = InnoDB,PARTITION p9 VALUES LESS THAN (737029) ENGINE = InnoDB,PARTITION p10 VALUES LESS THAN (737060) ENGINE = InnoDB);

2、插入部分資料資料,

INSERT INTO `effect_new` (`id`,`type`,`timezone`,`date`,`position`,`country`,`create_time`) VALUES ('1','0','GMT+8','2017-07-01','','M-NotiCleanFull-FamilyRecom-0026','2017-07-02 00:07:02');
INSERT INTO `effect_new` (`id`,`create_time`) VALUES ('2','1','2017-09-30','23','Ma5dtJub','EG','2017-10-01 00:00:00');
INSERT INTO `effect_new` (`id`,`create_time`) VALUES ('3','2017-09-10','10','28','DZ','2017-09-11 00:08:20');
INSERT INTO `effect_new` (`id`,`create_time`) VALUES ('4','2017-02-03','20','32','AD','2017-02-04 00:00:00');
INSERT INTO `effect_new` (`id`,`create_time`) VALUES ('5','2017-03-05','2',NULL,'AI','2017-03-06 02:10:00');
INSERT INTO `effect_new` (`id`,`create_time`) VALUES ('6','2017-09-23','13','M-BrandSplash-S-0038','AG','2017-09-23 13:00:00');
INSERT INTO `effect_new` (`id`,`create_time`) VALUES ('7','2017-10-13','12','BB-Main-AppAd-0018','AF','2017-10-14 12:00:00');
INSERT INTO `effect_new` (`id`,`create_time`) VALUES ('8','2017-10-28','M-ChargeReminder-S-0040','AE','2017-10-29 00:00:00');
INSERT INTO `effect_new` (`id`,`create_time`) VALUES ('9','2017-10-09','30','2017-10-10 00:09:00');
INSERT INTO `effect_new` (`id`,`create_time`) VALUES ('10','2017-10-05','5',' M-BrandSplash','LA','2017-10-06 05:10:00');

3、分析語句

EXPLAIN PARTITIONS
select * from effect_new_index
where create_time = '2017-10-14 12:00:00' 

結果為:

id select_type table partitions tpye possible_keys key key_len ref rows filtered extra
1 SIMPLE effect_new p8 ALL null null null null 391515 10 Using where

4、給表effect_new新增索引idx_ctime

5、分析新增索引後的執行計劃

結果為:

id select_type table partitions tpye possible_keys key key_len ref rows filtered extra
1 SIMPLE effect_new p8 ref idx_ctime idx_ctime 5 const 60760 100 null

6、結論:

雖然表已經根據此欄位分割槽,但這不能等同於索引。分了區,只能說該欄位為某個值的記錄會在某個分割槽裡面,但不是索引,還要一頓好找。

有時候,主鍵不等於分割槽依據列,這時候主鍵又想建聚集索引的話,那麼必須包含分割槽依據列,搞成複合主鍵。那麼,這種情況下,分割槽依據列不就有索引了嗎?是的,可是它不夠快,如果在這個複合索引裡面,分割槽依據列不排在第一位,就不夠快,如果查詢語句裡常常用分割槽依據列作為過濾條件,就有必要為分割槽依據列額外單獨建立一個索引。

總結

以上就是這篇文章的全部內容了,本文還有許多不足,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對我們的支援。