MySQL Forums
Forum List  »  Optimizer & Parser

Re: Subquery with range uses filesort
Posted by: Rick James
Date: November 18, 2013 04:33PM

I should emphasize that "filesort" is not the end of the world. It is merely a symptom of a complex query, and it may not be avoidable.

Meanwhile, let's see what we can do for you...

In this
select  *
    from  Player p
    join  History h on h.HistoryPlayer = p.PlayerID
    JOIN  
      ( SELECT  HistoryDate
            from  History
            where  HistoryPlayer = PlayerID
              and  HistoryDate <= '2013-01-01'
            order by  HistoryDate desc, HistoryDirector desc
            limit  0,1 ) x ON x.HistoryDate = h.HistoryDate
"HistoryDirector desc" seems to be unnecessary.

That would let you do MAX(HistoryDate) instead of ORDER BY and LIMIT.

Note that if there are two History rows for a given HistoryDate and PlayerID, the final result will have multiple rows. Is this what you expect?

Hmmm... Now I am confused by "where HistoryPlayer = PlayerID" in the subquery.

What is the query trying to do? Perhaps "find the latest info (from last year) for each player"? If so, then it is a "groupwise max" problem. Search for that.

Could we see the
SHOW CREATE TABLE History;
SHOW CREATE TABLE Players;
It smells like you are missing some indexes.

Notice how this one:
1,PRIMARY,Player,ALL,NULL,NULL,NULL,NULL,53985,100.00,
1,PRIMARY,History,ALL,NULL,NULL,NULL,NULL,880850,100.00,"Using where; Using join buffer"
2,"DEPENDENT SUBQUERY",History,ref,PlayerDateDirector,PlayerDateDirector,4,ratingscentral.Player.PlayerID,8808,100.00,"Using where; Using index; Using filesort"
fails to use any indexes. In fact it does a cross-join between Player and History before touching the _dependent_ subquery. That should take a looong time.

You have an awful number of indexes on Player. Most of them are probably not worth having.

Sorry, I don't (yet) have the answers you want.

Options: ReplyQuote


Subject
Views
Written By
Posted
4037
November 16, 2013 02:42PM
1434
November 17, 2013 07:42PM
1447
November 17, 2013 08:01PM
Re: Subquery with range uses filesort
1444
November 18, 2013 04:33PM
1983
November 18, 2013 07:54PM
1672
November 19, 2013 04:10PM
1526
November 19, 2013 08:36PM
1607
November 21, 2013 02:55PM
1813
December 05, 2013 03:46PM
1461
December 07, 2013 02:10PM
1427
December 09, 2013 06:08PM
1526
December 14, 2013 09:51PM
1393
December 14, 2013 10:17PM
1471
December 15, 2013 11:29PM
1520
December 17, 2013 06:58PM
1467
December 18, 2013 05:49PM
1508
December 18, 2013 08:28PM
1543
December 19, 2013 10:13PM
1519
December 19, 2013 10:28PM
1372
December 20, 2013 07:06PM
1457
December 21, 2013 07:36AM
1504
December 22, 2013 10:01AM
1559
December 22, 2013 10:45AM
1428
December 25, 2013 09:30PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.