PostGIS PointField查询的奇怪性能行为

时间:2017-02-20 20:44:43

标签: django postgresql django-models postgis geodjango

Django = 1.10.4

PostgreSQL = 9.6.1

POSTGIS = " 2.3.0 r15146" GEOS =" 3.5.0-CAPI-1.9.0 r4084" PROJ ="相对。 2015年9月8日9。9。2" GDAL =" GDAL 2.1.1,2010 / 07/07"的libxml =" 2.9.1" LIBJSON =" 0.12"光栅

我的用户类

class User(AbstractUser, TimeStampedModel):
    """Custom user model.
    Attributes:
        avatar (file): user's avatar, cropped to fill 300x300 px
        location (point): latest known GEO coordinates of the user
        location_updated (datetime): latest time user updated coordinates
        notifications (dict): settings for notifications to user
        is_appuser (bool): if True user can access to apps
    """
    AVATAR_IMAGE_SIZE = (300, 300)
    avatar = imagekitmodels.ProcessedImageField(
        upload_to=upload_user_media_to,
        processors=[ResizeToFill(*AVATAR_IMAGE_SIZE)],
        format='PNG',
        options={'quality': 100},
        editable=True,
        null=True,
        blank=True
    )

    location = gis_models.PointField(default=Point(x=0, y=0, srid=4326),
                                     blank=True,
                                     srid=4326)
    location_updated = models.DateTimeField(null=True,
                                            blank=True,
                                            editable=False)
    notifications = HStoreField(null=True)
    is_appuser = models.BooleanField(default=True)

    # so authentication happens by email instead of username
    # and username becomes sort of nick
    USERNAME_FIELD = 'email'

    # Make sure to exclude email from required fields if authentication
    # is done by email
    REQUIRED_FIELDS = ['username']

    def __str__(self):
        return self.username

    class Meta:
        verbose_name = 'User'
        verbose_name_plural = 'Users'

    def get_conferences(self):
        """Get confrences where the user is attendee.

        Returns:
            QuerySet of conferences.

        """

        return Conference.objects.filter(attendees=self)

现在,如果我使用所有字段查询记录,则查询的效果为 70ms ,如屏幕截图1所示。如果我删除' location'来自查询的字段 - 性能 3ms (请参阅两个屏幕截图中的第二个查询)

screenshot 1

现在从查询

中删除 location 字段

screenshot 2

但是,如果我在PSQL中运行相同的查询(即绕过Django ORM层),那么查询没有区别

staging=> \timing
Timing is on.
staging=> select id,location from users_user where id=20;
 id |                      location                      
----+----------------------------------------------------
 20 | 0101000020E61000009A9999999999F13F9A9999999999F13F
(1 row)

Time: 1.310 ms
staging=> select id from users_user where id=20;
 id 
----
 20
(1 row)

Time: 1.300 ms
staging=>

0 个答案:

没有答案