gnznroses Posted February 10, 2010 Share Posted February 10, 2010 EXPLAIN SELECT campaign FROM clicks WHERE user = 123 GROUP BY campaign I get "Using where; Using temporary; Using filesort" (ref=const, key and possible keys=user, rows=hundreds of thousands) campaign is an unsigned int - it is indexed (with a cardinality of about 2000) user is an unsigned int - it is indexed also this makes my query very slow. any way to fix this? thanks PS, I read this (http://dev.mysql.com/doc/refman/5.0/en/group-by-optimization.html) but it's a bit hard to follow what they're saying. Quote Link to comment https://forums.phpfreaks.com/topic/191564-why-is-my-group-by-not-using-my-index/ Share on other sites More sharing options...
jskywalker Posted February 10, 2010 Share Posted February 10, 2010 Try adding user to your GROUP BY This should 'tell' MySQL to use the index on user, and not use a 'filesort' If this does not help, more info is needed, like 1) SHOW CREATE TABLE clicks 2) How many records are the in your table? Quote Link to comment https://forums.phpfreaks.com/topic/191564-why-is-my-group-by-not-using-my-index/#findComment-1009930 Share on other sites More sharing options...
gnznroses Posted February 10, 2010 Author Share Posted February 10, 2010 i still get the same result EXPLAIN SELECT campaign FROM clicks WHERE user = 123 GROUP BY campaign, user CREATE TABLE `clicks` ( `id` int(10) unsigned NOT NULL auto_increment, `campaign` smallint(10) unsigned NOT NULL, `user` mediumint( unsigned NOT NULL, `datetime` datetime NOT NULL, PRIMARY KEY (`id`), KEY `user` (`user`), KEY `datetime` (`datetime`), KEY `campaign` (`campaign`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=14629687 ; Quote Link to comment https://forums.phpfreaks.com/topic/191564-why-is-my-group-by-not-using-my-index/#findComment-1010231 Share on other sites More sharing options...
sader Posted February 10, 2010 Share Posted February 10, 2010 not sure this will help but here's what I have found change GROUP BY to FORCE INDEX FOR GROUP BY Quote Link to comment https://forums.phpfreaks.com/topic/191564-why-is-my-group-by-not-using-my-index/#findComment-1010255 Share on other sites More sharing options...
gnznroses Posted February 11, 2010 Author Share Posted February 11, 2010 i found the solution, sorta. making a composite index on the two columns. the problem is, the table actually has about a dozen columns, not just the ones i included in my code. and there's several different combinations of columns that we use in various queries (campaign+user, campaign+date, user+date, etc etc). so i would almost need multiple composite indexes... so anytime you're selecting from one column (and using either distinct or group by) and filtering by another, it has to use a temporary table unless you have a composite index on both columns? Quote Link to comment https://forums.phpfreaks.com/topic/191564-why-is-my-group-by-not-using-my-index/#findComment-1010490 Share on other sites More sharing options...
fenway Posted February 16, 2010 Share Posted February 16, 2010 Yes, that's usually the case. Quote Link to comment https://forums.phpfreaks.com/topic/191564-why-is-my-group-by-not-using-my-index/#findComment-1013135 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.