Django:通过删除340个重复查询来进行有效查询

时间:2017-01-27 16:31:29

标签: django django-views django-queryset

我白天绘制了一堆价格数据。认为股票交易是白天的。

我想做什么:

  • 按日显示交易
  • 显示价格的平均线以显示总体趋势

问题:

当我在查询中查看Django调试工具栏时,我看到:

  • 346查询
  • 1498.11ms
  • 查看实际查询,我会在每天查询时看到get_queryset()的“重复340次”。
  • 如何才能提高效率,以避免重复?任何有关如何使其尽可能高效的提示/技巧将不胜感激。

如何

我有一个视图,继承自GraphView,我已经返回了返回对象价格所需的数据。使用此请求可能会返回数千个结果,尽可能提高查询效率对于加载时间非常重要。

使用的工具:

  • Django 1.10.1
  • 的Postgres
  • 在模板中绘制结果图表
  • Django调试工具栏

观点&查询:

class GraphView(TemplateView):

    def get_dates(self):
        dates = []
        if self.get_queryset():
            start = self.get_queryset()[0][2].date()
            end = datetime.today().date()
            delta = end - start
            for i in range(delta.days + 1):
                dates.append(start + timedelta(days=i))
        return dates

    def trend_line(self):
        trades = self.get_queryset()
        dates = self.get_dates()
        data_x = []
        data_y = []

        for date in dates:
            subset = trades.filter(date_of_price__date=date)
            prices_for_day = subset.aggregate(Avg('price'))
            if prices_for_day['price__avg'] > 0:
                data_x.append(date.strftime('%Y-%m-%d'))
                data_y.append(prices_for_day['price__avg'])
        return data_x, data_y

    def get_context_data(self, **kwargs):
        context = super(GraphView, self).get_context_data(**kwargs)
        x_axis_date = []
        y_axis_price = []
        bubble_text = []
        for trade in self.get_queryset():
            x_axis_date.append(trade[2].date().strftime('%Y-%m-%d'))
            y_axis_price.append(int(trade[1]))
            desc = "#%s" % (trade[0])
            bubble_text.append(str(desc.encode('ascii', 'ignore')))

        trend_data_x, trend_data_y = self.trend_line()

        try:
            x_axis_date_start = x_axis_date[0]
        except IndexError:
            x_axis_date_start = None

        try:
            x_axis_date_end = x_axis_date[-1]
        except IndexError:
            x_axis_date_end = None

        context.update({
            "x_axis_date": x_axis_date,
            "x_axis_date_start": x_axis_date_start,
            "x_axis_date_end": x_axis_date_end,
            "y_axis_price": y_axis_price,
            "bubble_text": bubble_text,
            "trend_data_x": trend_data_x,
            "trend_data_y": trend_data_y,
        })
        return context


 class ReferenceDetailView(StaffuserRequiredMixin, SetHeadlineMixin, GraphView):
        headline = "Variation Detail"
        template_name = "ref_trades/reference_detail.html"

        def get_reference_model(self):
            return get_object_or_404(ReferenceModel, pk=self.kwargs["pk"])

        def get_headline(self):
            return "%s" % self.get_reference_model()

        def get_queryset(self):
            return TradeModel.objects.filter(
                date_of_price__gte=datetime.now() - timedelta(days=365),
                reference_model__id=self.kwargs["pk"]
            ).exclude(price=0).values_list('id', 'price' , 'date_of_price', 'title')

谢谢

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

您可以检索按日期排序的所有对象,然后使用itertools.groupby()将其拆分为日期,而不是每天执行查询。

def data_points(self):
    trades = self.get_queryset()
    data_x = []
    data_y = []

    for date, subset in itertools.groupby(trades, lambda t: t.date):
        average_price = average(subset) # average() needs to be implemented
        if average_price > 0:
            data_x.append(date.strftime('%Y-%m-%d'))
            data_y.append(average_price)
    return data_x, data_y 

此方法将Web服务器CPU换成DB CPU / IO,这可能是也可能不是最佳方法,具体取决于您的基础架构