I'm trying to join 2 fairly large tables. One of which I've put a compound index on one of them, to try and speed things up. However I can't seem to get the query to utilise the compound index.
Could someone suggest what changes I could make to the indexes or the query to make it run any faster (currently about 0.75s. I'd like it faster).
you can see from the EXPLAIN results that the combined index on sale_date_by_group isn't being utilised.Code:CREATE TABLE `product_group` ( `prodGroupID` bigint(20) unsigned NOT NULL auto_increment, `productID` bigint(20) unsigned default NULL, `groupType` varchar(10) default NULL, `groupID` bigint(20) unsigned default NULL, PRIMARY KEY (`prodGroupID`), KEY `productID` (`productID`), KEY `groupID` (`groupID`), KEY `groupType` (`groupType`), KEY `type_id` (`groupType`,`groupID`) ) CREATE TABLE `sale_date_by_group` ( `unique_key` varchar(100) NOT NULL default '', `groupType` varchar(10) default NULL, `groupID` int(11) default NULL, `date` date default NULL, `salesChannelID` double(5,1) default NULL, `units` int(11) default NULL, `turnover` double(10,2) default NULL, `cost` double(10,2) default NULL, PRIMARY KEY (`unique_key`), KEY `date` (`date`), KEY `salesChannelID` (`salesChannelID`), KEY `combined_index` (`groupID`,`groupType`,`date`,`salesChannelID`) ) SELECT pg.productID, SUM(s.units)units FROM sale_date_by_group s, product_group pg WHERE pg.groupType='store' AND pg.groupID=42 AND pg.productID=s.groupID AND s.groupType='product' AND s.date>CURDATE() - INTERVAL 30 DAY GROUP BY pg.productID order by units desc limit 5 The EXPLAIN outputs: id select_type table type possible_keys key key_len ref rows Extra 1 SIMPLE s range date,combined_index date 4 NULL 463359 Using where; Using temporary; Using filesort 1 SIMPLE pg ref productID,groupID,groupType,type_id productID 9 hsdmain.s.groupID 1 Using where
Any pointers on improving the indexing or the query itself to make it faster?








Bookmarks