Mysql
 SQL >> Base de Dados >  >> RDS >> Mysql

Como acelerar SELECT em tabelas MySQL muito grandes

Para grandes volumes, prefira InnoDB e uma chave primária composta adequada:

create table tag_date_value ( tag_id smallint unsigned not null, tag_date datetime not null, value int unsigned not null default 0, primary key (tag_id, tag_date) ) engine=innodb;

A query mantém-se simples:

select tag_date, value from tag_date_value where tag_id = 1 and tag_date between 'x' and 'y' order by tag_date;

Não use ALTER TABLE para converter MyISAM para InnoDB em tabelas enormes. Exporte para CSV e reimporte numa nova tabela InnoDB, ordenando os dados durante a exportação.

select * into outfile 'data.dat' fields terminated by '|' lines terminated by '\r\n' from tag_date_value where tag_id between 1 and 50 order by tag_id, tag_date;

Depois importe na mesma ordem:

load data infile 'data.dat' into table tag_date_value fields terminated by '|' lines terminated by '\r\n' (tag_id, tag_date, value);