0
0
PostgreSQLquery~5 mins

Boolean type behavior in PostgreSQL - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Boolean type behavior
O(n)
Understanding Time Complexity

We want to understand how the time it takes to work with Boolean values changes as we use more data.

Specifically, how does checking or using Boolean values in queries grow when the data grows?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.


SELECT id, is_active
FROM users
WHERE is_active = TRUE;
    

This query selects user IDs where the Boolean column is_active is true.

Identify Repeating Operations

Identify the loops, recursion, array traversals that repeat.

  • Primary operation: Scanning each row in the users table to check the is_active value.
  • How many times: Once for every row in the table.
How Execution Grows With Input

As the number of users grows, the database checks more rows one by one.

Input Size (n)Approx. Operations
1010 checks of is_active
100100 checks of is_active
10001000 checks of is_active

Pattern observation: The number of checks grows directly with the number of rows.

Final Time Complexity

Time Complexity: O(n)

This means the time to run the query grows in a straight line as the table gets bigger.

Common Mistake

[X] Wrong: "Checking a Boolean column is instant no matter how big the table is."

[OK] Correct: Even though Booleans are simple, the database still looks at each row, so more rows mean more work.

Interview Connect

Understanding how simple Boolean checks scale helps you explain query performance clearly and confidently.

Self-Check

"What if the is_active column had an index? How would the time complexity change?"