简体   繁体   English

改善Django Rest框架中的分页和序列化时间

[英]Improving pagination and serialization time in django rest framework

I am using ListAPIView to render a list of objects. 我正在使用ListAPIView呈现对象列表。 The response returns paginated results with limit=50 . 响应返回limit=50分页结果。 The queryset size varies from 0 to a few hundred thousands. 查询集的大小从0到几十万不等。

What I have noticed is that when the queryset size is small, the API response time is small. 我注意到的是,当queryset大小较小时,API响应时间也较小。 But as the queryset size increases, the API response time becomes very large. 但是随着查询集大小的增加,API响应时间变得非常大。

Is pagination actually the culprit here? 分页实际上是这里的罪魁祸首吗?
Is there a way to optimize the response time for ListAPIView s using pagination? 有没有一种方法可以使用分页优化ListAPIView的响应时间?

As stated in the official django documentation, querysets are lazy . 如官方django文档中所述, querysets是lazy

QuerySets are lazy – the act of creating a QuerySet doesn't involve any database activity. QuerySet是惰性的–创建QuerySet的操作不涉及任何数据库活动。 You can stack filters together all day long, and Django won't actually run the query until the QuerySet is evaluated. 您可以整天将过滤器堆叠在一起,并且Django在评估QuerySet之前不会真正运行查询。

Also, pagination compliments queryset's laziness . 此外, 分页还可以补充queryset的惰性

Therefore, the culprit for your delay IS NOT pagination. 因此,造成延迟的罪魁祸首不是分页。
Case closed... 结案...


But what is it then? 但是那是什么呢?

What to do to get faster? 如何做才能更快?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM