Full-text search with Postgres in Supabase - Time & Space Complexity
When using full-text search in Postgres via Supabase, it's important to understand how the search time changes as your data grows.
We want to know how the number of search operations grows when the number of documents increases.
Analyze the time complexity of this full-text search query using Supabase.
const { data, error } = await supabase
.from('articles')
.select('*')
.textSearch('content', 'cloud & infrastructure')
.limit(10)
This query searches the 'content' column in the 'articles' table for the phrase 'cloud & infrastructure' and returns up to 10 matching rows.
Look at what happens when this search runs:
- Primary operation: The database performs an index scan on the full-text index on the 'content' column.
- How many times: Roughly O(log n) index lookups, where n is the number of documents.
As the number of articles grows, the search uses the index to quickly find matches.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 3-4 index lookups |
| 100 | About 7 index lookups |
| 1000 | About 10 index lookups |
Pattern observation: The search time grows roughly logarithmically with the number of documents because the index height grows logarithmically.
Time Complexity: O(log n)
This means the search time grows logarithmically with the number of documents in the table.
[X] Wrong: "Full-text search always runs instantly no matter how much data there is."
[OK] Correct: Even with an index, larger datasets mean larger indexes with greater depth (log n growth) and potentially larger posting lists to process.
Understanding how search time grows helps you design better queries and indexes, a useful skill for real-world cloud database work.
"What if we added a more selective filter before the full-text search? How would that affect the time complexity?"
Practice
to_tsvector in Postgres full-text search?Solution
Step 1: Understand
to_tsvectorfunctionto_tsvectorconverts plain text into a special searchable format called a tsvector, which breaks text into words and normalizes them for searching.Step 2: Identify its role in full-text search
This conversion allows Postgres to quickly find matching words during a search query.Final Answer:
To convert text into a searchable document format -> Option AQuick Check:
to_tsvector= searchable format [OK]
- Confusing to_tsvector with data insertion
- Thinking it deletes text
- Assuming it backs up data
to_tsquery in Postgres?Solution
Step 1: Understand
to_tsqueryusageto_tsquerycreates a search query from a string like 'search & term'. It should be on the right side of the @@ operator.Step 2: Check correct operator usage
The @@ operator checks if the tsvector (usually a column) matches the tsquery. So the column (tsvector) is on the left, andto_tsqueryis on the right.Final Answer:
SELECT * FROM articles WHERE content @@ to_tsquery('search & term'); -> Option DQuick Check:
tsvector @@ tsquery = correct syntax [OK]
- Putting to_tsquery on the left side
- Using = instead of @@ operator
- Passing column name inside to_tsquery
products with a column description, what will this query return?SELECT id FROM products WHERE to_tsvector(description) @@ to_tsquery('red & apple');Solution
Step 1: Analyze the query logic
The query usesto_tsvector(description)to convert descriptions into searchable format, then checks if it matchesto_tsquery('red & apple').Step 2: Understand the & operator in tsquery
The & means AND, so it matches rows containing both 'red' and 'apple' anywhere in the description, not necessarily as a phrase.Final Answer:
All products with descriptions containing both 'red' and 'apple' -> Option AQuick Check:
'red & apple' means both words present [OK]
- Thinking & means OR
- Assuming exact phrase match
- Confusing syntax causing no results
SELECT * FROM feedback WHERE to_tsvector(comment) @@ to_tsquery('happy | sad');What is the most likely reason?
Solution
Step 1: Check query correctness
The syntax and operator usage are correct for full-text search withto_tsvectorandto_tsquery.Step 2: Identify performance and result issues
If no results appear, often thecommentcolumn is not indexed as a tsvector, so the search is slow or ineffective.Final Answer:
Thecommentcolumn is not indexed with a tsvector column -> Option BQuick Check:
Missing tsvector index causes no results [OK]
- Assuming syntax error when query is correct
- Using LIKE instead of full-text search
- Misusing @@ operator
Solution
Step 1: Understand full-text search optimization
Creating a generated column that stores the tsvector form of the text allows fast searching without recalculating on each query.Step 2: Use GIN index for fast lookup
Adding a GIN index on the tsvector column speeds up searches dramatically, making queries efficient and scalable.Step 3: Query with @@ operator for accuracy
Using the @@ operator withto_tsqueryon the indexed tsvector column ensures accurate full-text matching.Final Answer:
Create a generated tsvector column indexed with GIN and query using @@ operator -> Option CQuick Check:
Generated tsvector + GIN index = fast, accurate search [OK]
- Using LIKE instead of full-text search
- Not indexing tsvector columns
- Relying on regex for large text search
Ready to prove it?
Pass the test and this pattern moves unknown
