MySQL query take too long -
mysql server give no reply , hanged after following query:
select i.id id, i.name product, i.stock stock, if(sum(s1.qty) null, 0, sum(s1.qty)) thisqty, if(sum(s2.qty) null, 0, sum(s2.qty)) lastqty, if(sum(s3.qty) null, 0, sum(s3.qty)) last2qty, if(sum(s4.qty) null, 0, sum(s4.qty)) last3qty item i, sale s1, sale s2, sale s3, sale s4, odr o1, odr o2, odr o3, odr o4 i.id = s1.itemid , s1.oui = o1.oui , (o1.ddate between '2016-02-08' , '2016-02-14') , i.id = s2.itemid , s2.oui = o2.oui , (o2.ddate between '2016-02-01' , '2016-02-07') , i.id = s3.itemid , s3.oui = o3.oui , (o3.ddate between '2016-01-25' , '2016-01-31') , i.id = s4.itemid , s4.oui = o4.oui , (o4.ddate between '2016-01-18' , '2016-01-24') group product;
my table structure follows:
mysql> describe item; +---------+--------------+------+-----+---------+----------------+ | field | type | null | key | default | | +---------+--------------+------+-----+---------+----------------+ | id | int(3) | no | pri | null | auto_increment | | name | varchar(255) | no | | null | | | wprice | int(11) | no | | null | | | sprice | int(11) | no | | null | | | bprice | int(11) | no | | null | | | stock | float | no | | null | | | buyfrom | varchar(255) | no | | null | | | unit | varchar(255) | no | | null | | +---------+--------------+------+-----+---------+----------------+ 8 rows in set (0.03 sec) mysql> describe odr; +--------+-------------+------+-----+---------+-------+ | field | type | null | key | default | | +--------+-------------+------+-----+---------+-------+ | oui | varchar(32) | yes | | null | | | odate | varchar(16) | yes | | null | | | ddate | varchar(16) | yes | | null | | | otime | varchar(16) | yes | | null | | | cid | varchar(6) | yes | | null | | | disc | int(11) | yes | | null | | | total | int(11) | yes | | null | | | net | int(11) | yes | | null | | | status | char(1) | yes | | null | | +--------+-------------+------+-----+---------+-------+ 9 rows in set (0.02 sec) mysql> describe sale; +--------+-------------+------+-----+---------+-------+ | field | type | null | key | default | | +--------+-------------+------+-----+---------+-------+ | oui | varchar(32) | yes | | null | | | itemid | varchar(3) | yes | | null | | | qty | float | yes | | null | | | price | int(11) | yes | | null | | | amount | int(11) | yes | | null | | | profit | float | no | | null | | +--------+-------------+------+-----+---------+-------+ 6 rows in set (0.03 sec)
i tried query s1 , worked. may many ands in clause. tried phpmyadmin , still didnt work
changed query filter data inside sub queries.
try this:
select i.id id, i.name product, i.stock stock, if(sum(s1.qty) null, 0, sum(s1.qty)) thisqty, if(sum(s2.qty) null, 0, sum(s2.qty)) lastqty, if(sum(s3.qty) null, 0, sum(s3.qty)) last2qty, if(sum(s4.qty) null, 0, sum(s4.qty)) last3qty item inner join (select sale.* sale inner join odr on sale.oui=odr.oui odr.ddate between '2016-02-08' , '2016-02-14' ) s1 on i.id = s1.itemid inner join (select sale.* sale inner join odr on sale.oui=odr.oui odr.ddate between '2016-02-01' , '2016-02-07' ) s2 on i.id = s2.itemid inner join (select sale.* sale inner join odr on sale.oui=odr.oui odr.ddate between '2016-01-25' , '2016-01-31' ) s3 on i.id = s3.itemid inner join (select sale.* sale inner join odr on sale.oui=odr.oui odr.ddate between '2016-01-18' , '2016-01-24' ) s4 on i.id = s4.itemid group product;
Comments
Post a Comment