I have the following database structure:
class Book(models.Model):
id = models.AutoField(primary_key=True, db_column='id')
name = models.CharField(max_length=255, db_column='name')
author = models.ForeignKey('Author', to_field='id', db_column='author_id')
class Author(models.Model):
id = models.AutoField(primary_key=True, db_column='id')
fio = models.CharField(max_length=255, db_column='fio')
def __unicode__(self):
return self.name
And this Admin class for Book:
class BookAdmin(admin.ModelAdmin):
list_display = ('id', 'name',)
fields = ('id', 'name', 'author',)
readonly_fields = ('id',)
raw_id_fields = ('author',)
Everything works fine when It's 500-1000 book records, but on 1-2 million books page freeze for few minutes before shows something. Profiler show me that django join books with authors and then cut last 100 records.
SELECT ••• FROM `books` INNER JOIN `authors` ON ( `books`.`author_id` = `authors`.`id` ) ORDER BY `books`.`id` DESC LIMIT 100
How can I optimize django to join authors after select books from database? Or use something like that
select * from (SELECT * FROM books ORDER BY books.id DESC LIMIT 100) t, authors a where t.author_id = a.id