This is a discussion on Performance issue with sub query within the MySQL forums, part of the Database Server Software category; --> I have this query that runs horribly slow due to the sub queries I am using. select s. * ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| I have this query that runs horribly slow due to the sub queries I am using. select s. * , f. * , md.discount as man_disc , md.charge as man_upcharge , dd.discount as drug_disc , dd.charge as drug_upcharge , ep.discount as ep_discount_man , ( select f.ship_fee / count( * ) from scripts where cforder_id = s.cforder_id ) as ship_cost, (select count( * ) from scripts where cforder_id = s.cforder_id) as order_count from scripts s inner join client_fees f on s.customer_id = f.customer_id left join client_man_discounts md on md.man = s.man and md.customer_id = s.customer_id left join client_drug_discounts dd on s.drugid = dd.drug_id left join client_ep_discounts ep on ep.customer_id = s.customer_id and s.man = ep.man where s.customer_id = 7 and s.status = 4 and s.ship_date is not null and s.ship_date != '0000-00-00' and s.ship_date between '2007-10-01' and '2007-10-09'; I need the information provided by the sub queries and I was hoping to avoid using three separate queries- that's why I used these sub's. Each one adds ~ 5 seconds to the execution time of this query. I know sub queries are inefficient, but I have used them before without this much of a performance hit. My tables are InnoDB and I have done my joins on the PK or FK of my tables. Is there a better way to handle this? |