title: Huge process time differences depending on slight query paremeters difference
body: <p>I have two models, PartMaster and Location, with a one to many relationship. I have to perform a search on the field part_masters.combo and locations.ubicacion, by using left join of the two tables.</p>
  
  <p>My problem is that the same query process time is absolutely different depending on how the parameters of the query are. </p>
  
  <p>This query has an execution time of around <strong>450 ms</strong>.</p>
  
  <p>The plan for the query is this</p>
  
  <p><a href="http://tatiyants.com/pev/#/plans/plan_1527591568019" rel="nofollow noreferrer">plan for the query looking for 'P0'</a></p>
  
  <pre><code>SELECT DISTINCT "part_masters".* FROM "part_masters" LEFT OUTER JOIN 
  "locations" ON "locations"."sap_cod" = "part_masters"."sap_cod" WHERE 
  (unaccent(locations.ubicacion) ILIKE unaccent('%P0%')) AND 
  (unaccent(part_masters.combo) ILIKE unaccent('%junta%')) AND 
  (unaccent(part_masters.combo) ILIKE unaccent('%torica%')) ORDER BY 
  "part_masters"."sap_cod" ASC
  </code></pre>
  
  <p>And this other query, just changing 'P0' for 'P01'as query parameter of locations.ubicacion takes <strong>38 secs</strong> to get executed, i got outtimed most of the time. </p>
  
  <p><a href="http://tatiyants.com/pev/#/plans/plan_1527591500155" rel="nofollow noreferrer">plan for the query looking for 'P01'</a></p>
  
  <pre><code>SELECT DISTINCT "part_masters".* FROM "part_masters" LEFT OUTER JOIN 
  "locations" ON "locations"."sap_cod" = "part_masters"."sap_cod" WHERE 
  (unaccent(locations.ubicacion) ILIKE unaccent('%P01%')) AND 
  (unaccent(part_masters.combo) ILIKE unaccent('%junta%')) AND 
  (unaccent(part_masters.combo) ILIKE unaccent('%torica%')) ORDER BY 
  "part_masters"."sap_cod" ASC
  </code></pre>
  
  <p>ANALYZE OUTPUT:</p>
  
  <pre><code>Unique  (cost=3880.72..3880.77 rows=1 width=242) (actual 
  time=39902.298..39902.305 rows=8 loops=1)
  -&gt;  Sort  (cost=3880.72..3880.73 rows=1 width=242) (actual 
  time=39902.297..39902.297 rows=8 loops=1)
      Sort Key: part_masters.sap_cod, part_masters.id, 
  part_masters.descripcion_maestro, part_masters.ref_fabricante, 
  part_masters.fabricante, part_masters.stock, part_masters.precio_medio, 
  part_masters.planta_cod, part_masters.planta_nombre, 
  part_masters.unidad_medida, part_masters.grupo_compras, 
  part_masters.created_at, part_masters.updated_at, 
  part_masters.combinada_maestro, part_masters.precio_estandar, 
  part_masters.fabricante_nombre, part_masters.combo
      Sort Method: quicksort  Memory: 29kB
      -&gt;  Nested Loop  (cost=0.00..3880.71 rows=1 width=242) (actual 
  time=10393.015..39902.250 rows=8 loops=1)
            Join Filter: ((part_masters.sap_cod)::text = 
  (locations.sap_cod)::text)
            Rows Removed by Join Filter: 438318
            -&gt;  Seq Scan on part_masters  (cost=0.00..2451.75 rows=1 
   width=242) (actual time=2.135..315.211 rows=262 loops=1)
                  Filter: ((unaccent(combo) ~~* unaccent('%junta%'::text)) AND 
   (unaccent(combo) ~~* unaccent('%torica%'::text)))
                  Rows Removed by Filter: 38408
            -&gt;  Seq Scan on locations  (cost=0.00..1409.24 rows=1578 width=5) 
   (actual time=0.107..148.671 rows=1673 loops=262)
                  Filter: (unaccent((ubicacion)::text) ~~* 
   unaccent('%P01%'::text))
                  Rows Removed by Filter: 37586
   Total runtime: 39902.358 ms
  </code></pre>
  
  <p>I have this index in combo, is not being used.</p>
  
  <p>part_masters_on_combo_idx UNUSED (trigram index)</p>
  
  <p>There isn't any index for locations.ubicacion.</p>
  
  <p>According to the plans I linked, there is a huge difference in both Actual Loops and Shared Hit Blocks number, I don't know if that helps</p>