时间:2021-07-01 10:21:17 帮助过:22人阅读
create table dictnn ( id int, word varchar(14) not null default '', key(word) )engine myisam charset utf8;
create table dictyn ( id int, word varchar(14), key(word) )engine myisam charset utf8;
alter table dictnn disable keys; alter table dictyn disable keys;
insert into dictnn select id,if(id%2,word,'') from dict limit 10000; insert into dictyn select id,if(id%2,word,null) from dict limit 10000;
alert table dictnn enable keys; alter table dictyn enable keys;
Enum列的说明
1: enum列在内部是用整型来储存的
2: enum列与enum列相关联速度最快
3: enum列比(var)char的弱势---在碰到与char关联时,要转化.要花时间.
4: 优势在于,当char非常长时,enum依然是整型固定长度.
当查询的数据量越大时,enum的优势越明显.
5: enum与char/varchar关联,因为要转化,速度要比enum->enum,char->char要慢,
但有时也这样用-----就是在数据量特别大时,可以节省IO.
试验:
create table t2 ( id int, gender enum('man','woman'), key(gender) )engine myisam charset utf8;
create table t3 ( id int, gender char(5) not null default '', key(gender) )engine myisam charset utf8;
alter table t2 disable keys; alter table t3 disable keys;
insert into t2 select id,if(id%2,'man','woman') from dict limit 10000; insert into t3 select id,if(id%2,'man','woman') from dict limit 10000;
alter table t2 enable keys; alter table t3 enable keys; mysql> select count(*) from t2 as ta,t2 as tb where ta.gender=tb.gender mysql> select count(*) from t3 as ta,t3 as tb where ta.gender=tb.gender
列<---->列
时间
Enum<--->enum
10.53
Char<---->char
24.65
Enum<---->char
18.22
如果t2表的优势不明显,加大t3的gender列,char(15),
char(20)...
随着t3 gender列的变大,t2表优势逐渐明显.
原因----无论enum(‘manmaman’,’womanwomanwoman’)枚举的字符多长,
内部都是用整型表示, 在内存中产生的数据大小不变,
而char型,却在内存中产生的数据越来越多.
总结: enum 和enum类型关联速度比较快
Enum 类型 节省了IO
以上就是mysql 优化(1)表的优化与列类型选择的内容,更多相关内容请关注PHP中文网(www.gxlcms.com)!