Opened 17 years ago
Closed 17 years ago
#5570 closed (fixed)
Improve performance of generic relations
Reported by: | Manuel Saelices | Owned by: | Jacob |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Keywords: | generic relations GenericForeignKey | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | yes |
Needs tests: | yes | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
Every access of an object relates to another object with generic relations send two SQL statements. I think it should be cached more efficiently.
Look at this example model:
class Note(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.IntegerField() related_object = generic.GenericForeignKey('content_type', 'object_id') class Project(models.Model): name = models.CharField(maxlength=50)
Imagine there are two notes asociated both to one project object (not necessary the same object).
Now look at this code:
>>> from django.db import connection >>> [ n.related_object for n in Note.objects.all() ] [<Project: Equipo Yaco Community>, <Project: Equipo Yaco Community>] >>> connection.queries [{'sql': u'SELECT "myapp_note"."id","myapp_note"."content_type_id","myapp_note"."object_id" FROM "projects_note"', 'time': '0.003'}, {'sql': u'SELECT "django_content_type"."id",... FROM "django_content_type" WHERE ("django_content_type"."id" = 22)', 'time': '0.002'}, {'sql': u'SELECT "django_content_type"."id",... FROM "django_content_type" WHERE ("django_content_type"."id" = 22)', 'time': '0.002'}, {'sql': u'SELECT "myapp_project"."id","myapp_project"."name" FROM "myapp_project" WHERE ("myapp_project"."id" = 1)',
As you can see, there are two identical SQL sentences (number 2 and 3). But with the patch uploaded, these two sentences reduces in one.
This can be a good improvement for apps that uses intensively generic relations. In the best of cases, can reduce 50% of SQL sentences in a loop of adquiring related objects.
Attachments (1)
Change History (11)
by , 17 years ago
Attachment: | generic_relations_r6403.diff added |
---|
comment:1 by , 17 years ago
Status: | new → assigned |
---|
comment:2 by , 17 years ago
Has patch: | unset |
---|---|
Resolution: | → invalid |
Status: | assigned → closed |
This is not a bug, it's normal behaviour. If you have multiple notes refering to the same content type, you get multiple queries. If you want it changed, you need to use caching in your application. This is the same for generic foreign keys or normal foreign keys.
You can't really simply add a cache for this without proper cache invalidation etc.
If you disagree, please discuss this at the developer mailing list.
comment:3 by , 17 years ago
I know thats is the normal behaviour. But for example on [source:django/contrib/contenttypes/models.py django/contrib/contenttypes/models.py] does same type of caching. And there are a lot of caching in other files. Remember, I don't use django cache framework. I use a simple dictionary for caching. It's not the same.
comment:4 by , 17 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
I retract ... and go to bed ;-)
Sorry.
comment:5 by , 17 years ago
i also noticed that there are some indentical queries in the same page if generic relations was used.
follow-up: 7 comment:6 by , 17 years ago
Has patch: | set |
---|---|
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Accepted |
anonymous: are you saying that there are other instances of these duplicate queries?
msaelices: well spotted, thank you, but why are you adding/deleting blank lines in the patch?
comment:7 by , 17 years ago
Replying to Simon G <dev@simon.net.nz>:
anonymous: are you saying that there are other instances of these duplicate queries?
msaelices: well spotted, thank you, but why are you adding/deleting blank lines in the patch?
I only remove ugly white spaces... sorry, I hate put spaces for nothing.
One question. Why patch needs improvement? I think is good.
comment:8 by , 17 years ago
Needs documentation: | set |
---|---|
Needs tests: | set |
Owner: | changed from | to
Status: | reopened → new |
comment:9 by , 17 years ago
comment:10 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Patch that improves generic relations performance