How to optimize this MySQL query
        Posted  
        
            by James Simpson
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by James Simpson
        
        
        
        Published on 2010-05-27T17:18:02Z
        Indexed on 
            2010/05/27
            17:21 UTC
        
        
        Read the original article
        Hit count: 615
        
This query was working fine when the database was small, but now that there are millions of rows in the database, I am realizing I should have looked at optimizing this earlier. It is looking at over 600,000 rows and is Using where; Using temporary; Using filesort (which leads to an execution time of 5-10 seconds). It is using an index on the field 'battle_type.'
SELECT username, SUM( outcome ) AS wins, COUNT( * ) - SUM( outcome ) AS losses
FROM tblBattleHistory
WHERE battle_type =  '0' && outcome <  '2'
GROUP BY username
ORDER BY wins DESC , losses ASC , username ASC 
LIMIT 0 , 50
        © Stack Overflow or respective owner