Django REST Framework序列化非常慢 [英] Django REST Framework Serialize extremely slow

查看:1096
本文介绍了Django REST Framework序列化非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用django-restframework的Python 2.7和Django 1.7.1中 我有一个API,它从数据库中返回一些特定的值,它使用了这样的自定义序列化程序:

I am in Python 2.7 and Django 1.7.1 with django-restframework I have an API that returns me some specific values taken fron the Database, it uses a Custom Serializer like this:

class InventarioSerializer(serializers.ModelSerializer):
    item = serializers.RelatedField(source='producto.item')
    ubicacion = serializers.RelatedField(source='ubicacion.nombre')
    class Meta:
        model = Inventario
        fields = ('epc','item','cantidad','ubicacion')

我的API视图以这种方式调用:

My API's view is called this way:

class ItemEnInventarioViewSet(InventarioListModelMixin, viewsets.ModelViewSet):
    serializer_class = InventarioSerializer
    renderer_classes = (UnicodeJSONRenderer,)

我的ListModelMixin是这样的:

and my ListModelMixin is this:

class InventarioListModelMixin(object):
    def list(self, request, *args, **kwargs):
        item = request.QUERY_PARAMS.get('item', None)
        inventario = Inventario.objects.filter(producto__item = item)
        if inventario.count() == 0:
            return HttpResponse(u"El item %s no se encuentra en el inventario" % item,status=400)
        self.object_list = inventario
        # Switch between paginated or standard style responses
        page = self.paginate_queryset(self.object_list)
        if page is not None:
            serializer = self.get_pagination_serializer(page)
        else:
            serializer = self.get_serializer(self.object_list, many=True) <<--THIS IS THE PROBLEM
        return Response(serializer.data)

它工作正常,但是当我尝试从数据库中获得约1000个或更多条目时,序列化程序使其运行非常缓慢,约25至35秒.

It works fine, but when I try to GET form the DB arround 1000 or more entries, the serializer makes it very very slow, arround 25 to 35 seconds.

对数据库的查询非常简单,因此数据库根本不是问题.

The Query to the DB is very simple so the DB is not the problem at all.

如果我使用此功能"data = serializers.serialize('json', myQuerySet)"序列化查询集,最多需要3秒钟,但是我没有得到所需的信息,这就是为什么我使用自定义序列化器"

If I serialize the queryset with this function "data = serializers.serialize('json', myQuerySet)" it takes at most 3 seconds but i dont get the info as I want, that's why I use a Custom Serializer

是否有最快的方法来获取该数量的值?也许与另一个序列化器?有什么主意吗?

** ANSWER感谢Kevin ** 将查询更改为:

**ANSWER Thanks to Kevin ** Changing the query to:

inventario = Inventario.objects.select_related('producto__item','ubicacion__nombre').filter(producto__item = item)

...使序列化程序不会在每个结果行中都击中数据库以检索外部值.

...makes the Serializer not to hit the database every result-row to retrieve the Foreign values.

推荐答案

对数据库的查询非常简单,因此数据库根本不是问题.

The Query to the DB is very simple so the DB is not the problem at all.

请确保您的查询没有 N + 1问题.它们可能很简单,但是如果其中有很多,则将花费大量时间.我在这里写了很多有关修复Django REST Framework中的性能问题的文章,您可以找到很多有关它的信息.通过搜索.

Make sure you do not have a N+1 issue with your queries. They may be simple, but if there are many of them then it will take up a considerable amount of time. I've written quite a bit about fixing performance issues in Django REST Framework on here, and you can find a lot about it by searching around.

是否有最快的方法来获取该数量的值?也许与另一个序列化器?有什么主意吗?

Is there a fastest way to GET that quantity of values? Maybe with another Serializer? any idea?

如果您的数据变化不那么频繁,或者您可以处理任何可能的缓存问题,则可以从向API中添加一些缓存中受益匪浅. drf扩展提供了许多有用的缓存混合器,如果您愿意,可以为您提供帮助问题实际上与您的查询无关.

If your data does not change that often, or you can deal with any possible caching issues, you may benefit greatly from adding some caching to your API. drf-extensions provides quite a few useful mixins for caching that may help you if your issue is not actually with your queries.

当我尝试从数据库arround 1000或更多条目中获取

when I try to GET form the DB arround 1000 or more entries

我了解您的代码已内置分页功能,但是在处理大量数据时,我想强调使用分页功能的价值.请求中的性能趋向于非常线性,并且您需要检索的数据越多,检索所有数据所需的时间就越长.

I understand that your code has pagination built into it, but I want to stress the value in using pagination when working with large amounts of data. The performance in requests tends to be very linear, and the more data you have to retrieve the longer it is going to take to retrieve it all.

这篇关于Django REST Framework序列化非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆