Often, you either can't or don't want to rewrite the query. However, you can still try and speed it up through any of the techniques discussed here.
By now, we assume that you've looked at various problems already, so the following are more advanced ideas for you to try.
For queries involving large sorts or for join queries it may be useful to increase the amount of working memory that can be used for query execution. Try setting:
SET work_mem = '1TB';
Then, run EXPLAIN...