title: Django haystack whoosh super slow
body: <p>I have a simple setup with django-haystack and whoosh engine. A search yielding 19 objects took me 8 seconds. I used the django-debug-toolbar to determine that i had a bunch of repeated queries.</p>
  
  <p>I then updated my search view to prefetch relations, so that duplicate queries would not happen:</p>
  
  <pre><code>class MySearchView(SearchView):
      template_name = 'search_results.html'
      form_class = SearchForm
      queryset = RelatedSearchQuerySet().load_all().load_all_queryset(
          models.Customer, models.Customer.objects.all().select_related('customer_number').prefetch_related(
              'keywords'
          )
      ).load_all_queryset(
          models.Contact, models.Contact.objects.all().select_related('customer')
      ).load_all_queryset(
          models.Account, models.Account.objects.all().select_related(
              'customer', 'account_number', 'main_contact', 'main_contact__customer'
          )
      ).load_all_queryset(
          models.Invoice, models.Invoice.objects.all().select_related(
              'customer', 'end_customer', 'customer__original', 'end_customer__original', 'quote_number', 'invoice_number'
          )
      ).load_all_queryset(
          models.File, models.File.objects.all().select_related('file_number', 'customer').prefetch_related(
              'keywords'
          )
      ).load_all_queryset(
          models.Import, models.Import.objects.all().select_related('import_number', 'customer').prefetch_related(
              'keywords'
          )
      ).load_all_queryset(
          models.Event, models.Event.objects.all().prefetch_related('customers', 'contracts', 'accounts', 'keywords')
      )
  </code></pre>
  
  <p>But even then, the search still takes 5 seconds. I then used the profiler from <code>django-debug-toolbar</code>, which gave me this information:</p>
  
  <p><a href="https://i.stack.imgur.com/254ZD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/254ZD.png" alt="Django debug toolbar profiler results"></a></p>
  
  <p>From what I can tell, the issue lies in <code>haystack/query:779::__getitem__</code>, which is hit twice, each costing 1.5 seconds. I have glanced through the code in question, but cannot make sense of it. So where do I go from here?</p>