/ / Date = max (Date) Running Slowly - mysql, sql

Date = max (Date) Running Slowly - mysql, sql

Ho circa 6 milioni di dischi neltabella storico_data. Sto cercando di trovare tutti i simboli in cui l'ultima riga registrata non corrisponde alla data più alta sul tavolo. Credo che questa query funzionerà ma ci vorrà un'eternità per funzionare. Così a lungo, infatti, ogni volta la connessione viene interrotta. C'è un modo per rendere questa query più veloce?

select symbol, histDate
from historical_data as a
where histDate =
(select max(histDate)
from historical_data as b
where a.symbol = b.symbol);

risposte:

1 per risposta № 1

Vuoi un indice per questa query. L'indice migliore è historical_data(symbol, histdate).

Potresti trovare più veloce per esprimere la query come:

select hd.*
from historical_data hd join
(select symbol, max(histDate) as maxhd
from historical_data
group by symbol
) s
on hd.histDate = s.histDate;

MODIFICARE:

Ops. La tua query di esempio non esegue ciò che il testo dice che vuoi.

select symbol
from historical_data hd cross join
(select max(histDate) as maxhd from historical_data) m
group by symbol, maxhd
having max(hd.histDate) <> maxhd;