Django (?) Very slow with large datasets after doing some python profiling

I was comparing an old PHP script with my version, an earlier version of Django and PHP, with a full splash of HTML, and everything worked faster. MUCH faster to the point that something must be wrong on Django.

First, some context: I have a page that spits out sales reports. Data can be filtered by several things, but mostly filtered by date. This makes caching a bit more difficult, because the possibilities for results are almost endless. There are many numbers and calculations, but in PHP there has never been a problem with processing.

UPDATE:

  • After some additional testing, there is nothing in my view that causes a slowdown. If I just count the data crunch and spit out 5 lines of the displayed HTML, it is not so slow (still slower than PHP), but if I get a lot of data, it is VERY slow.

  • Whenever I ran a large report (for example, all sales for a year), the processor load is 100%. I don’t know how much this means. I am using mod_python and Apache. Perhaps switching to WSGI might help?

  • My template tags show that subtotals / final processes are processed from 0.1 second to 1 second for really large sets. I call them about 6 times in the report, so they do not seem to be the biggest problem.

Now I launched the Python profiler and returned with these results:

Ordered by: internal time
   List reduced from 3074 to 20 due to restriction 

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  2939417   26.290    0.000   44.857    0.000 /usr/lib/python2.5/tokenize.py:212(generate_tokens)
  2822655   17.049    0.000   17.049    0.000 {built-in method match}
  1689928   15.418    0.000   23.297    0.000 /usr/lib/python2.5/decimal.py:515(__new__)
 12289605   11.464    0.000   11.464    0.000 {isinstance}
   882618    9.614    0.000   25.518    0.000 /usr/lib/python2.5/decimal.py:1447(_fix)
    17393    8.742    0.001   60.798    0.003 /usr/lib/python2.5/tokenize.py:158(tokenize_loop)
       11    7.886    0.717    7.886    0.717 {method 'accept' of '_socket.socket' objects}
   365577    7.854    0.000   30.233    0.000 /usr/lib/python2.5/decimal.py:954(__add__)
  2922024    7.199    0.000    7.199    0.000 /usr/lib/python2.5/inspect.py:571(tokeneater)
   438750    5.868    0.000   31.033    0.000 /usr/lib/python2.5/decimal.py:1064(__mul__)
    60799    5.666    0.000    9.377    0.000 /usr/lib/python2.5/site-packages/django/db/models/base.py:241(__init__)
    17393    4.734    0.000    4.734    0.000 {method 'query' of '_mysql.connection' objects}
  1124348    4.631    0.000    8.469    0.000 /usr/lib/python2.5/site-packages/django/utils/encoding.py:44(force_unicode)
   219076    4.139    0.000  156.618    0.001 /usr/lib/python2.5/site-packages/django/template/__init__.py:700(_resolve_lookup)
  1074478    3.690    0.000   11.096    0.000 /usr/lib/python2.5/decimal.py:5065(_convert_other)
  2973281    3.424    0.000    3.424    0.000 /usr/lib/python2.5/decimal.py:718(__nonzero__)
   759014    2.962    0.000    3.371    0.000 /usr/lib/python2.5/decimal.py:4675(__init__)
   381756    2.806    0.000  128.447    0.000 /usr/lib/python2.5/site-packages/django/db/models/fields/related.py:231(__get__)
   842130    2.764    0.000    3.557    0.000 /usr/lib/python2.5/decimal.py:3339(_dec_from_triple)

tokenize.py , , . Decimal.py , , , 90% . , match, (- Django ?) , itertools ifilter.

, , , , .

- - , ? , / , .

. / , , , . tokenize.py?

+5
4

, , - .

: ORM Django (.. sales-data = modelobj.objects(). all()), PHP SQL- query_set.

Django , ORM/Model (() ).

PHP , , .

, - . Django RAW SQL : http://docs.djangoproject.com/en/dev/topics/db/sql/#topics-db-sql

, ...

+6

"tokenize.py , , ."

.

. http://docs.python.org/library/tokenize.html.

tokenize Python, Python

Tokenize, , , .

AFAIK ( Django) Django tokenize. , - . , , , , .

- . - . , Python. .

, , , -, . .

, . , .

, .. defaultdict Python. , . , . ORM Django RDBMS, .

+2

CPU , ValuesQuerySet, , .

:

Blog.objects.order_by('id').values()
+2

. , ORM SQL-.

, , probem , , .

:

PS: fyi, , . Debug Footer Middleware , 500! sql- . select_related() 5 , .

+1

All Articles