title: Slow iteration over django queryset
body: <p>I am iterating over a django queryset that contains anywhere from 500-1000 objects. The corresponding model/table has 7 fields in it as well. The problem is that it takes about 3 seconds to iterate over which seems way too long when considering all the other data processing that needs to be done in my application.</p>
  
  <p>EDIT:
  Here is my model:</p>
  
  <pre><code>    class Node(models.Model):
          node_id = models.CharField(null=True, blank=True, max_length=30)
          jobs = models.TextField(null=True, blank=True)
          available_mem = models.CharField(null=True, blank=True, max_length=30)
          assigned_mem = models.CharField(null=True, blank=True ,max_length=30)
          available_ncpus = models.PositiveIntegerField(null=True, blank=True)
          assigned_ncpus = models.PositiveIntegerField(null=True, blank=True)
          cluster = models.CharField(null=True, blank=True, max_length=30)
          datetime = models.DateTimeField(auto_now_add=False)
  </code></pre>
  
  <p>This is my initial query, which is very fast:</p>
  
  <pre><code>    timestamp = models.Node.objects.order_by('-pk').filter(cluster=cluster)[0]
      self.nodes = models.Node.objects.filter(datetime=timestamp.datetime)
  </code></pre>
  
  <p>But then, I go to iterate and it takes 3 seconds, I've tried two ways as seen below:</p>
  
  <pre><code>    def jobs_by_node(self):
      """returns a dictionary containing keys that
         are strings of node ids and values that
         are lists of the jobs running on that node."""
      jobs_by_node = {}
  
      #iterate over nodes and populate jobs_by_node dictionary
      tstart = time.time()
      for node in self.nodes:
          pass #I have omitted the code because the slowdown is simply iteration
      tend = time.time()
      tfinal = tend-tstart
      return jobs_by_node
  </code></pre>
  
  <p>Other method:</p>
  
  <pre><code>    all_nodes = self.nodes.values('node_id')
      tstart = time.time()
      for node in all_nodes:
          pass
      tend = time.time()
      tfinal = tend-tstart
  </code></pre>
  
  <p>I tried the second method by referring to <a href="https://stackoverflow.com/questions/5656238/how-do-i-speed-up-iteration-of-large-datasets-in-django">this</a> post, but it still has not sped up my iteration one bit. I've scoured the web to no avail. Any help optimizing this process will be greatly appreciated. Thank you.</p>
  
  <p>Note: I'm using Django version 1.5 and Python 2.7.3</p>