diff options
| author | Nick Pope <nick.pope@flightdataservices.com> | 2019-10-21 17:34:19 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-10-24 15:13:26 +0200 |
| commit | 55df1750be3c88db89444335f77dca10681dcbe3 (patch) | |
| tree | 71c8547cd87b78ba47659ad34aff957352172251 /django | |
| parent | 742961332e1e2221e6fb9506c7254164e0c2cb5a (diff) | |
Refs #30897 -- Added support for ANALYZE option to Queryset.explain() on MariaDB and MySQL 8.0.18+.
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/backends/mysql/features.py | 4 | ||||
| -rw-r--r-- | django/db/backends/mysql/operations.py | 11 |
2 files changed, 12 insertions, 3 deletions
diff --git a/django/db/backends/mysql/features.py b/django/db/backends/mysql/features.py index 0bcb05b1d1..6a7f87ba27 100644 --- a/django/db/backends/mysql/features.py +++ b/django/db/backends/mysql/features.py @@ -118,6 +118,10 @@ class DatabaseFeatures(BaseDatabaseFeatures): return not self.connection.mysql_is_mariadb and self.connection.mysql_version < (5, 7) @cached_property + def supports_explain_analyze(self): + return self.connection.mysql_is_mariadb or self.connection.mysql_version >= (8, 0, 18) + + @cached_property def supported_explain_formats(self): # Alias MySQL's TRADITIONAL to TEXT for consistency with other # backends. diff --git a/django/db/backends/mysql/operations.py b/django/db/backends/mysql/operations.py index ef5d23214e..b801afef4a 100644 --- a/django/db/backends/mysql/operations.py +++ b/django/db/backends/mysql/operations.py @@ -299,11 +299,16 @@ class DatabaseOperations(BaseDatabaseOperations): elif not format and 'TREE' in self.connection.features.supported_explain_formats: # Use TREE by default (if supported) as it's more informative. format = 'TREE' + analyze = options.pop('analyze', False) prefix = super().explain_query_prefix(format, **options) - if format: + if analyze and self.connection.features.supports_explain_analyze: + # MariaDB uses ANALYZE instead of EXPLAIN ANALYZE. + prefix = 'ANALYZE' if self.connection.mysql_is_mariadb else prefix + ' ANALYZE' + if format and not (analyze and not self.connection.mysql_is_mariadb): + # Only MariaDB supports the analyze option with formats. prefix += ' FORMAT=%s' % format - if self.connection.features.needs_explain_extended and format is None: - # EXTENDED and FORMAT are mutually exclusive options. + if self.connection.features.needs_explain_extended and not analyze and format is None: + # ANALYZE, EXTENDED, and FORMAT are mutually exclusive options. prefix += ' EXTENDED' return prefix |
