Revoking privileges in MySQL - Time & Space Complexity
When we revoke privileges in a database, the system checks and updates user permissions.
We want to understand how the time it takes changes as the number of users or privileges grows.
Analyze the time complexity of the following code snippet.
REVOKE SELECT, INSERT ON database_name.* FROM 'user'@'host';
This command removes specific privileges from a user on all tables in a database.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The database system checks the privilege tables to find and update the user's privileges.
- How many times: It scans the privilege entries related to the user and the specified privileges once.
The time to revoke privileges grows with the number of privilege entries the system must check.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 privilege entries | 10 checks |
| 100 privilege entries | 100 checks |
| 1000 privilege entries | 1000 checks |
Pattern observation: The work increases directly with the number of privilege entries to check.
Time Complexity: O(n)
This means the time to revoke privileges grows linearly with the number of privilege entries involved.
[X] Wrong: "Revoking privileges happens instantly no matter how many users or privileges exist."
[OK] Correct: The system must find and update privilege records, so more entries mean more work and more time.
Understanding how privilege changes scale helps you manage database security efficiently and shows you grasp how databases handle user permissions behind the scenes.
"What if we revoke privileges from multiple users at once? How would the time complexity change?"