思路话语

。Arlen:思想有多远你就能走多远...

mysql 索引 索引长度 fulltext 字符集

1. myisam类型表的索引总长度不能超过1000个bytes。innodb索引总长度则不能超过767bytes。

   在uft8下,定义字段时,如field varchar(n), key(field),则n*3不能超过1000

   在gbk下,定义字段时,如field varchar(n), key(field),则n*2不能超过1000

   在latin1下,定义字段时,如field varchar(n), key(field),则n*1不能超过1000

这个key可以不管是fulltext还是普通的key和unique key都有这样的限制。当然,如果是定义成普通的key,则在创建表时可能不会报错,因为mysql会自动取前面1000个bytes来创建索引。

试验:

alter table test convert to charset utf8;
create index test_name3 on test(name3).
此时warning:Specified key was too long;max key length is 1000 bytes.但是索引创建成功,查看表结构可以看到创建的索引是一个前缀索引:‘key test_name3(name3(333))’

结论:

对于myisam表,如果创建组合索引,所创建的索引长度和不能超过1000 bytes,否则会报错,创建失败;对于myisam的单列索引,最大长度也不能超过1000,否则会报警,但是创建成功,最终创建的是前缀索引(utf8下取前333个character,注意,不是bytes)。 

 

测试:
create table test1(id int,name1 varchar(300),name2 varchar(300),name3 varchar(500))charset=latin1 engine=innodb;
create index test1_name on test(name1,name2,name3);
此时给出warning:Specified key was too long;max key length is 767 bytes.

修改表结构:alter table test1 convert to charset utf8;
create index test1_name3 on test(name3).
此时给出warning:Specified key was too long;max key length is 767 bytes.

 

再测试:

create table testft
(
id int,
title1 varchar(1000) not null,
title2 varchar(10) not null,
key(title1,title2)
)engine=innodb default charset=latin1;

 

show create table出来得到如下:

CREATE TABLE `testft` (
  `id` int(11) default NULL,
  `title1` varchar(1000) NOT NULL,
  `title2` varchar(10) NOT NULL,
  KEY `title1` (`title1`(767),`title2`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1


结论:对于创建innodb的组合索引,如果各个列中的长度不超过767,则不再计算所有列的总长度,如果有超过767的,则给出报警,索引最后创建成功,但是对于超过767字节的列取前缀索引;对于innodb的单列索引,超过767的,给出warning,最终索引创建成功,取前缀索引(utf8下取前255个character)。 

 

2. fulltext索引,不仅是中文分词很糟糕,英文分词也不理想。

而且目前只有myisam支持fulltext索引,仅有char,varchar,text三种数据类型可以建fulltext索引

测试:

drop table if exists testft;
create table testft
(
id int,
title varchar(1000) not null,
fulltext(title)
)engine=myisam default charset=latin1;

 

insert into testft(id,title)values(1,'i am arlen');
insert into testft(id,title)values(1,'it is my book');
insert into testft(id,title)values(1,'影帝 刘德华');

select * from testft where match(title) against('am')
select * from testft where match(title) against('arlen') #只有这一项查了出来
select * from testft where match(title) against('book')
select * from testft where match(title) against('is')
select * from testft where match(title) against('it')
select * from testft where match(title) against('影帝')

 

 

 

posted on 2010-06-02 13:11  Arlen  阅读(1009)  评论(0编辑  收藏  举报

导航