Avoiding “OR” for Better Query Performance
The simple query:<p><pre><code>    explain (analyze, timing) SELECT id FROM a
    WHERE a_val LIKE &#x27;something%&#x27;
       OR a_val LIKE &#x27;other%&#x27;;
                                                           QUERY PLAN                                                       
    ------------------------------------------------------------------------------------------------------------------------
     Bitmap Heap Scan on a  (cost=8.87..12.88 rows=20 width=4) (actual time=0.012..0.012 rows=0 loops=1)
       Recheck Cond: ((a_val ~~ &#x27;something%&#x27;::text) OR (a_val ~~ &#x27;other%&#x27;::text))
       Filter: ((a_val ~~ &#x27;something%&#x27;::text) OR (a_val ~~ &#x27;other%&#x27;::text))
       -&gt;  BitmapOr  (cost=8.87..8.87 rows=1 width=0) (actual time=0.011..0.011 rows=0 loops=1)
             -&gt;  Bitmap Index Scan on a_val_idx  (cost=0.00..4.43 rows=1 width=0) (actual time=0.009..0.009 rows=0 loops=1)
                   Index Cond: ((a_val ~&gt;=~ &#x27;something&#x27;::text) AND (a_val ~&lt;~ &#x27;somethinh&#x27;::text))
             -&gt;  Bitmap Index Scan on a_val_idx  (cost=0.00..4.43 rows=1 width=0) (actual time=0.001..0.001 rows=0 loops=1)
                   Index Cond: ((a_val ~&gt;=~ &#x27;other&#x27;::text) AND (a_val ~&lt;~ &#x27;othes&#x27;::text))
     Planning Time: 0.088 ms
     Execution Time: 0.033 ms
    (10 rows)
</code></pre>
using union:<p><pre><code>    explain (analyze, timing)
    select id from a
    where a_val like &#x27;something%&#x27;
    union
    select id from a
    where a_val like &#x27;other%&#x27;;
                                                              QUERY PLAN                                                          
    ------------------------------------------------------------------------------------------------------------------------------
     HashAggregate  (cost=17.23..17.43 rows=20 width=4) (actual time=0.135..0.135 rows=0 loops=1)
       Group Key: a.id
       -&gt;  Append  (cost=0.42..17.18 rows=20 width=4) (actual time=0.133..0.133 rows=0 loops=1)
             -&gt;  Index Scan using a_val_idx on a  (cost=0.42..8.44 rows=10 width=4) (actual time=0.128..0.128 rows=0 loops=1)
                   Index Cond: ((a_val ~&gt;=~ &#x27;something&#x27;::text) AND (a_val ~&lt;~ &#x27;somethinh&#x27;::text))
                   Filter: (a_val ~~ &#x27;something%&#x27;::text)
             -&gt;  Index Scan using a_val_idx on a a_1  (cost=0.42..8.44 rows=10 width=4) (actual time=0.004..0.004 rows=0 loops=1)
                   Index Cond: ((a_val ~&gt;=~ &#x27;other&#x27;::text) AND (a_val ~&lt;~ &#x27;othes&#x27;::text))
                   Filter: (a_val ~~ &#x27;other%&#x27;::text)
     Planning Time: 0.124 ms
     Execution Time: 0.164 ms
    (11 rows)
</code></pre>
using gin index:<p><pre><code>    EXPLAIN (analyze, timing)
    SELECT id FROM a
    WHERE a_val LIKE ANY (ARRAY[&#x27;something%&#x27;, &#x27;other%&#x27;]);
                                                            QUERY PLAN                                                        
    --------------------------------------------------------------------------------------------------------------------------
     Bitmap Heap Scan on a  (cost=176.15..247.11 rows=20 width=4) (actual time=0.036..0.036 rows=0 loops=1)
       Recheck Cond: (a_val ~~ ANY (&#x27;{something%,other%}&#x27;::text[]))
       -&gt;  Bitmap Index Scan on a_val_trgm_idx  (cost=0.00..176.15 rows=20 width=0) (actual time=0.034..0.034 rows=0 loops=1)
             Index Cond: (a_val ~~ ANY (&#x27;{something%,other%}&#x27;::text[]))
     Planning Time: 0.091 ms
     Execution Time: 0.193 ms
    (6 rows)

</code></pre>
Doesn&#x27;t the simple query win?