title: Django query using large amount of memory
body: <p>I have a query that is causing memory spikes in my application. The below code is designed to show a single record, but occasionally show 5 to 10 records. The problem is there are edge cases where 100,000 results are passed to MultipleObjectsReturned. I believe this causes the high memory usage. The code is:</p>
  
  <pre><code>try:
      record = record_class.objects.get(**filter_params)
      context["record"] = record
  
  except record_class.MultipleObjectsReturned:
      records = record_class.objects.filter(**filter_params)
      template_path = "record/%(type)s/%(type)s_multiple.html" % {"type": record_type}
      return render(request, template_path, {"records": records}, current_app=record_type)
  </code></pre>
  
  <p>I thought about adding a slice at the end of the filter query, so it looks like this:</p>
  
  <pre><code>records = record_class.objects.filter(**filter_params)[:20]
  </code></pre>
  
  <p>But the code still seems slow. Is there a way to limit the results to 20 in a way that does not load the entire query or cause high memory usage?</p>