Aquí está mi problema, estoy seleccionando y haciendo varias combinaciones para obtener los elementos correctos ... atrae una buena cantidad de filas, por encima de 100.000. Esta consulta lleva más de 5 minutos cuando el rango de fechas se establece en 1 año.¿Cómo puedo acelerar la consulta de MySQL con varias combinaciones?
No sé si es posible, pero me temo que el usuario podría extender el intervalo de fechas a diez años y bloquearlo.
¿Alguien sabe cómo puedo acelerar esto? Aquí está la consulta.
SELECT DISTINCT t1.first_name, t1.last_name, t1.email
FROM table1 AS t1
INNER JOIN table2 AS t2 ON t1.CU_id = t2.O_cid
INNER JOIN table3 AS t3 ON t2.O_ref = t3.I_oref
INNER JOIN table4 AS t4 ON t3.I_pid = t4.P_id
INNER JOIN table5 AS t5 ON t4.P_cat = t5.C_id
WHERE t1.subscribe =1
AND t1.Cdate >= $startDate
AND t1.Cdate <= $endDate
AND t5.store =2
¡No soy el mejor con mysql así que cualquier ayuda sería apreciada!
¡Gracias de antemano!
ACTUALIZACIÓN
Aquí se explica el que pidió
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t5 ref PRIMARY,C_store_type,C_id,C_store_type_2 C_store_type_2 1 const 101 Using temporary
1 SIMPLE t4 ref PRIMARY,P_cat P_cat 5 alphacom.t5.C_id 326 Using where
1 SIMPLE t3 ref I_pid,I_oref I_pid 4 alphacom.t4.P_id 31
1 SIMPLE t2 eq_ref O_ref,O_cid O_ref 28 alphacom.t3.I_oref 1
1 SIMPLE t1 eq_ref PRIMARY PRIMARY 4 alphacom.t2.O_cid 1 Using where
También he añadido un índice para Tabla5 filas y filas Table4 porque realmente no cambian, sin embargo las otras mesas conseguir alrededor 500-1000 entradas al mes ... Escuché que debería agregar un índice a una tabla que tenga tantas entradas nuevas ... ¿es cierto?
Sería útil conocer las tablas y la indexación en su lugar: actualice para incluir las instrucciones CREATE TABLE. La necesidad de 'DISTINCT' hace que me pregunte qué tabla (s) son responsables de necesitar eso. –
Un EXPLAIN ayudaría también. – ceejayoz
-OMG Ponies, no sé a qué te refieres con las sentencias create table. -Ceejayoz, agregué el EXPLAIN – BinarySolo00100