Home  >  Q&A  >  body text

myisam - 关于mysql的定长表

首先,问题是定长表的查询速度为什么会比不定长表会慢
有这么一张大的users表,存储的字段比较多,然后设计成为了主表和副表,使用的存储引擎都是myisam
与前辈聊天,前辈说这种设计需要把主表设计成定长表,这样在进行数据查询的时候速度上会更快,因为每个字段的长度是定长的,所以每条记录的长度也是定长的
但是实际上我在用的时候,发现定长表并没有更快(以下是两张myisam的表,数据量为140万+):
定长表,以下简称a表:

CREATE TABLE `users_myisam_fixed` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` char(100) COLLATE utf8_unicode_ci NOT NULL,
  `email` char(100) COLLATE utf8_unicode_ci NOT NULL,
  `password` char(60) COLLATE utf8_unicode_ci NOT NULL,
  `remember_token` char(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `age` tinyint(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1544258 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

不定长表,以下简称b表:

CREATE TABLE `users_myisam` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `password` varchar(60) COLLATE utf8_unicode_ci NOT NULL,
  `remember_token` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `age` tinyint(4) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1544258 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci    

以上,表中都没有添加索引。
在两个表中分别执行select * from {$tablename} 的时候:a表耗时4.302s,b表耗时3.484s 定长表反而更慢。。
在分别执行select * from {$tablename} where name='may25',select * from {$tablename} where name like 'a%' 的时候,还是a表所用时间更长

之前在学习mysql的时候也知道charvarchar是存在查询上的差距的,但是并没有实际的尝试过,谁能给我解释一下为什么我在尝试的时候定长表反而会更慢?没有添加索引的原因?还是什么的别的原因。。。一脸懵逼求大神

PHPzPHPz2743 days ago801

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-17 15:17:27

    I have not studied the specific bottom layer. I guess that the fixed length can prevent a large number of fragmented files from being generated on the mechanical hard disk and increase the seek time during search.
    If you have an SSD, this should not be a problem.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 15:17:27

    If there are many nulls in your data, then the length occupied by varchar will be less than that of char, and the length must be redundant in your myisam table, then at this time, the space occupied by varchar will be less than that of cahr, because Varchar saves data based on actual occupancy. The more space it occupies, the search time will increase accordingly during each query, which will cause the query time to increase.

    reply
    0
  • Cancelreply