diff options
| author | Tom <tom@tomforb.es> | 2017-09-10 15:34:18 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2018-04-19 10:52:19 -0400 |
| commit | c1c163b42717ed5e051098ebf0e2f5c77810f20e (patch) | |
| tree | b3c42bcf178fe8f33394f41252a92238cb8baff3 /docs | |
| parent | df90e462d91d3a77aa89b69d791bf17c2bf7ff9b (diff) | |
Fixed #28574 -- Added QuerySet.explain().
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/ref/models/querysets.txt | 43 | ||||
| -rw-r--r-- | docs/releases/2.1.txt | 3 |
2 files changed, 46 insertions, 0 deletions
diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index d623966b3a..b2f430d7bc 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -2476,6 +2476,49 @@ Class method that returns an instance of :class:`~django.db.models.Manager` with a copy of the ``QuerySet``’s methods. See :ref:`create-manager-with-queryset-methods` for more details. +``explain()`` +~~~~~~~~~~~~~ + +.. versionadded:: 2.1 + +.. method:: explain(format=None, **options) + +Returns a string of the ``QuerySet``’s execution plan, which details how the +database would execute the query, including any indexes or joins that would be +used. Knowing these details may help you improve the performance of slow +queries. + +For example, when using PostgreSQL:: + + >>> print(Blog.objects.filter(title='My Blog').explain()) + Seq Scan on blog (cost=0.00..35.50 rows=10 width=12) + Filter: (title = 'My Blog'::bpchar) + +The output differs significantly between databases. + +``explain()`` is supported by all built-in database backends except Oracle +because an implementation there isn't straightforward. + +The ``format`` parameter changes the output format from the databases's default, +usually text-based. PostgreSQL supports ``'TEXT'``, ``'JSON'``, ``'YAML'``, and +``'XML'``. MySQL supports ``'TEXT'`` (also called ``'TRADITIONAL'``) and +``'JSON'``. + +Some databases accept flags that can return more information about the query. +Pass these flags as keyword arguments. For example, when using PostgreSQL:: + + >>> print(Blog.objects.filter(title='My Blog').explain(verbose=True)) + Seq Scan on public.blog (cost=0.00..35.50 rows=10 width=12) (actual time=0.004..0.004 rows=10 loops=1) + Output: id, title + Filter: (blog.title = 'My Blog'::bpchar) + Planning time: 0.064 ms + Execution time: 0.058 ms + +On some databases, flags may cause the query to be executed which could have +adverse effects on your database. For example, PostgreSQL's ``ANALYZE`` flag +could result in changes to data if there are triggers or if a function is +called, even for a ``SELECT`` query. + .. _field-lookups: ``Field`` lookups diff --git a/docs/releases/2.1.txt b/docs/releases/2.1.txt index 9044c3cd70..7b65b90b9c 100644 --- a/docs/releases/2.1.txt +++ b/docs/releases/2.1.txt @@ -236,6 +236,9 @@ Models encouraged instead of :class:`~django.db.models.NullBooleanField`, which will likely be deprecated in the future. +* The new :meth:`.QuerySet.explain` method displays the database's execution + plan of a queryset's query. + Requests and Responses ~~~~~~~~~~~~~~~~~~~~~~ |
