<div dir="ltr"><div>More followup: <br><br>SELECT "r"."date", COUNT(DISTINCT "r"."id")<br>FROM "result" "r"<br>WHERE "r"."date" >= %(p1)s<br>GROUP BY "r"."date"<br>{'p1':datetime.date(2014, 10, 6)}<br><br></div>Is the SQL generated, and it's taking 51 seconds to run.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On 5 November 2014 08:12, Matthew Bell <span dir="ltr"><<a href="mailto:matthewrobertbell@gmail.com" target="_blank">matthewrobertbell@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Of course, as soon as I send the email, I find something that works:<br><br>models.select((r.date, models.count(r)) for r in models.Result if r.date >= datetime.date.today() - datetime.timedelta(days=10))[:]<br><br></div>Is this the most efficient method?<br><div><div class="gmail_extra"><div><div class="h5"><br><div class="gmail_quote">On 5 November 2014 08:02, Matthew Bell <span dir="ltr"><<a href="mailto:matthewrobertbell@gmail.com" target="_blank">matthewrobertbell@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hi, <br><br>I have naively tried to execute this query:<br><br>models.select((r.date, models.count(r.date)) for r in models.Result if r.date >= datetime.date.today() - datetime.timedelta(days=10)).without_distinct()<br><br></div>With the aim of counting how many times each date appears. I was doing this with collections.Counter, but this doesn't scale to millions of results (far too much CPU and memory used). What query can I use to make the database do the work?<br><br>Thanks<span><font color="#888888"><br><div><div>-- <br><div>Regards,<br><br>Matthew Bell<br></div>
</div></div></font></span></div>
</blockquote></div><br><br clear="all"><br></div></div><span class="HOEnZb"><font color="#888888">-- <br><div>Regards,<br><br>Matthew Bell<br></div>
</font></span></div></div></div>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">Regards,<br><br>Matthew Bell<br></div>
</div>