title: Check if a sentence is in a string in pl/pgsql
body: <p>I have a pl/pgsql script that needs to check if a word/sentence is in a string, and it must take care of word boundaries, and case insenstive.</p>
  
  <p>Example:</p>
  
  <ul>
  <li>String: "my label xx zz yy", Pattern: "my label", MATCH</li>
  <li>String: "xx my label zz", Pattern: "my label", MATCH</li>
  <li>String: "my labelxx zz", Pattern: "my label", NO MATCH</li>
  </ul>
  
  <p>So the obvious solution is to use a regex, like this:</p>
  
  <pre><code>select _label ~* (E'\\y' || _pattern || E'\\y') into _match;
  </code></pre>
  
  <p>It works but is slow, compared to a simple </p>
  
  <pre><code>select _label ilike '%' || _pattern || '%' into _match;
  </code></pre>
  
  <p>This is wrapped in a function that my script calls A LOT (in the tens of millions, I do a lot of recursion), and with this requirement the overall runtime doubled.</p>
  
  <p>Now my question is, is there a faster way to implement this ?</p>
  
  <p>Thanks.</p>
  
  <p>EDIT: ended up using this:</p>
  
  <pre><code>if _label ilike '%' || _pattern || '%' then
      select _label ~* (E'\\m' || _pattern || E'\\M') into _match;
  end if;
  </code></pre>
  
  <p>and it is significantly faster.</p>