明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

MySQL分區(qū)字段列有必要再單獨建索引嗎?

[摘要]大家都知道對于分區(qū)字段必須是主鍵的一部分,那么建了復(fù)合主鍵之后,是否需要對分許字段再單獨添加一個索引呢?有沒有效果?本文主要給大家介紹了關(guān)于MySQL分區(qū)字段列是否有必要再單獨建索引的相關(guān)資料,文中...
大家都知道對于分區(qū)字段必須是主鍵的一部分,那么建了復(fù)合主鍵之后,是否需要對分許字段再單獨添加一個索引呢?有沒有效果?本文主要給大家介紹了關(guān)于MySQL分區(qū)字段列是否有必要再單獨建索引的相關(guān)資料,文中通過示例進行了驗證,對大家的理解和學(xué)習(xí)具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

1、新建表effect_new(以創(chuàng)建時間按月分區(qū))


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、插入部分數(shù)據(jù)數(shù)據(jù),


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


結(jié)果為:


idselect_typetablepartitionstpyepossible_keyskeykey_lenrefrowsfilteredextra
1SIMPLEeffect_newp8ALLnullnullnullnull39151510Using where

4、給表effect_new添加索引idx_ctime

5、分析添加索引后的執(zhí)行計劃

結(jié)果為:


idselect_typetablepartitionstpyepossible_keyskeykey_lenrefrowsfilteredextra
1SIMPLEeffect_newp8refidx_ctimeidx_ctime5const60760100null

6、結(jié)論:

雖然表已經(jīng)根據(jù)此字段分區(qū),但這不能等同于索引。分了區(qū),只能說該字段為某個值的記錄會在某個分區(qū)里面,但不是索引,還要一頓好找。

有時候,主鍵不等于分區(qū)依據(jù)列,這時候主鍵又想建聚集索引的話,那么必須包含分區(qū)依據(jù)列,搞成復(fù)合主鍵。那么,這種情況下,分區(qū)依據(jù)列不就有索引了嗎?是的,可是它不夠快,如果在這個復(fù)合索引里面,分區(qū)依據(jù)列不排在第一位,就不夠快,如果查找語句里常常用分區(qū)依據(jù)列作為過濾條件,就有必要為分區(qū)依據(jù)列額外單獨建立一個索引。

相關(guān)推薦:

MYSQL的分區(qū)字段,必須包含在主鍵字段內(nèi)_MySQL

MySQL不同存儲引擎和不同分區(qū)字段對于查詢的影響_MySQL

MySQL分區(qū)表partition線上修改分區(qū)字段_MySQL

以上就是MySQL分區(qū)字段列有必要再單獨建索引嗎?的詳細內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!


學(xué)習(xí)教程快速掌握從入門到精通的SQL知識。