title: Improving Django Slice Step Performance
body: <p>I have an application where I am using a Django instance to store logs from a number of separate entities.  I then have a UX component that grabs log files from the django instance and graphs the values.  To keep things reasonable, I try to only grab a sample of the log values, then increase the granularity as the user zooms in.
  My problem is that as the number of logs grows, the time to grab each downsampled group of logs is growing unsustainably.</p>
  
  <p>Here is a simplified version of the model</p>
  
  <pre><code>class LogModel(models.Model):
     localtime = models.DateTimeField(db_index=True)
     value1 = models.FloatField(blank=True, null=True)
     value1 = models.FloatField(blank=True, null=True)
     value1 = models.FloatField(blank=True, null=True)
     owner = models.ForeignKey('auth.User', related_name='logs')
  </code></pre>
  
  <p>Here is a sample query I use to get the data:</p>
  
  <pre><code>q = LogModel.objects.filter(owner=SomeOwner).order_by('localtime')
  qNum = q.count()
  logs = q[:qNum:(qNum/1000)]
  </code></pre>
  
  <p>Sometimes running this query takes a very long time (~16s).  The number of logs in a large device right now is ~150K.  If there are other things hitting the database, it can take a very long time (>1min).</p>
  
  <p>Other information:
  System:
  VM w/ 2 CPU, 4GB RAM
  DB: PostgreSQL 9.3
  OS: Ubuntu 14.04</p>
  
  <p>I have tried to follow the general optimizing the database guidelines, but with little luck.  </p>
  
  <p>Things that I have tried:
  Increased the ram to the database.
  Limiting the number of concurrent heavy database queries (3).  </p>
  
  <p>The total database is not very large, and should definitely fit into the 1GB I allocated to the database.  I feel like I am missing some very basic understanding of how this works, or a basic optimization.
  Thanks,</p>