sql - Optimize WHERE clause in query -


i have following query:

select table2.serialcode,        p.name,        date,        power,        behind,        direction,        length,        centerlongitude,        centerlatitude,        currentlongitude,        currentlatitude table1 table2   join pivots p on p.serialcode = table2.serial table2.serialcode = '49257'  , date = (select max(a.date) table1 a.serialcode ='49257'); 

it seems retrieving select max subquery each join. takes lot of time. there way optimize it? appreciated.

sub selects end being evaluated "per row of main query" can cause tremendous performance problems once try scale larger number of rows.

sub selects can eliminated data model tweak.

here's 1 approach: add new is_latest table track if it's max value (and ties, use other fields created time stamp or row id). set 1 if true, else 0.

then can add where is_latest = 1 query , radically improve performance.

you can schedule update happen or add trigger etc. if need automated way of keeping is_latest date.

other approaches involve 2 tables - 1 keep latest record , table keep history.


Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -