最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

postgresql - DRF list api response so slow and cause CPU throttling - Stack Overflow

programmeradmin2浏览0评论

An API in my project works fine locally but when it is called in production, it slows the application's response time (all other APIs) and the container workload gets throttled for about 10 seconds.

here is my model.py:

class Event(BaseModel):
    user = models.ForeignKey(User, related_name='events', on_delete=models.CASCADE)
    other_anizers = models.ManyToManyField(User, related_name='anized_events')
    teachers = models.ManyToManyField(User, related_name='teached_events')
    sponsors = models.ManyToManyField(User, related_name='sponsored_events')
    uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
    title = models.CharField(max_length=150)
    slug = models.SlugField(max_length=300, unique=True)
    ...

view.py:

    @action(
        detail=False, 
        serializer_class=UserEventListSerializer, 
        url_path='user-events'
    )
    def user_events(self, request):
        '''
        Retrieves all events in which the user has participated.
        '''
        user = get_object_or_404(User, username=self.request.query_params.get('username'))
        queryset = self.filter_queryset(self.get_queryset())
        queryset = queryset.filter(
            Q(user=user) | 
            Q(other_anizers=user) | 
            Q(teachers=user) | 
            Q(sponsors=user)
        )
        page = self.paginate_queryset(queryset)
        serializer = self.get_serializer(
            page, many=True, context={'user': user, 'request': self.request}
        )
        return self.get_paginated_response(serializer.data)

At first, I thought it was the filter query with Q and OR, but when I ran it in the Django shell (in production) it got the data fine. Also, I use the same serializer on some other APIs that work fine so it's not the serializer.

Does anyone have an idea what it might be ??

发布评论

评论列表(0)

  1. 暂无评论