I built a search box once with WHERE name LIKE '%' || $1 || '%'. It felt smart. I was matching anything, anywhere in the string. Users could type “apple” and find “Red Apple Pie.”

It worked fine when we had 5,000 rows.

At a few million rows, every search took seconds. The database CPU spiked to 100% and stayed there. My users saw a spinner that never went away.

I thought I needed a better server. I didn’t. I needed a different index.


Why LIKE '%term%' kills performance

The problem isn’t the LIKE operator. It’s the leading wildcard.

A standard B-tree index in SQL is sorted from the start of the string. If you search for term% (prefix match), the database can jump straight to where “term” starts in the sorted list. That’s fast.

But if you search for %term%, the value could be anywhere. The index is sorted by the first letter, not the middle one. So the database has no choice but to scan every single row. It reads the whole table. Every time.

You can verify this yourself. Run your query with EXPLAIN ANALYZE in Postgres. Look for a line that says Seq Scan. That’s your smoking gun. It means “sequential scan,” or “I’m reading everything because I have to.”

If you see an Index Scan or Bitmap Index Scan, you’re usually okay. If you see Seq Scan on a large table, you’re paying the price for that leading %.


Prefix search is enough more often than you think

Before you over-engineer this, ask yourself: do users actually need to match words in the middle of a string?

For autocomplete on names or emails, usually no. If I’m typing “gre” to find “greg@example.com,” I don’t care if it matches “mcgregor@example.com” in the middle. I want the start.

In Postgres, there’s a catch. If your database collation isn’t C, a standard B-tree index won’t work for LIKE prefix matching because the sort order follows locale rules. You need to tell it to use pattern-based sorting.

Here’s what I do:

CREATE INDEX idx_users_email_prefix ON users (email text_pattern_ops);

That one line makes LIKE 'term%' fast. No extension needed. Just a different index type.

I use this for almost every search box that completes as you type. It’s cheap, it’s fast, and it solves 80% of my problems.


Use pg_trgm when you need “contains”

Sometimes prefix isn’t enough. Maybe I’m searching product names and the keyword could be in the middle. “Running Shoes for Men.” If I type “shoes,” I want a hit.

That’s where pg_trgm comes in. It creates an index based on trigrams (three-character sequences). It allows the database to look for substrings without scanning every row.

Set it up like this:

CREATE EXTENSION IF NOT EXISTS pg_trgm;

CREATE INDEX idx_products_name_trgm ON products USING gin (name gin_trgm_ops);

Now LIKE '%term%' and ILIKE '%term%' can use the index. This is my default for “contains” searches on short-to-medium length strings.

One caveat: it works best when the search term is at least three characters long. If someone types “a” or “an,” the trigram index helps less. But for real words, it’s a massive win over a sequential scan.


Full-text search for long text

If you’re searching articles, blog posts, or product descriptions, pg_trgm is still an option, but it’s not the best fit. You’re dealing with words, grammar, and language structure.

That’s where Postgres full-text search shines. It handles stemming, which means “running” matches “run.” You can rank results with ts_rank. It ignores stop words like “the” and “and.”

Here’s how I query it:

SELECT *
FROM articles
WHERE to_tsvector('english', body) @@ plainto_tsquery('english', $1);

You need a GIN index on the vector for speed:

CREATE INDEX idx_articles_fts ON articles USING gin (to_tsvector('english', body));

This is not for partial words. If I search for “sho,” it won’t find “shoes.” Full-text search matches whole words and their stems, not substrings. It’s language-aware, not substring-aware.

For long text, use this. For names and short codes, use pg_trgm or prefix indexes.


MySQL has its own version

If you’re on MySQL, you don’t have pg_trgm. You have FULLTEXT.

The setup is slightly different. You add a FULLTEXT index to your table:

ALTER TABLE articles ADD FULLTEXT(title, body);

Then you query it with MATCH and AGAINST:

SELECT *
FROM articles
WHERE MATCH(title, body) AGAINST ('search words' IN NATURAL LANGUAGE MODE);

It works similarly to Postgres full-text search. It’s good for natural language queries. It’s not as flexible as the Postgres ecosystem, but it gets the job done for basic text search without pulling in a separate service.


A small bonus for multiple patterns

I used to chain OR statements when I needed to match multiple patterns. It got ugly fast.

WHERE name LIKE '%foo%' OR name LIKE '%bar%' OR name LIKE '%baz%'

In Postgres, you can use the ANY operator with an array. It’s cleaner and sometimes optimizes better:

WHERE name LIKE ANY (ARRAY['%foo%', '%bar%', '%baz%'])

I wrote a post about LIKE and IN before, but this array trick is underused. If you’re doing multiple pattern matches in one query, try it. It keeps your SQL readable.


When to stop fighting SQL

If you need typo tolerance, advanced ranking, faceted filtering, or instant results across millions of documents with complex queries, SQL is going to hurt.

That’s when I reach for a dedicated search engine like Meilisearch or Typesense. They handle the heavy lifting: tokenization, ranking, fuzzy matching, and indexing at scale.

But I don’t jump there immediately. I try pg_trgm first. If that stops being enough, then I add a search service. Premature abstraction is its own kind of bug.


Pick the index for the search you actually do

LIKE isn’t the problem. The leading wildcard is.

If you’re doing prefix searches, use text_pattern_ops. If you’re doing substring searches on short text, use pg_trgm. If you’re searching long documents, use full-text search.

Stop scanning every row because you were lazy about indexing. Your database will thank you. And your users will stop staring at the spinner.