title: How to optimize flask_admin search over strings
body: <p>I created an index on a text column, but searches using filters (contains) still takes like 15 seconds, and (equals) takes around 4 seconds.</p>
  
  <p>A search in the flask admin search box is similarly slow.</p>
  
  <p>So what would be a way to optimize this search? BTW: I'm talking 9 million strings. Simply searching this in pure python would be a lot faster (when in memory).</p>
  
  <p>Here's the analyze:</p>
  
  <pre><code>... text = 'project'
  
  Aggregate  (cost=546598.03..546598.04 rows=1 width=0) (actual time=1742.969..1742.969 rows=1 loops=1)
     -&gt;  Seq Scan on twitter  (cost=0.00..546597.64 rows=156 width=0) (actual time=1742.966..1742.966 rows=0 loops=1)
           Filter: ((text)::text = 'project'::text)
           Rows Removed by Filter: 9129062
   Planning time: 0.159 ms
   Execution time: 1743.000 ms
  (6 rows)
  
  ... text ilike '%project%'
  
   Aggregate  (cost=547376.38..547376.39 rows=1 width=0) (actual time=18154.062..18154.062 rows=1 loops=1)
     -&gt;  Seq Scan on twitter  (cost=0.00..546635.55 rows=296332 width=0) (actual time=0.177..18101.235 rows=498397 loops=1)
           Filter: ((text)::text ~~* '%project%'::text)
           Rows Removed by Filter: 8631301
   Planning time: 1.142 ms
   Execution time: 18154.104 ms
  (6 rows)
  </code></pre>