Discussion:
Help,Help,Help! ! ! Why Django serialization takes 3 to 30 seconds
h***@nebulas.io
2018-09-10 09:30:47 UTC
Permalink
record = ExchangeCoinTransaction.objects.filter(eth_tx_hash=eth_tx_hash)
if not record:
return False, "the eth transaction is't exchange coin transaction"
json_rec = serializers.serialize("json", record)
record = json.loads(json_rec)

#This simple query database, and serialize the results of the operation
takes 3 to 4s, similar operations may take more time
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Azion
2018-09-10 11:30:11 UTC
Permalink
Hi Haitao,

Are you sure it's the serialization the problem? Asking this 'cause, if
your database/model doesn't have an index over `eth_tx_hash`, it may
take a very long time for the database to find the records you need.

(Also, maybe using `.select_related()`
(https://docs.djangoproject.com/en/2.1/ref/models/querysets/#select-related)
on your query may speed things up by bringing all related elements, in
case your model have a few ForeignKeys or ManyToManyFields).
Post by h***@nebulas.io
record =
ExchangeCoinTransaction.objects.filter(eth_tx_hash=eth_tx_hash)
return False, "the eth transaction is't exchange coin transaction"
json_rec = serializers.serialize("json", record)
record = json.loads(json_rec)
#This simple query database, and serialize the results of the
operation takes 3 to 4s, similar operations may take more time
--
You received this message because you are subscribed to the Google
Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it,
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jason
2018-09-10 11:35:19 UTC
Permalink
While serialization is a computationally expensive operation, it is much
more likely it is due to infefficient database setup, configuration,
indices and unexpected N+1 queries being executed that are the cause of
your issue. Use a profiler like django-silk
<https://github.com/jazzband/django-silk> to determine where your slow
points actually are
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
h***@nebulas.io
2018-09-12 03:33:33 UTC
Permalink
Thank you, Azion,

I have solved this problem, The reason why the query speed is too slow is
mainly that the server where the program is located is too far away from
the server where the database is located. My backend program is deployed in
China, the database is deployed in the US, and every query will establish a
connection, so the query speed is slow.
Post by Azion
Hi Haitao,
Are you sure it's the serialization the problem? Asking this 'cause, if
your database/model doesn't have an index over `eth_tx_hash`, it may take a
very long time for the database to find the records you need.
(Also, maybe using `.select_related()` (
https://docs.djangoproject.com/en/2.1/ref/models/querysets/#select-related)
on your query may speed things up by bringing all related elements, in case
your model have a few ForeignKeys or ManyToManyFields).
record = ExchangeCoinTransaction.objects.filter(eth_tx_hash=eth_tx_hash)
return False, "the eth transaction is't exchange coin transaction"
json_rec = serializers.serialize("json", record)
record = json.loads(json_rec)
#This simple query database, and serialize the results of the operation
takes 3 to 4s, similar operations may take more time
--
You received this message because you are subscribed to the Google Groups
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Jason
2018-09-12 12:19:27 UTC
Permalink
uh, that is some network latency but not the root cause for long
responses. round trip from US to China is in the order of 300ms or less.
Check out the table at
http://www.verizonenterprise.com/about/network/latency/ and look at latency
from NA to Taiwan and Korea.
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...