mysql - What is the best way to optimize this sql query -
i have following sql query, noticed it's putting pressure on server since every time run it, cpu usage jumps 20%.
select c.name, c.billingaddress, c.billingcity, c.billingstate, c.billingzip,c.ifactive, (select count(l.id) newloads l l.idcompany = c.id , l.smallstatus='1') numberloads, (select (sum(l.loadrate))/(sum(l.esmiles)) newloads l l.idcompany = c.id , l.loadrate != '0' , l.esmiles != '0' , l.smallstatus='1') rpm `companies` c ifcontractor ='0' $cond order numberloads desc
this might more efficient:
select c.name, c.billingaddress, c.billingcity, c.billingstate, c.billingzip, c.ifactive, x.numberloads, x.rpm ( select l.idcompany, count(*) numberloads, sum(l.loadrate))/(sum(l.esmiles) rpm newloads l l.smallstatus = '1' ) x join companies c on c.id = x.idcompany ifcontractor = '0' $cond order x.numberloads desc;
please provide show create table
, explain select ...
.
Comments
Post a Comment