Granting privileges in MySQL - Time & Space Complexity
When we grant privileges in a database, the system updates access rights for users. Understanding how long this takes helps us manage performance as the database grows.
We want to know how the time to grant privileges changes as the number of users or privileges increases.
Analyze the time complexity of the following MySQL command.
GRANT SELECT, INSERT ON mydb.* TO 'user'@'localhost';
FLUSH PRIVILEGES;
This code grants SELECT and INSERT rights on all tables in the database "mydb" to a specific user, then reloads the privilege tables.
Look for repeated actions inside the grant process.
- Primary operation: Updating a single row in the mysql.db privilege table for the user, host, and database.
- How many times: Once (constant time), regardless of the number of listed privileges or tables in the database.
The time to grant these database-level privileges is constant and does not grow with the number of privileges or database objects affected.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 privileges or objects | 1 update |
| 100 privileges or objects | 1 update |
| 1000 privileges or objects | 1 update |
Pattern observation: The work is constant regardless of the number of privileges or objects involved.
Time Complexity: O(1)
This means the time to grant privileges is constant even as the number of privileges or database objects grows.
[X] Wrong: "Granting privileges on db.* requires an update for every table, so time grows with #tables."
[OK] Correct: MySQL stores database-level privileges in a single row per user per database in mysql.db; it applies to all tables without per-table entries.
Understanding how privilege changes scale helps you explain database performance and security management clearly. This skill shows you think about real system behavior, not just commands.
"What if we grant privileges to multiple users at once? How would the time complexity change?"