﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
5570	Improve performance of generic relations	Manuel Saelices	Jacob	"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:

{{{
#!python
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:

{{{
#!python
>>> 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."		closed	Database layer (models, ORM)	dev		fixed	generic relations GenericForeignKey		Accepted	1	1	1	1	0	0
