Why access control protects data in MySQL - Performance Analysis
We want to understand how the time it takes to check user permissions grows as the number of users and permissions increases.
How does the system handle more users and rules without slowing down too much?
Analyze the time complexity of the following MySQL query that checks user access.
SELECT * FROM permissions p
JOIN user_roles ur ON p.role_id = ur.role_id
WHERE ur.user_id = ? AND p.resource = 'file123';
This query finds all permissions for a user on a specific resource by joining user roles with permissions.
Look at what repeats as data grows.
- Primary operation: Joining the permissions and user_roles tables.
- How many times: The database checks each role linked to the user and matches permissions for that role.
As the number of roles per user and permissions per role grow, the work to find access grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 roles | About 10 permission checks |
| 100 roles | About 100 permission checks |
| 1000 roles | About 1000 permission checks |
Pattern observation: The work grows roughly in direct proportion to the number of roles and permissions checked.
Time Complexity: O(n)
This means the time to check access grows linearly with the number of roles and permissions involved.
[X] Wrong: "Checking access is always instant no matter how many roles or permissions exist."
[OK] Correct: More roles and permissions mean more checks, so the time grows with data size.
Understanding how access control queries scale helps you design systems that stay fast as users and permissions grow.
"What if we added caching for user permissions? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of access control
Access control restricts database access to authorized users only.Step 2: Identify the benefit of restricting access
By limiting who can view or modify data, it protects data from unauthorized changes or leaks.Final Answer:
It limits who can view or change data, protecting it from unauthorized access. -> Option AQuick Check:
Access control = Protects data [OK]
- Confusing access control with performance features
- Thinking access control handles backups
- Believing access control formats data display
Solution
Step 1: Recall MySQL commands for permissions
GRANT is used to give permissions; REVOKE removes them.Step 2: Identify the command that grants access
GRANT assigns privileges to users to access or modify data.Final Answer:
GRANT -> Option CQuick Check:
GRANT = Give permission [OK]
- Confusing GRANT with REVOKE
- Choosing data manipulation commands like SELECT or DELETE
- Thinking SELECT grants permissions
GRANT SELECT ON employees TO 'user1'@'localhost';
REVOKE INSERT ON employees FROM 'user1'@'localhost';What can 'user1' do on the
employees table?Solution
Step 1: Analyze the GRANT command
GRANT SELECT allows user1 to read data from employees table.Step 2: Analyze the REVOKE command
REVOKE INSERT removes insert permission, so user1 cannot add data.Final Answer:
Only select (read) data from employees -> Option BQuick Check:
GRANT SELECT + REVOKE INSERT = Read only [OK]
- Assuming REVOKE removes all permissions
- Thinking user1 can insert data
- Confusing SELECT with INSERT permission
GRANT SELECT ON sales TO 'user2'@'%';
REVOKE SELECT ON sales FROM 'user2'@'%';But user2 can still read the sales table. What is the likely problem?
Solution
Step 1: Understand permission scope in MySQL
Permissions are granted per user and host combination.Step 2: Identify why REVOKE may not work as expected
If user2 has another entry with SELECT permission from a different host, that permission remains.Final Answer:
User2 has SELECT permission from another host or user entry. -> Option AQuick Check:
Multiple user entries can override REVOKE [OK]
- Assuming REVOKE syntax is wrong without checking user entries
- Believing REVOKE is unsupported for SELECT
- Thinking a database restart is needed for permission changes
Solution
Step 1: Identify the need for restricted update access
Only HR should modify salaries to protect sensitive data.Step 2: Apply access control best practice
Grant UPDATE permission only to HR users and revoke from others to enforce this.Final Answer:
Grant UPDATE permission on salaries table only to HR users, revoke from others. -> Option DQuick Check:
Restrict update rights to protect sensitive data [OK]
- Giving update rights to all users
- Relying only on application-level checks
- Ignoring access control for sensitive tables
Ready to prove it?
Pass the test and this pattern moves unknown
