title: How to improve Django-PostgreSQL search performance and increase HTML rendering or AJAX response speed?
body: <p>I am working on a full text search application in django with postgreSQL where the database has about <strong>50k</strong> rows currently. I am using <strong>SearchRank</strong> and <strong>TrigramSimilarity</strong> and have already implemented <strong>SearchVectorField</strong> and <strong>GIN indexes</strong>, but still the search results takes around <strong>5-10 seconds</strong> to display on the screen depending on the search complexity.</p>
  <p>I am using jquery <strong>ajax</strong> call to send the search query from UI to django views.py and after performing the search, filter and order operations, I am rendering a different HTML along with result query set using <strong>DTL</strong>. <em>The execution time taken inside the views.py function is around 0.5 seconds but the total time taken during the ajax call and response is about 5-6 seconds.</em></p>
  <p>Please see my code below:</p>
  <p>VIEWS.PY</p>
  <pre><code>start_time = time.time()
  
  search_query = SearchQuery(query)
  rank = SearchRank('search_vector', search_query, weights=[0.2, 0.4, 0.6, 0.8])
  trigram = (TrigramSimilarity('report_name', query_trigram) + TrigramSimilarity('tool', query_trigram) + TrigramSimilarity('owner', query_trigram)
  
  result_set = BI_Report.objects.annotate(rank=rank, trigram=trigram, total=rank+trigram).order_by('-total').filter(Q(trigram__gt=0.2) | Q(rank__gt=0.1))[:50]
  
  time_taken = time.time() - start_time
  
  return render(request, 'display.html', {&quot;result_set&quot;: result_set, 'query':query})
  </code></pre>
  <p>time_taken = 0.45 seconds</p>
  <p>GIN indexes are present on both SearchVectorField and other database fields and it's showing Bitmap heap scan when running SQL inside postgres. In my code, on using .explain() I could see that SearchRank is doing an index scan on the search vector field but TrigramSimilarity is doing a parallel seq scan.</p>
  <p>display HTML</p>
  <pre><code>{% for i in result_set %}
                          &lt;tr&gt;
                              &lt;td&gt;{{ i.report_name }}&lt;/td&gt;
                              &lt;td&gt;{{ i.tool }}&lt;/td&gt;
                              &lt;td&gt;{{ i.owner }}&lt;/td&gt;
                          &lt;/tr&gt;
  {% endfor %}
  </code></pre>
  <p>homepage &amp; AJAX</p>
  <pre><code>&lt;div id='result'&gt;&lt;/div&gt;
  &lt;script&gt;
              var starttime = new Date();
              $.ajax({
                  type: &quot;GET&quot;,
                  url: &quot;display/&quot;,
                  headers: {&quot;key&quot; : query},
                  success: function(responseData) {
                          document.getElementById('result').innerHTML = responseData;
                          total_time = new Date() - starttime;
                          console.log(total_time)
                      }
                  },
              });
  &lt;/script&gt;
  </code></pre>
  <p>total_time = 5800 milli-seconds</p>
  <p>I am passing the search query in the request headers to the views.py function and displaying the rendered   html with all results using document.getElementById.innerhtml.</p>
  <p><em>I am not sure what is causing the delay - is it the search operation itself or render/ajax call too?
  I want to display the search results on the screen within 2 seconds. Kindly please suggest in what way I can improve the search operation speed and increase the ajax call-response speed.</em></p>
  <p>I will be grateful for any help. Thank you!</p>