ballhogjoni Posted January 6, 2009 Share Posted January 6, 2009 Can anyone think of a way to optimize this query?: SELECT sum(o.total) as gross_sales FROM travel_orders AS o WHERE o.company_id != '0' AND (o.charge_date BETWEEN '1199163600' AND '1230699600') AND o.status IN ('charge_held','charged','filled','in_process','processed') I am using explain and my type is ALL, so I want to get it to use something else. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/139745-query-optimization/ Share on other sites More sharing options...
corbin Posted January 7, 2009 Share Posted January 7, 2009 What indexes do you have on the table? Can you show us: SHOW CREATE TABLE <table name>; and EXPLAIN SELECT sum(o.total) as gross_sales FROM travel_orders AS o WHERE o.company_id != '0' AND (o.charge_date BETWEEN '1199163600' AND '1230699600') AND o.status IN ('charge_held','charged','filled','in_process','processed'); First thing that comes to mind is no indexes. Second thought is a full table scan on SUM(o.total) (which would be related to no-indexes). Third is o.status being compared to strings.... Quote Link to comment https://forums.phpfreaks.com/topic/139745-query-optimization/#findComment-731326 Share on other sites More sharing options...
ballhogjoni Posted January 7, 2009 Author Share Posted January 7, 2009 Can you show us: SHOW CREATE TABLE <table name>; CREATE TABLE `travel_orders` ( `id` int(10) unsigned NOT NULL auto_increment, `company_id` int(10) unsigned NOT NULL default '0', `status` enum('cc_pending','incomplete','new','cancel','printed','partially_charged','charged','charge_cancel','filled','processed','proc_cancel','held','charge_held') NOT NULL default 'cc_pending', `charge_date` int(10) unsigned NOT NULL default '0', `total` float NOT NULL default '0', PRIMARY KEY (`id`), KEY `order_status` (`status`), KEY `company_idx` (`company_id`), KEY `charge_date` (`charge_date`) ) ENGINE=MyISAM AUTO_INCREMENT=133023 DEFAULT CHARSET=latin1 I did not show the entire table or all of the keys because its private, but you now have the relevant info. EXPLAIN: 1,SIMPLE,o,ALL,(order_status,status_affiliate,company_idx,charge_date),N,N,N,651,Using where Each column is comma seperated. Quote Link to comment https://forums.phpfreaks.com/topic/139745-query-optimization/#findComment-731623 Share on other sites More sharing options...
fenway Posted January 7, 2009 Share Posted January 7, 2009 Drop the != condition for a minute... it shouldn't be ALL. Quote Link to comment https://forums.phpfreaks.com/topic/139745-query-optimization/#findComment-731747 Share on other sites More sharing options...
ballhogjoni Posted January 7, 2009 Author Share Posted January 7, 2009 Drop the != condition for a minute... it shouldn't be ALL. Tried that and it didn't change anything. Quote Link to comment https://forums.phpfreaks.com/topic/139745-query-optimization/#findComment-731755 Share on other sites More sharing options...
fenway Posted January 8, 2009 Share Posted January 8, 2009 Hmmm... I just want to make sure that the indexes are working as expected. Drop the IN() too... ALL should switch to RANGE. Quote Link to comment https://forums.phpfreaks.com/topic/139745-query-optimization/#findComment-732260 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.