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

2021-09-14 08:00:04 字數 4191 閱讀 6646

對於分割槽字段必須是主鍵的一部分,那麼建了復合主鍵之後,是否需要對分許字段再單獨新增乙個索引呢?有沒有效果?下面來驗證一下

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);

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 ('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');

explain partitions

select * from effect_new_index

where create_time = '2017-10-14 12:00:00'

結果為:

idselect_type

table

partitions

tpye

possible_keys

keykey_len

refrows

filtered

extra

1******

effect_new

p8all

null

null

null

null

391515

10using where

結果為:

idselect_type

table

partitions

tpye

possible_keys

keykey_len

refrows

filtered

extra

1******

effect_new

p8ref

idx_ctime

idx_ctime

5const

60760

100null

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

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

TEXT欄位是否有必要拆分成獨立表?

最近不止一次的被問及這麼乙個問題 乙個含有text欄位的寬表,是否有必要把text拆分出去作為乙個獨立的表,來提高效能?下面談談我個人的看法 一般來說,將text欄位,從一張操作頻繁的表中拆分出去,成為乙個key value結構的獨立表是 好處頗多的。其有利之處主要體現在下面三個方面 ps 以下的討...

mysql欄位是否存在 mysql判斷列是否存在

本篇文章將通過儲存過程來判斷列 字段 是否存在。請看詳情。判斷字段是否存在 drop procedure if exists schema change delimiter create procedure schema change begin declare currentdatabase va...

Mysql區分度很差是否有必要建索引

一般而言區分度很差的字段沒必要建索引,但是這種也要視情況而定。儲存引擎底層,我們經常聽到,不走索引而走全表掃瞄。那麼底層為什麼不走索引而走全表掃瞄。這裡涉及到聚集索引和非聚集索引,對於非聚集索引值,查詢的字段如果包含非索引字段,就會通過聚集索引主鍵從聚集索引中查詢出需要查詢出來的資料,也就是我們常說...