mysql - Query runs very slow -
i have query runs slow. goal of query list articles table , quantity table artikli. want see article sales listed in tables (artikli_2016 year, , artikli_2015 previous year)
select a.artid, a.artsifra, a.artnaziv, a.artstanje, a.artstanjepalete, ( a.artstanje + a.artstanjepalete ) total, sum(g.izlaz) promet, c.jmoznaka, sum(f.izlaz) promet artikli left join artikli_2016 g on a.artid = g.artikal left join artikli_2015 f on a.artid = f.artikal left join jed_mjere c on a.artmjera = c.jmid a.artroditelj != 0 , a.artaktivan group a.artid order a.artid asc
once join artikli_2015 gets slow. helpful if can give me advice or solution run query faster.
due 2 lots of multiple records (i suspect) need use couple of sub queries sums. assuming jed_mjere has @ 1 record each artmjera.
select a.artid, a.artsifra, a.artnaziv, a.artstanje, a.artstanjepalete, (a.artstanje + a.artstanjepalete) total, g.promet, c.jmoznaka, f.promet artikli left outer join ( select artikal, sum(izlaz) promet artikli_2016 group artikal ) g on a.artid = g.artikal left outer join ( select artikal, sum(izlaz) promet artikli_2015 group artikal ) f on a.artid = f.artikal left outer join jed_mjere c on a.artmjera = c.jmid a.artroditelj != 0 , a.artaktivan order a.artid asc
if query dealing small number of records artikli (ie a.artroditelj != 0 , a.artaktivan excludes rows) maybe worth doing join exclude records in sub queries.
select a.artid, a.artsifra, a.artnaziv, a.artstanje, a.artstanjepalete, (a.artstanje + a.artstanjepalete) total, g.promet, c.jmoznaka, f.promet artikli left outer join ( select g.artikal, sum(g.izlaz) promet artikli inner join artikli_2016 g on a.artid = g.artikal a.artroditelj != 0 , a.artaktivan group g.artikal ) g on a.artid = g.artikal left outer join ( select f.artikal, sum(f.izlaz) promet artikli inner join artikli_2015 f on a.artid = g.artikal a.artroditelj != 0 , a.artaktivan group f.artikal ) f on a.artid = f.artikal left outer join jed_mjere c on a.artmjera = c.jmid a.artroditelj != 0 , a.artaktivan order a.artid asc
Comments
Post a Comment