我正在寻找加速需要使用 distinct 的查询,因为它有一个选择的 M2M 字段。在这一点上,我不确定我的速度问题是否与我的数据库服务器配置方式有关,或者是否与我的查询集有关。
我的问题:最快的查询集是什么,我还可以通过更改我的 Postgresql 设置来提高速度吗?
Postgresql 服务器信息
实例:EC2 m1.xlarge
Postgresql 版本:9.1
文章记录:240,695
总内存:14980 MB
shared_buffers:3617MB Effective_cache_size
:8000MB
work_mem:40MB
checkpoint_segments:10
maintenance_work_mem:64MB
相关型号
class AuthorsModelMixin(models.Model):
authors = models.ManyToManyField('people.Person', blank=True)
nonstaff_authors = models.CharField(
verbose_name='Non-staff authors', max_length=255, blank=True,
help_text="Used for the name of the author for non-staff members.")
byline_title = models.CharField(
max_length=255, blank=True,
help_text="Often contains an organization. Title of the person, or " \
"entity associated with the byline and a specified person " \
"(i.e. Associated Press).")
class Meta:
abstract = True
class TaxonomyModelMixin(models.Model):
sections = models.ManyToManyField(Section, blank=True)
tags = TaggableManager(
blank=True, help_text='A comma-separated list of tags (i.e. ' \
'Outdoors, Election, My Great News Topic).')
class Meta:
abstract = True
class PublishModelMixin(models.Model):
status_choices = (
('D', 'Draft'),
('P', 'Published'),
('T', 'Trash'),
)
comment_choices = (
('enabled', 'Enabled'),
('disabled', 'Disabled'),
)
sites = models.ManyToManyField(Site, default=[1])
status = models.CharField(
max_length=1, default='P', db_index=True, choices=status_choices,
help_text='Only published items will appear on the site')
published = models.DateTimeField(
default=timezone.now, db_index=True,
help_text='Select the date you want the content to be published.')
is_premium = models.BooleanField(
choices=((True, 'Yes'), (False, 'No')),
verbose_name='Premium Content', default=True)
comments = models.CharField(
max_length=30, default='enabled',
choices=comment_choices, help_text='Enable or disable comments.')
created = models.DateTimeField(auto_now_add=True)
modified = models.DateTimeField(auto_now=True)
objects = PublishedManager()
class Meta:
abstract = True
class Article(AuthorsModelMixin, TaxonomyModelMixin, PublishModelMixin):
title = models.CharField(max_length=255)
slug = SlugModelField(max_length=255)
lead_photo = models.ForeignKey('media.Photo', blank=True, null=True)
summary = models.TextField(blank=True)
body = models.TextField()
我尝试过的查询集
查询集 1
查询时间:(76 毫秒)
优点:快速且不可能不显示已发布的文章
缺点:如果更高的 id 具有较旧的发布日期,则文章列表将无序
queryset = Article.objects \
.published() \
.filter(sections__full_slug__startswith=section.full_slug) \
.prefetch_related('lead_photo', 'authors') \
.order_by('-id') \
.distinct('id')
查询集 2
查询时间:(76 毫秒)
优点:文章始终井井有条
缺点:如果两篇文章的发布日期和时间相同,则只会列出一篇
queryset = Article.objects \
.published() \
.filter(sections__full_slug__startswith=section.full_slug) \
.prefetch_related('lead_photo', 'authors') \
.order_by('-published') \
.distinct('published')
查询集 3
查询时间:(1007 毫秒)
优点:文章一直都是有序的,没有文章不被列出的机会
缺点:慢得多!
queryset = Article.objects \
.published() \
.filter(sections__full_slug__startswith=section.full_slug) \
.prefetch_related('lead_photo', 'authors') \
.order_by('-id', '-published') \
.distinct('id')
查询集 4
查询时间:(4797.85 毫秒)
优点:不多,但不使用DISTINCT ON
意味着它可以在 SQLite 等其他数据库上进行测试
缺点:慢得多!!!
queryset = Article.objects \
.published() \
.filter(sections__full_slug__startswith=section.full_slug) \
.prefetch_related('lead_photo', 'authors') \
.order_by('-published') \
.distinct()