简体   繁体   English

Azure PostgreSQL 灵活服务器的数据库 Django 慢

[英]Azure Database for PostgreSQL flexible server Slow with Django

Am using Django to connect to Azure Database for PostgreSQL flexible server but it's very slow, below are the specs of the server:我正在使用 Django 连接到Azure Database for PostgreSQL flexible server ,但它非常慢,以下是服务器的规格:

Compute + storage计算+存储

Pricing tier
Memory Optimized
Compute size
Standard_E4s_v3 (4 vCores, 32 GiB memory, 6400 max iops)
Storage
32 GiB

High Availability:高可用性:

High availability
Enabled
High availability mode
ZoneRedundant
Availability zone
2
Standby availability zone
1

Specs眼镜

眼镜

As you can see above, the specs are high, but the performance isn't better, in postman when I was hitting to get data it took 34.5 seconds , this is way too much to wait.正如你在上面看到的,规格很高,但性能并没有更好,在 postman 中,当我点击获取数据时需要34.5 seconds ,这实在是太等待了。

In my Django code, I tried my best to optimize the queries, and on Heroku, it was super fast, however here on Azure it's extremely slow, what could be done to improve the speed of the server?在我的 Django 代码中,我尽力优化查询,在 Heroku 上,它非常快,但是在 Azure 上它非常慢,可以做些什么来提高服务器的速度?

For more information on Django this is the view.py of the endpoint:有关 Django 的更多信息,这是端点的view.py

@method_decorator(cache_page(60 * 60 * 4), name='get')
@method_decorator(vary_on_cookie, name='get')
class PostList(generics.ListCreateAPIView):
    """Blog post lists"""
    queryset = Post.objects.filter(status=APPROVED).select_related(
        "owner", "grade_level", "feedback").prefetch_related(
        "bookmarks", "likes", "comments",
        "tags", "tags__following").prefetch_related("address_views")
    serializer_class = serializers.PostSerializer
    authentication_classes = (JWTAuthentication,)
    permission_classes = (PostsProtectOrReadOnly, IsMentorOnly)

    def filter_queryset(self, queryset):
        ordering = self.request.GET.get("order_by", None)
        author = self.request.GET.get("author", None)
        search = self.request.GET.get("search", None)
        tag = self.request.GET.get("tag", None)

        # filter queryset with filter_backends 🖟
        queryset = super().filter_queryset(queryset)
        if ordering == 'blog_views':
            queryset = queryset.annotate(
                address_views_count=Count('address_views')).order_by(
                '-address_views_count')

        if author:
            queryset = queryset.filter(owner__email=author).select_related(
                "owner", "grade_level", "feedback").prefetch_related(
                "bookmarks", "likes", "comments", "tags",
                "tags__following").prefetch_related("address_views")

        if tag:
            queryset = queryset.filter(
                tags__name__icontains=tag).select_related(
                "owner", "grade_level", "feedback").prefetch_related(
                "bookmarks", "likes", "comments", "tags",
                "tags__following").prefetch_related("address_views")

        if search:
            queryset = queryset.annotate(
                rank=SearchRank(SearchVector('title', 'body', 'description'),
                                SearchQuery(search))).filter(
                rank__gte=SEARCH_VALUE).order_by('-rank')

        return queryset

Then is my serializer.py :然后是我的serializer.py

class PostSerializer(SoftDeletionSerializer):
    """Post Serializer"""
    owner = UserProfile(read_only=True)
    tags = TagSerializer(many=True)
    comments = CommentSerializer(many=True, read_only=True)
    slug = serializers.SlugField(read_only=True)
    grade_level = GradeClassSerializer(many=False)

    class Meta:
        model = Post
        fields = ('uuid', 'id', 'title', 'body', 'owner', 'slug', 'grade_level',
                  'comments', 'tags', 'description', 'image',
                  'created', 'modified', 'blog_views', 'blog_likes',
                  'blog_bookmarks', 'status',
                  'read_time', 'is_featured',)
        readonly = ('id', 'status',) + SoftDeletionSerializer.Meta.fields

    def to_representation(self, instance):
        """Overwrite to serialize extra fields in the response."""
        representation = super(PostSerializer, self).to_representation(instance)
        representation['has_bookmarked'] = self.has_bookmarked(instance)
        representation['has_liked'] = self.has_liked(instance)
        return representation

    def has_liked(self, instance):
        return self.context['request'].user in list(instance.likes.all())

    def has_bookmarked(self, instance):
        return self.context['request'].user in list(instance.bookmarks.all())

In my settings.py , I don't have unused middleware or apps, I cleaned it up, and I think the issue is on the database, not the code.在我的settings.py ,我没有未使用的中间件或应用程序,我清理了它,我认为问题出在数据库上,而不是代码上。

What could improve the speed of Azure Database for PostgreSQL flexible server ?什么可以提高Azure Database for PostgreSQL flexible server的速度?

This latency issue could be because of region differences in Azure. You should have the backend-service, that communicates with the database, in the same region.此延迟问题可能是因为 Azure 中的区域差异。您应该在同一区域中拥有与数据库通信的后端服务。

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

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