Django QuerySet performance - Stack Overflow

I'm debugging sitemap generation, and finally have the following snippet:items=Event.objects.all()

I'm debugging sitemap generation, and finally have the following snippet:

        items=Event.objects.all()
        for limit in (1000, 10000, 20000):
            events=items[:limit]
            print(f'{limit=}')
            start=time()
            for obj in events.values('id', 'slug', 'date_updated'):
                s='/events/{0[slug]}_{0[id]}_{0[date_updated]}/'.format(obj)
            print(f'  Time spent: {time()-start:.2f}')
            start=time()
            for obj in events.only('slug', 'date_updated'):
                s=f'/events/{obj.slug}_{obj.pk}_{obj.date_updated}/'
            print(f'  Time spent: {time()-start:.2f}')

And I have the following output:

limit=1000
  Time spent: 0.26
  Time spent: 9.80
limit=10000
  Time spent: 0.66
  Time spent: 85.09
limit=20000
  Time spent: 1.18
  Time spent: 177.20

I see in the doc that QuerySet.values() is recommended for few fields and when you don't need the complete Model behaviour, which is indeed the case here. But is the overhead of creating Model instances (and whatever more there is under the hood) indeed SO huge?

Or it's something about my setup and (mis)configuration. When it's doing models, I see both python and mysql processes consume CPU. And it's inexplicable, because same query when I run it in mysql client takes half a second.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742358448a4428908.html

相关推荐

  • Django QuerySet performance - Stack Overflow

    I'm debugging sitemap generation, and finally have the following snippet:items=Event.objects.all()

    3天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信