11,000 products, 2.7 seconds per search: one OR too many
For the B2B shop Semsters we measured the search before the full range was imported. A single condition without an index made it slow. Here is what was behind it, how we fixed it and why the same principle applies in WordPress and WooCommerce.
We are a WordPress workshop, and this post is about a shop that is not WordPress. That is not a contradiction. The mistake we found there follows a principle that holds just as much in WordPress and WooCommerce, and we have measurements from before and after the fix.
First, some context: the numbers come from a test with test products that we ran before importing the full range, not from live operation. The shop itself is still in its demo phase and shows sample prices. That is exactly what such a test is for: it finds the mistake before a customer feels it.
What Semsters is
Semsters (called Landwiese until November 2026) is the B2B organic wholesale shop of Bauerntüte GmbH in Cologne. Bauerntüte has been an organic delivery service since 2013, and its founder is Manuel Perera. Semsters is aimed at day-care centres, canteens, restaurants and offices. Different rules apply there than in consumer retail: prices are shown net, every product has tiered prices, buyers work with company accounts, and purchases are made on invoice once the company has been approved. There are also recipes that scale to any number of portions, and an enquiry route for day-care centres.
There are 625 products live in the range, with sample prices. The goal is the full range of around 11,000 products, which comes from Bauerntüte's Excel master data spreadsheet.
Why not WooCommerce
We built the shop with Next.js, PostgreSQL and Drizzle on a server of its own. That was not a technical preference: the client explicitly wanted neither WordPress nor WooCommerce. On top of that came what the business demands, namely net prices, tiers per product, company accounts and a range of 11,000 products that comes from Excel. Whether WooCommerce with suitable extensions could have carried that as well, we have not measured, so we do not claim it could not.
For most shops WooCommerce remains the right choice. It is widespread, well documented, and anyone who knows it feels at home straight away. We are a WordPress workshop and we recommend it to most merchants. At Semsters the client's wish and the B2B shape of the business pointed the other way.
The test before the full import
With 625 products a database forgives almost anything. Whether search holds up with the full range is not something such a catalogue tells you. So before the import we measured it with 11,250 test products, roughly the size of the target. One search took 2.7 seconds. The suggestions while typing took 0.36 seconds, and that is noticeable, because they are meant to appear while you type and not afterwards.
The measurement ran locally, with Postgres as an embedded library (PGlite), not on the server. So do not rely on the absolute seconds. What carries weight is the ratio before and after the rebuild.
The pattern: an OR with one part that has no index
Search had to do two things: look for words, with German word-stem recognition, and forgive typos. Someone who types “Kartofel” should still find potatoes. Both lived in a single condition, joined by an OR. Simplified, it looked like this, where $2 is the similarity threshold:
-- before, simplified
WHERE to_tsvector('german', suchtext) @@ websearch_to_tsquery('german', $1)
OR bezeichnung ILIKE '%' || $1 || '%'
OR ean = $1
OR word_similarity($1, bezeichnung) > $2
The column names are German (suchtext is the text that gets searched, bezeichnung the product name), because that is how they are in the shop. This reads like the task itself and looks harmless. The problem is in the last line. word_similarity() is a function, and an index cannot serve a function call in a WHERE. A trigram index meant for exactly this kind of similarity search was already sitting on the product name. It splits every word into overlapping runs of three letters and remembers which rows contain them. “Kartofel” and “Kartoffel” share almost all of them, so one can be found through the other. The index went unused because indexes attach to operators, not to function calls.
With an AND this would have been half as bad: Postgres would take the index of one part and check the rest against the few matches. With an OR every part has to be able to return matches on its own. If even one cannot, the only route left is through all rows. That is exactly what happened: on every search Postgres read all the products.
Two amplifiers came on top. The full-text value was recomputed for every row on every search, although it only changes when the product changes. And that happened ten times on every page view.
What we changed
- The search words are stored in a column. The searched text becomes a ready-made word list (in Postgres a
tsvector), as a generated column that the database keeps up to date itself. A GIN index sits on top of it. Search no longer computes anything per row. - The typo check runs through the
<%operator. It asks whether a word in the product name is similar enough to the search term and, unlike the function, may use the existing trigram index. The threshold is 0.4. The earlier, stricter check still follows it, so the matches stay the same. The pg_trgm documentation describes the operator and the setting. - The EAN has an index too. Search also checks in passing whether the term is an EAN. That branch got an index as well, because a single one without is enough to slow everything down.
- ANALYZE runs at startup. Postgres decides which route is cheaper based on statistics about the table. After an import or a change to the columns those figures are stale, and then it plans on wrong assumptions.
- The word list for suggestions sits in a cache. It used to be recomputed from the catalogue on every request. Now each server process keeps it in memory. The price: new products only show up in the suggestions once the cache expires.
The condition looks almost the same afterwards, but every part has an index it can reach:
-- after, simplified
WHERE suchvektor @@ websearch_to_tsquery('german', $1)
OR bezeichnung ILIKE '%' || $1 || '%'
OR ean = $1
OR ($1 <% bezeichnung AND word_similarity($1, bezeichnung) > $2)
-- once at startup
ALTER TABLE artikel ADD COLUMN suchvektor tsvector
GENERATED ALWAYS AS (to_tsvector('german', suchtext)) STORED;
CREATE INDEX artikel_vektor_idx ON artikel USING gin (suchvektor);
CREATE INDEX artikel_trgm_idx ON artikel USING gin (bezeichnung gin_trgm_ops);
CREATE INDEX artikel_ean_idx ON artikel (ean);
SET pg_trgm.word_similarity_threshold = 0.4;
ANALYZE artikel;
You may notice the ILIKE '%…%' in the second line, which is exactly the pattern that becomes a problem with WordPress further down. Here it is harmless, because the trigram index on the product name serves that pattern too. A normal index could not.
The result, measured with the same 11,250 test products:
| Operation | Before | After |
|---|---|---|
| One search | 2.7 seconds | 0.05 to 0.19 seconds |
| Suggestions while typing | 0.36 seconds | 0.01 seconds |
The cross-check: the same matches
Faster is only better if search finds the same things. A change to search can quietly alter what gets found, and without a comparison you only notice when a customer cannot find a product. So we ran 30 comparison queries before and after the rebuild and compared the matches. They were identical.
Why no search engine of our own
We did not put an additional search engine next to the database but used what Postgres brings along. The database is there anyway, already holds the data, and search does not have to be kept in sync with a second system. The code is cut so that search sits behind a single file. If it ever hits its limits, switching to a search service changes something only there.
The same principle in WordPress and WooCommerce
The default WordPress search looks in title, excerpt and content, using LIKE with a percent sign before and after the term. That is how it is written in WordPress core. Simplified, for the term “potato”:
-- default WordPress search, simplified
WHERE (post_title LIKE '%potato%')
OR (post_excerpt LIKE '%potato%')
OR (post_content LIKE '%potato%')
A wildcard at the start of the pattern cannot use a normal index. The database therefore reads every row and checks it. With a few hundred products you will not notice, with many thousands you will, and it grows with the catalogue. In the default setup this also applies to the product search of a WooCommerce shop, unless a search plugin replaces it: it builds on the search of the WordPress main query.
The difference to Semsters matters. There, the database had a tool that could fix the mistake. With WordPress the answer is not a trigram index: WordPress runs on MySQL or MariaDB, and their tools are different. The principle stays the same, and it fits in one sentence:
Every part of a search condition needs an index, otherwise the slowest part sets the pace.
What you can take away as a WooCommerce owner
- Find out which search you actually use. If you have set up neither a search plugin nor a search service, it is the default search. A search that takes its matches from an index of its own, instead of reading all products on every request, avoids the pattern above.
- Measure with a realistic number of products before a big import. Create as many test products on a copy of your shop as the range will have later, and measure typical search terms. With a catalogue of a few hundred products almost any search forgives almost anything. We only saw it with 11,250 test products.
- Ask the database what it is doing. Put
EXPLAINin front of the query and it shows whether it uses an index or reads the whole table. If MySQL showsALLin thetypefield, or Postgres showsSeq Scan, every row is being read. - Count how often the query runs per page view. A tool that lists all database queries of a page will show you. For us the full-text value was recomputed ten times on every page view, which you do not see in a single query.
- Compare the matches before and after every change. A list of real search terms from your search log is enough, with typos too if you have any. Save the matches beforehand and compare afterwards.
What this test does not show
The test has limits. The numbers come from test products and a local Postgres environment, not from live operation at Semsters, which still runs with sample prices. What is said above about WordPress and WooCommerce is the behaviour of the pattern as it stands in the WordPress source code, not a measurement on a shop of ours. And we did not pit WooCommerce against Semsters in this test, so a comparison of the two routes is not in here.
How to get a slow WordPress site fast again, step by step, is covered in the post Speed up slow WordPress. Search is one of the places that only show up once the catalogue grows.
Is your shop getting slower as the range grows?
Tell us where it hurts. We measure first and then tell you whether it is a case for our speed optimisation in the agency section.
Frequently asked questions
Why did you not build Semsters with WooCommerce?
The client explicitly wanted neither WordPress nor WooCommerce. On top of that came net prices, tiered prices per product, company accounts and a range of around 11,000 products from an Excel spreadsheet. Whether WooCommerce with suitable extensions could have carried that as well, we have not measured. For most shops WooCommerce remains the right choice.
What is the difference between word_similarity() and the <% operator in Postgres?
Both measure how similar a search term is to a word in the text. The <% operator returns true when that value reaches the configured threshold (pg_trgm.word_similarity_threshold). Because it is an operator, a trigram index can serve it. An index cannot serve the function, so the database has to evaluate it for every row.
Why does a single OR slow down the whole search?
With an OR, every part of the condition must be able to return matches from an index on its own. If even one part cannot, the database has no choice but to read every row of the table and check the condition there. With an AND it could take the index of one part and check the rest against the few matches.
Why does WordPress search get slower as the catalogue grows?
The default WordPress search looks in title, excerpt and content with LIKE and a percent sign before and after the search term. A wildcard at the start cannot use a normal index, so the database reads every row. The more entries there are, the longer that takes.
How do I check that search still finds the same things after a change?
Before the change, put together a list of real search terms, ideally from your search log, and save the matches for each query. After the change, run the same list again and compare. We compared 30 queries before and after the change, and the matches were identical.