Column-level security with masking policies in Snowflake - Time & Space Complexity
We want to understand how the time to apply column-level security with masking policies changes as data grows.
Specifically, how does the system handle masking when many rows are queried?
Analyze the time complexity of applying a masking policy on a column during a SELECT query.
CREATE MASKING POLICY ssn_mask AS
(val STRING) RETURNS STRING ->
CASE
WHEN CURRENT_ROLE() IN ('FULL_ACCESS_ROLE') THEN val
ELSE 'XXX-XX-XXXX'
END;
ALTER TABLE employees
ALTER COLUMN ssn
SET MASKING POLICY ssn_mask;
SELECT ssn FROM employees;
This sequence creates a masking policy, applies it to a column, and then queries that column.
Look at what happens repeatedly when querying masked columns.
- Primary operation: Applying the masking policy logic to each row's column value.
- How many times: Once per row returned by the query.
As the number of rows increases, the masking logic runs for each row.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 masking checks |
| 100 | 100 masking checks |
| 1000 | 1000 masking checks |
Pattern observation: The number of masking operations grows directly with the number of rows.
Time Complexity: O(n)
This means the time to apply masking grows linearly with the number of rows returned.
[X] Wrong: "Masking policies apply once per query, so time stays the same regardless of rows."
[OK] Correct: Masking runs on each row's data, so more rows mean more masking operations.
Understanding how security features scale helps you design efficient data access controls in real projects.
What if the masking policy used a complex function instead of a simple case? How would that affect time complexity?