| 1 |
.. _performance: |
| 2 |
|
| 3 |
Performance Considerations |
| 4 |
========================== |
| 5 |
|
| 6 |
Usually, when Django users create their own polymorphic ad-hoc solution without a tool like |
| 7 |
:pypi:`django-polymorphic`, this usually results in a variation of: |
| 8 |
|
| 9 |
.. code-block:: python |
| 10 |
|
| 11 |
result_objects = [ o.get_real_instance() for o in BaseModel.objects.filter(...) ] |
| 12 |
|
| 13 |
which has very bad performance, as it introduces one additional SQL query for every object in the |
| 14 |
result which is not of class ``BaseModel``. Compared to these solutions, :pypi:`django-polymorphic` |
| 15 |
has the advantage that it only needs 1 SQL query *per object type*, and not *per object*. |
| 16 |
|
| 17 |
The current implementation does not use any custom SQL or Django DB layer internals - it is purely |
| 18 |
based on the standard Django ORM. Specifically, the query: |
| 19 |
|
| 20 |
.. code-block:: python |
| 21 |
|
| 22 |
result_objects = list( ModelA.objects.filter(...) ) |
| 23 |
|
| 24 |
performs one SQL query to retrieve ``ModelA`` objects and one additional query for each unique |
| 25 |
derived class occurring in result_objects. The best case for retrieving 100 objects is 1 SQL query |
| 26 |
if all are class ``ModelA``. If 50 objects are ``ModelA`` and 50 are ``ModelB``, then two queries |
| 27 |
are executed. The pathological worst case is 101 db queries if result_objects contains 100 different |
| 28 |
object types (with all of them subclasses of ``ModelA``). |
| 29 |
|
| 30 |
Iteration: Memory vs DB Round Trips |
| 31 |
----------------------------------- |
| 32 |
|
| 33 |
When iterating over large QuerySets, there is a trade-off between memory consumption and number |
| 34 |
of round trips to the database. One additional query is needed per model subclass present in the |
| 35 |
QuerySet and these queries take the form of ``SELECT ... WHERE pk IN (....)`` with a potentially |
| 36 |
large number of IDs in the IN clause. All models in the IN clause will be loaded into memory during |
| 37 |
iteration. |
| 38 |
|
| 39 |
To balance this trade-off, by default a maximum of 2000 objects are requested at once. This means |
| 40 |
that if your QuerySet contains 10,000 objects of 3 different subclasses, then 16 queries will be |
| 41 |
executed: 1 to fetch the base objects, and 5 (10/2 == 5) * 3 more to fetch the subclasses. |
| 42 |
|
| 43 |
The `chunk_size` parameter on :meth:`~django.db.models.query.QuerySet.iterator` can be used to |
| 44 |
change the number of objects loaded into memory at once during iteration. For example, to load 5000 |
| 45 |
objects at once: |
| 46 |
|
| 47 |
.. code-block:: python |
| 48 |
|
| 49 |
for obj in ModelA.objects.all().iterator(chunk_size=5000): |
| 50 |
process(obj) |
| 51 |
|
| 52 |
.. note:: |
| 53 |
|
| 54 |
``chunk_size`` on non-polymorphic QuerySets controls the number of rows fetched from the |
| 55 |
database at once, but for polymorphic QuerySets the behavior is more analogous to its behavior |
| 56 |
when :meth:`~django.db.models.query.QuerySet.prefetch_related` is used. |
| 57 |
|
| 58 |
Some database backends limit the number of parameters in a query. For those backends the |
| 59 |
``chunk_size`` will be restricted to be no greater than that limit. This limit can be checked in: |
| 60 |
|
| 61 |
.. code-block:: python |
| 62 |
|
| 63 |
from django.db import connection |
| 64 |
|
| 65 |
print(connection.features.max_query_params) |
| 66 |
|
| 67 |
|
| 68 |
You may change the global default fallback ``chunk_size`` by modifying the |
| 69 |
:attr:`polymorphic.query.Polymorphic_QuerySet_objects_per_request` attribute. Place code like |
| 70 |
this somewhere that will be executed during startup: |
| 71 |
|
| 72 |
.. code-block:: python |
| 73 |
|
| 74 |
from polymorphic import query |
| 75 |
|
| 76 |
query.Polymorphic_QuerySet_objects_per_request = 5000 |
| 77 |
|
| 78 |
|
| 79 |
:class:`~django.contrib.contenttypes.models.ContentType` retrieval |
| 80 |
------------------------------------------------------------------ |
| 81 |
|
| 82 |
When fetching the :class:`~django.contrib.contenttypes.models.ContentType` class, it's tempting to |
| 83 |
read the :attr:`~polymorphic.models.PolymorphicModel.polymorphic_ctype` field directly. However, |
| 84 |
this performs an additional query via the :class:`~django.db.models.ForeignKey` object to fetch the |
| 85 |
:class:`~django.contrib.contenttypes.models.ContentType`. Instead, use: |
| 86 |
|
| 87 |
.. code-block:: python |
| 88 |
|
| 89 |
from django.contrib.contenttypes.models import ContentType |
| 90 |
|
| 91 |
ctype = ContentType.objects.get_for_id(object.polymorphic_ctype_id) |
| 92 |
|
| 93 |
This uses the :meth:`~django.contrib.contenttypes.models.ContentTypeManager.get_for_id` function |
| 94 |
which caches the results internally. |