VALUES clause for inline data in PostgreSQL - Time & Space Complexity
We want to understand how the time it takes to run a VALUES clause grows as we add more rows of inline data.
How does adding more rows affect the work the database does?
Analyze the time complexity of the following code snippet.
VALUES
(1, 'apple'),
(2, 'banana'),
(3, 'cherry'),
(4, 'date'),
(5, 'elderberry');
This code creates a small inline table with 5 rows of data using the VALUES clause.
- Primary operation: The database reads each row of inline data one by one.
- How many times: Once for each row listed in the VALUES clause.
As you add more rows to the VALUES clause, the database does more work reading each row.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Reads 10 rows |
| 100 | Reads 100 rows |
| 1000 | Reads 1000 rows |
Pattern observation: The work grows directly with the number of rows added.
Time Complexity: O(n)
This means the time to process the VALUES clause grows in a straight line as you add more rows.
[X] Wrong: "Adding more rows to VALUES doesn't affect performance much because it's just inline data."
[OK] Correct: Even inline data must be read row by row, so more rows mean more work for the database.
Understanding how inline data scales helps you explain query costs clearly and shows you know how databases handle data internally.
"What if we replaced the VALUES clause with a SELECT from a large table? How would the time complexity change?"