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?"
Practice
GRANT SELECT ON database.* TO 'user'@'host'; command do in MySQL?Solution
Step 1: Understand the GRANT command
The GRANT command is used to give specific permissions to a user on a database or table.Step 2: Analyze the SELECT privilege
SELECT privilege allows reading data but not modifying it.Final Answer:
Allows the user to read data from all tables in the specified database. -> Option AQuick Check:
GRANT SELECT = read permission [OK]
- Confusing SELECT with UPDATE or DELETE privileges
- Thinking GRANT creates a user
- Assuming it deletes data
shop database to user 'alice' connecting from any host?Solution
Step 1: Check the database and user format
The database must be followed by .* to specify all tables, and user must include host.Step 2: Verify the correct order of clauses
The syntax is: GRANT privileges ON database.* TO 'user'@'host';Final Answer:
GRANT ALL PRIVILEGES ON shop.* TO 'alice'@'%'; -> Option AQuick Check:
Correct syntax = GRANT ALL PRIVILEGES ON shop.* TO 'alice'@'%'; [OK]
- Omitting .* after database name
- Placing TO clause incorrectly
- Not specifying host with user
GRANT SELECT, INSERT ON sales.* TO 'bob'@'localhost'; SHOW GRANTS FOR 'bob'@'localhost';
What will the
SHOW GRANTS output include?Solution
Step 1: Understand the GRANT command given
The user 'bob' is granted SELECT and INSERT privileges on all tables in the sales database.Step 2: Interpret SHOW GRANTS output
SHOW GRANTS displays the exact privileges granted, so it will list SELECT and INSERT on sales.* for bob.Final Answer:
GRANT SELECT, INSERT ON sales.* TO 'bob'@'localhost' -> Option CQuick Check:
SHOW GRANTS matches granted privileges [OK]
- Assuming ALL PRIVILEGES are granted
- Ignoring INSERT privilege
- Thinking SHOW GRANTS returns no rows
GRANT UPDATE, DELETE ON * TO 'carol'@'localhost';
Solution
Step 1: Check the ON clause syntax
The ON clause requires a database or table name, e.g., database.* or database.table.Step 2: Identify the missing part
The command uses * alone, which is invalid syntax in MySQL for ON.Final Answer:
Missing database or table name after ON -> Option DQuick Check:
ON must specify database or table [OK]
- Using * alone without database name
- Using wrong quotes for user
- Assuming localhost is invalid host
'dave' to create new tables only in the inventory database but not modify data. Which GRANT command is correct?Solution
Step 1: Identify the needed privilege
To create tables, the CREATE privilege is required; INSERT or ALL PRIVILEGES would allow data changes.Step 2: Scope the privilege to the inventory database
Granting on inventory.* limits the permission to that database only.Step 3: Check user and host format
User 'dave'@'localhost' is correctly specified.Final Answer:
GRANT CREATE ON inventory.* TO 'dave'@'localhost'; -> Option BQuick Check:
CREATE only on inventory = GRANT CREATE ON inventory.* TO 'dave'@'localhost'; [OK]
- Granting ALL PRIVILEGES instead of just CREATE
- Including INSERT which allows data changes
- Granting on *.* instead of specific database
Ready to prove it?
Pass the test and this pattern moves unknown
