Ticket #14767: db-optimize-fixes.diff
File db-optimize-fixes.diff, 4.4 KB (added by , 14 years ago) |
---|
-
docs/topics/db/optimization.txt
diff --git a/docs/topics/db/optimization.txt b/docs/topics/db/optimization.txt index 7449d63..96d1239 100644
a b Profile first 13 13 As general programming practice, this goes without saying. Find out :ref:`what 14 14 queries you are doing and what they are costing you 15 15 <faq-see-raw-sql-queries>`. You may also want to use an external project like 16 'django-debug-toolbar', or a tool that monitors your database directly.16 django-debug-toolbar_, or a tool that monitors your database directly. 17 17 18 18 Remember that you may be optimizing for speed or memory or both, depending on 19 19 your requirements. Sometimes optimizing for one will be detrimental to the … … readability of your code. **All** of the suggestions below come with the caveat 29 29 that in your circumstances the general principle might not apply, or might even 30 30 be reversed. 31 31 32 .. _django-debug-toolbar: http://robhudson.github.com/django-debug-toolbar/ 33 32 34 Use standard DB optimization techniques 33 35 ======================================= 34 36 35 37 ...including: 36 38 37 39 * Indexes. This is a number one priority, *after* you have determined from 38 profiling what indexes should be added. Use :attr:`django.db.models.Field.db_index` to add39 t hese from Django.40 profiling what indexes should be added. Use :attr:`django.db.models.Field.db_index` 41 to add these from Django. 40 42 41 43 * Appropriate use of field types. 42 44 … … Understand cached attributes 69 71 As well as caching of the whole ``QuerySet``, there is caching of the result of 70 72 attributes on ORM objects. In general, attributes that are not callable will be 71 73 cached. For example, assuming the :ref:`example Weblog models 72 <queryset-model-example>`: 74 <queryset-model-example>`:: 73 75 74 76 >>> entry = Entry.objects.get(id=1) 75 77 >>> entry.blog # Blog object is retrieved at this point … … Don't retrieve things you don't need 156 158 Use ``QuerySet.values()`` and ``values_list()`` 157 159 ----------------------------------------------- 158 160 159 When you just want a dict/list of values, and don't need ORM model objects, make160 appropriate usage of :meth:`~django.db.models.QuerySet.values()`. 161 These can be useful for replacing model objects in template code - as long as 162 the dicts you supply have the same attributes as those used in the template, you 163 a re fine.161 When you just want a ``dict`` or ``list`` of values, and don't need ORM model 162 objects, make appropriate usage of 163 :meth:`~django.db.models.QuerySet.values()`. These can be useful for replacing 164 model objects in template code - as long as the dicts you supply have the same 165 attributes as those used in the template, you are fine. 164 166 165 167 Use ``QuerySet.defer()`` and ``only()`` 166 168 --------------------------------------- … … Use ``QuerySet.defer()`` and ``only()`` 168 170 Use :meth:`~django.db.models.QuerySet.defer()` and 169 171 :meth:`~django.db.models.QuerySet.only()` if there are database columns you 170 172 know that you won't need (or won't need in most cases) to avoid loading 171 them. Note that if you *do* use them, the ORM will have to go and get them in a 172 separate query, making this a pessimization if you use it inappropriately. 173 174 Also, be aware that there is some (small extra) overhead incurred inside Django when constructing a model with deferred fields. Don't be too aggressive in deferring fields without profiling as the database has to read most of the non-text, non-VARCHAR data from the disk for a single row in the results, even if it ends up only using a few columns. The `defer()` and `only()` methods are most useful when you can avoid loading a lot of text data or for fields that might take a lot of processing to convert back to Python. As always, profile first, then optimize. 173 them. Note that if you *do* use them, the ORM will have to go and get them in 174 a separate query, making this a pessimization if you use it inappropriately. 175 176 Also, be aware that there is some (small extra) overhead incurred inside 177 Django when constructing a model with deferred fields. Don't be too aggressive 178 in deferring fields without profiling as the database has to read most of the 179 non-text, non-VARCHAR data from the disk for a single row in the results, even 180 if it ends up only using a few columns. The ``defer()`` and ``only()`` methods 181 are most useful when you can avoid loading a lot of text data or for fields 182 that might take a lot of processing to convert back to Python. As always, 183 profile first, then optimize. 175 184 176 185 Use QuerySet.count() 177 186 --------------------