sql >> Base de Datos >  >> RDS >> Mysql

Mysql mejora la velocidad SELECT

tómate el tiempo de leer mi respuesta aquí:(tiene volúmenes similares a los tuyos)

500 millones de filas, escaneo de rango de 15 millones de filas en 0,02 segundos.

MySQL y NoSQL:Ayúdame a elegir el adecuado

luego modifique su motor de tabla a innodb de la siguiente manera:

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;

en su lugar, podría considerar lo siguiente como la clave principal:

primary key (tag_id, tag_date, value) -- added value save some I/O

¡pero solo si el valor no es un tipo de varchar GRANDE!

consulta como antes:

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

espero que esto ayude :)

EDITAR

oh, olvidé mencionarlo:no use alterar tabla para cambiar el tipo de motor de mysiam a innodb, sino que descargue los datos en archivos csv y vuelva a importarlos en una tabla innodb recién creada y vacía.

tenga en cuenta que estoy ordenando los datos durante el proceso de exportación:¡los índices agrupados son la CLAVE!

Exportar

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...

Importar

volver a importar a la tabla en el orden correcto!

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...