花点时间在这里阅读我的答案:(与您的内容相似)
5 亿行,1500 万行范围扫描在 0.02 秒内。
MySQL 和 NoSQL:帮我选一个合适的
然后将您的表引擎修改为 innodb,如下所示:
create table tag_date_value
(
tag_id smallint unsigned not null, -- i prefer ints to chars
tag_date datetime not null, -- can we make this date vs datetime ?
value int unsigned not null default 0, -- or whatever datatype you require
primary key (tag_id, tag_date) -- clustered composite PK
)
engine=innodb;
您可能会将以下内容视为主键:
primary key (tag_id, tag_date, value) -- added value save some I/O
但前提是 value 不是一些 LARGE varchar 类型!
像以前一样查询:
select
tag_date,
value
from
tag_date_value
where
tag_id = 1 and
tag_date between 'x' and 'y'
order by
tag_date;
希望这可以帮助 :)
编辑
哦忘了提 - 不要使用 alter table 将引擎类型从 mysiam 更改为 innodb,而是将数据转储到 csv 文件中,然后重新导入到新创建的空 innodb 表中。
请注意,我在导出过程中订购数据 - 聚集索引是关键!
出口
select * into outfile 'tag_dat_value_001.dat'
fields terminated by '|' optionally enclosed by '"'
lines terminated by '\r\n'
from
tag_date_value
where
tag_id between 1 and 50
order by
tag_id, tag_date;
select * into outfile 'tag_dat_value_002.dat'
fields terminated by '|' optionally enclosed by '"'
lines terminated by '\r\n'
from
tag_date_value
where
tag_id between 51 and 100
order by
tag_id, tag_date;
-- etc...
进口
以正确的顺序导入表中!
start transaction;
load data infile 'tag_dat_value_001.dat'
into table tag_date_value
fields terminated by '|' optionally enclosed by '"'
lines terminated by '\r\n'
(
tag_id,
tag_date,
value
);
commit;
-- etc...