D
D
dustik2014-02-18 09:24:40
MySQL
dustik, 2014-02-18 09:24:40

Why is the processing of large tables (ManyToManyField) slow in the Django admin interface?

Hello dear community. Faced with a problem, I rely on your comprehensive knowledge.
The essence of the problem:
There are the following application models for the book catalog (elcat):
1.

# Таблица с книгами
class book(models.Model):
    book_id = models.BigIntegerField(primary_key=True, unique=True)
    name = models.CharField(max_length=512)
    ...
    author_id = models.ManyToManyField('author')
    ...

2.
#Таблица с авторами
class author(models.Model):
    author_id = models.BigIntegerField(primary_key=True)
    name = models.CharField(max_length=512)

3. Table generated by Django framework:
df3c23d5994a4292bab694eb95d8ff7c.PNG
Number of records in tables:
elcat_book = 121525 elcat_author
= 49832
'elcat_author' in the field 'elcat_book.author_id'.
I tried to use the recommendation from The Definitive Guide to Django: , but in this situation, loading does not become faster and besides, very often the JavaScript script that implements filter_horizontal stops working completely and the browser has to be forcibly turned off. Perhaps raw_id_fields can help me, but I still could not figure out if it would work for ManyToManyField.
filter_horizontal = (author_id',)
I ask you to prompt, it is possible and in general terms, how it is possible to solve a similar problem. If you have encountered similar (slow speed) and solved it with external applications, then please tell me which ones. Also, I will not refuse links to smart articles where this is chewed up.
---------------------------------
Django version = VERSION = (1, 6, 0, 'final', 0 );
python=2.7.1;
MySQL='5.6.13-log'.
Everything works on Windows 7 (x64) Pro.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
Hellpain, 2014-02-18
@Hellpain

When loading the detailed page of a book, all authors are requested. You can set up an admin model for a book

class AdminBook(ModelAdmin):
    raw_id_fields = ('author_id', )

site.register(Book, AdminBook)

Here is more information about optimizing the speed of the admin panel. go

L
leclecovich, 2014-02-18
@leclecovich

First, BigIntegerField as a PK is not the best idea in terms of optimization. Omit book_id and author_id in author. Django will substitute AutoField itself. In addition, a relation looks more logical when:

class book(models.Model):
    name = models.CharField(max_length=512)
    ...
    author = models.ManyToManyField('author')

Second, make sure that the ORM will call prefetch_related properly when fetching.
As a general recommendation, use ORM logging, or use django-debug-toolbar .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question