Search configuration and languages in PostgreSQL - Time & Space Complexity
When using search configurations and languages in PostgreSQL, it is important to understand how the search process scales as the amount of data grows.
We want to know how the time to find matches changes when we have more text or more documents to search.
Analyze the time complexity of the following PostgreSQL full-text search query using a specific search configuration.
SELECT *
FROM documents
WHERE to_tsvector('english', content) @@ to_tsquery('english', 'database & search');
This query searches the "content" column of the "documents" table for entries matching the words "database" and "search" using the English search configuration.
Look for repeated work done by the query.
- Primary operation: The database processes each document's text to create a searchable vector and then compares it to the search query.
- How many times: This happens once for each document in the table.
As the number of documents grows, the search work grows roughly in proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 vector creations and comparisons |
| 100 | About 100 vector creations and comparisons |
| 1000 | About 1000 vector creations and comparisons |
Pattern observation: The work grows linearly with the number of documents because each one is checked.
Time Complexity: O(n)
This means the time to complete the search grows directly in proportion to the number of documents being searched.
[X] Wrong: "Using a search configuration makes the search instant no matter how many documents there are."
[OK] Correct: The search configuration helps interpret the text but does not reduce the number of documents to check unless indexes are used.
Understanding how search configurations affect query time helps you explain how databases handle text search efficiently and how to scale searches as data grows.
"What if we add a full-text index on the content column? How would the time complexity change?"