Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django rest framework ModelSerializer runs too slowly

We want to provide an api for our django project,so we use drf (django rest framework). we use ModelSerializer which provides a shortcut that lets you automatically create a Serializer class with fields that correspond to the Model fields. Our problem is it runs very slowly.In other word,process of serialization takes about 40 second until retrieving the response.

How we can reduce this delay?
VIEW

class MyObjViewSet(viewsets.ModelViewSet):

    pagination_class = LargeResultsSetPagination

    def get_queryset(self):
        queryset = MyObj.objects.all().order_by('time')
        return queryset

    serializer_class = MyObjSerializer

My Obj Model

class MyObj(models.Model):
    id = models.BigIntegerField(primary_key=True)
    time = models.DateTimeField()
    type = models.CharField(max_length=5)
    user = models.ForeignKey('User', related_name='myobj')

MyObj User Model

class User(models.Model):
    id = models.IntegerField(primary_key=True)
    username = models.CharField(max_length=80)

my Obj serializer

class MyObjSerializer(serializers.ModelSerializer):

    class Meta:
        model = MyObj
        fields = ('id', 'type', 'time', 'user')

My problem is when i want to retrieve list of my objs, its take about to 40 second!

like image 474
Haydar Ghasemi Avatar asked Dec 19 '25 07:12

Haydar Ghasemi


1 Answers

Actualy, it is a real problem of drf.
If you have a lot of objects returned by queryset then you have to do few steps:
1. Check that you use many=True, it will produce significant performance improvement.
2. Avoid ModelSerializer on huge objects sets. In fact, worth idea is to avoid any kind of rest_framework.Serializer
3. Try to use serpy library for serializing. But you shouldn't forget that it is not fully compatible with rest_framework.Serializer.
4. Use .values() and DictSerializer. It will give you BIG speed up of serializing.
5. Do not forget about indexes in your database. 6. Use such powerful things as prefetch_related and select_related while working with ForeignKey.
7. The last way is to use simple dict. Otherwise, I didn't get appreciable result: only 10% versus serpy with DictSerializer.

I had a case when I have to serialize many objects (about 3-5k), overhead from drf serializer was at least 2,5 seconds (without sql's time). After optimizing I got about ~200-300 ms.

I hope, drf's developers will do some perfomance-improvements in framework.

like image 141
Ruslan Galimov Avatar answered Dec 21 '25 23:12

Ruslan Galimov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!