8👍
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.
1👍
Man, don’t use “MyObj.objects.all().order_by(‘time’)”,
If you has above tens of hundreds of datas, then just get all and order by time,
it would takes so long……
“that is not the seriallizer’s problem, that is an order problem.”
you can limit the time you search, use:
1、gt:bigger then
now = datetime.datetime.now()
#yesterday
start = now – datetime.timedelta(hours=23, minutes=59, seconds=59)
a=yourobject.objects .filter(youdatetimcolumn__gt=start)
2、gte:bigger or equl then
a=yourobject.objects .filter(youdatetimcolumn__gte=start)
3、lt:littler then
a=yourobject.objects .filter(youdatetimcolumn__lt=start)
4、lte:littler or equal then
a=yourobject.objects .filter(youdatetimcolumn__lte=start)
5、range: a range of time
start_date = datetime.date(2005, 1, 1)
end_date = datetime.date(2005, 3, 31)
Entry.objects.filter(pub_date__range=(start_date, end_date))
6、year: one of the years
Entry.objects.filter(pub_date__year=2005)
7、month:one of the months
Entry.objects.filter(pub_date__month=12)
8、day:a day
Entry.objects.filter(pub_date__day=3)
9、week_day: a weekday
Entry.objects.filter(pub_date__week_day=2)
source: https://www.cnblogs.com/linjiqin/p/3821914.html
If you need to use get.all, then just not use “order_by_time”, in normal situation, not use order_by_time can make your requset faster, you just need to order it after get these datas.
0👍
It’s not a problem of the serializer, the problem its on the query.
You are getting all the objects, I assume there is a big number of them because of the pagination and also you want them all ordered by time. The problem is that the Model definition has no hint for the database to create a index on the time field.
Try to add the hint to create an index in the database and the speed will rise.
class MyObj(models.Model):
id = models.BigIntegerField(primary_key=True)
time = models.DateTimeField(db_index=True)
type = models.CharField(max_length=5)
user = models.ForeignKey('User', related_name='myobj')
- [Django]-How can I disable a third party API when executing Django unit tests?
- [Django]-How do I treat django's TemporaryUploadedFile as a regular python file
- [Django]-Django: Related model 'users.UserProfile' cannot be resolved