| 1 |
.. _Benchmark: |
| 2 |
|
| 3 |
Benchmark |
| 4 |
--------- |
| 5 |
|
| 6 |
.. contents:: |
| 7 |
|
| 8 |
Introduction |
| 9 |
............ |
| 10 |
|
| 11 |
This benchmark does not intend to be exhaustive nor fair to SQL. |
| 12 |
It shows how django-cachalot behaves on an unoptimised application. |
| 13 |
On an application using perfectly optimised SQL queries only, |
| 14 |
django-cachalot may not be useful. |
| 15 |
Unfortunately, most Django apps (including Django itself) |
| 16 |
use unoptimised queries. Of course, they often lack useful indexes |
| 17 |
(even though it only requires 20 characters per index…). |
| 18 |
But what you may not know is that |
| 19 |
**the ORM currently generates totally unoptimised queries** [#]_. |
| 20 |
|
| 21 |
You can run the benchmarks yourself (officially supported on Linux |
| 22 |
and Mac). You will need a database called "cachalot" on MySQL and PostgreSQL. |
| 23 |
Additionally, on PostgreSQL, you will need to create a role |
| 24 |
called "cachalot." Running the benchmarks can raise |
| 25 |
errors with specific instructions for how to fix it. |
| 26 |
|
| 27 |
#. Install: ``pip install -r requirements/benchmark.txt`` |
| 28 |
#. Run: ``python benchmark.py`` |
| 29 |
|
| 30 |
The output will be in benchmark/TODAY'S_DATE/ |
| 31 |
|
| 32 |
Conditions |
| 33 |
.......... |
| 34 |
|
| 35 |
.. include:: ../benchmark/docs/2018-08-09/conditions.rst |
| 36 |
|
| 37 |
Note that |
| 38 |
`MySQL’s query cache <http://dev.mysql.com/doc/refman/5.7/en/query-cache.html>`_ |
| 39 |
is active during the benchmark. |
| 40 |
|
| 41 |
Database results |
| 42 |
................ |
| 43 |
|
| 44 |
.. include:: ../benchmark/docs/2018-08-09/db_results.rst |
| 45 |
|
| 46 |
.. image:: ../benchmark/docs/2018-08-09/db.svg |
| 47 |
|
| 48 |
|
| 49 |
Cache results |
| 50 |
............. |
| 51 |
|
| 52 |
.. include:: ../benchmark/docs/2018-08-09/cache_results.rst |
| 53 |
|
| 54 |
.. image:: ../benchmark/docs/2018-08-09/cache.svg |
| 55 |
|
| 56 |
|
| 57 |
Database detailed results |
| 58 |
......................... |
| 59 |
|
| 60 |
MySQL |
| 61 |
~~~~~ |
| 62 |
|
| 63 |
.. image:: ../benchmark/docs/2018-08-09/db_mysql.svg |
| 64 |
|
| 65 |
PostgreSQL |
| 66 |
~~~~~~~~~~ |
| 67 |
|
| 68 |
.. image:: ../benchmark/docs/2018-08-09/db_postgresql.svg |
| 69 |
|
| 70 |
SQLite |
| 71 |
~~~~~~ |
| 72 |
|
| 73 |
.. image:: ../benchmark/docs/2018-08-09/db_sqlite.svg |
| 74 |
|
| 75 |
|
| 76 |
Cache detailed results |
| 77 |
...................... |
| 78 |
|
| 79 |
File-based |
| 80 |
~~~~~~~~~~ |
| 81 |
|
| 82 |
.. image:: ../benchmark/docs/2018-08-09/cache_filebased.svg |
| 83 |
|
| 84 |
Locmem |
| 85 |
~~~~~~ |
| 86 |
|
| 87 |
.. image:: ../benchmark/docs/2018-08-09/cache_locmem.svg |
| 88 |
|
| 89 |
Memcached (python-memcached) |
| 90 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 91 |
|
| 92 |
.. image:: ../benchmark/docs/2018-08-09/cache_memcached.svg |
| 93 |
|
| 94 |
Memcached (pylibmc) |
| 95 |
~~~~~~~~~~~~~~~~~~~ |
| 96 |
|
| 97 |
.. image:: ../benchmark/docs/2018-08-09/cache_pylibmc.svg |
| 98 |
|
| 99 |
Redis |
| 100 |
~~~~~ |
| 101 |
|
| 102 |
.. image:: ../benchmark/docs/2018-08-09/cache_redis.svg |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
.. [#] The ORM fetches way too much data if you don’t restrict it using |
| 107 |
``.only`` and ``.defer``. You can divide the execution time |
| 108 |
of most queries by 2-3 by specifying what you want to fetch. |
| 109 |
But specifying which data we want for each query is very long |
| 110 |
and unmaintainable. An automation using field usage statistics |
| 111 |
is possible and would drastically improve performance. |
| 112 |
Other performance issues occur with slicing. |
| 113 |
You can often optimise a sliced query using a subquery, like |
| 114 |
``YourModel.objects.filter(pk__in=YourModel.objects.filter(…)[10000:10050]).select_related(…)`` |
| 115 |
instead of ``YourModel.objects.filter(…).select_related(…)[10000:10050]``. |
| 116 |
I’ll maybe work on these issues one day. |