#25377: COUNT(*) not COUNT('*')
----------------------------------------------+--------------------
     Reporter:  adamchainz                    |      Owner:  nobody
         Type:  Cleanup/optimization          |     Status:  new
    Component:  Database layer (models, ORM)  |    Version:  1.8
     Severity:  Normal                        |   Keywords:
 Triage Stage:  Unreviewed                    |  Has patch:  1
Easy pickings:  0                             |      UI/UX:  0
----------------------------------------------+--------------------
 Performance regression on 1.8 expression refactor. Count stopped
 outputting `COUNT(*)` and started outputting `COUNT('*')` due to using a
 `Value`. Possibly unnoticed until now because it doesn't affect the most
 popular database options much - I can't measure a performance drop for
 this on MySQL / InnoDb.

 However I have managed to measure a performance drop on MariaDB + Aria
 (=MySQL fork + MyISAM fork):

 {{{
 adamj@localhost [3]> select count(*) from count_test;
 +----------+
 | count(*) |
 +----------+
 |    10000 |
 +----------+
 1 row in set (0.00 sec)

 adamj@localhost [10]> select benchmark(100 * 1000 * 1000, (select count(*)
 from count_test));
 +-----------------------------------------------------------------+
 | benchmark(100 * 1000 * 1000, (select count(*) from count_test)) |
 +-----------------------------------------------------------------+
 |                                                               0 |
 +-----------------------------------------------------------------+
 1 row in set (0.86 sec)

 adamj@localhost [11]> select benchmark(100 * 1000 * 1000, (select
 count('*') from count_test));
 +-------------------------------------------------------------------+
 | benchmark(100 * 1000 * 1000, (select count('*') from count_test)) |
 +-------------------------------------------------------------------+
 |                                                                 0 |
 +-------------------------------------------------------------------+
 1 row in set (1.23 sec)

 }}}

 This is because MyISAM / Aria store the count in a metadata variable but
 once you introduce the expression it figures it has to do a table scan.
 The situation would of course only get worse with more rows, 10000 is
 tiny.

--
Ticket URL: <https://code.djangoproject.com/ticket/25377>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.e38d3e1d0fb4cef12e260d507426ef11%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to