Created
November 16, 2010 12:32
-
-
Save dchaplinsky/701757 to your computer and use it in GitHub Desktop.
Method to avoid heavy filesort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FoobarManager(models.Manager): | |
def latest(self, limit=10): | |
""" more effecient version of query to avoid filesort """ | |
ids = list(self.values_list('id', flat=True).order_by('-created')[:limit]) | |
return self.filter(id__in=ids).extra( | |
select={'manual': 'FIELD(id,%s)' % ','.join(map(str, ids))}, | |
order_by=['manual']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment