All three are correct, but if you examine the query plans using EXPLAIN
you'll see that the plans are very different.
See this SQLFiddle - note the differing execution times. Examine the query plans with "view execution plan".
In your case, the =
is probably using a b-tree index lookup for a single value, which should be very fast, while the ~
versions are probably doing sequential scans - and quite CPU-expensive ones at that, because it has to try the regex against every single row.
Handily I just wrote about this in answer to another post. See this answer which explains that LIKE
and SIMILAR TO
can use a suitably-created index for a prefix match like you're doing, but ~
cannot.
Try creating an index like CREATE INDEX atable_junk_txt_idx ON aTable(junk text_pattern_ops)
then using LIKE 'foo b%'
.
Note that every extra index will slow down every insert, update and delete, so don't create indexes you don't have to.
See Using explain on the Pg wiki.