View limitations in MySQL - Time & Space Complexity
When using views in MySQL, it's important to understand how their performance changes as data grows.
We want to see how the time to get results from a view changes when the underlying data gets bigger.
Analyze the time complexity of the following view query.
CREATE VIEW recent_orders AS
SELECT order_id, customer_id, order_date
FROM orders
WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY);
SELECT * FROM recent_orders;
This view selects orders from the last 30 days and then retrieves all those recent orders.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning the orders table to find rows matching the date condition.
- How many times: Once per query execution, scanning all rows or using an index to filter.
As the orders table grows, the time to filter recent orders grows roughly in proportion to the number of rows scanned.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 rows checked |
| 100 | About 100 rows checked |
| 1000 | About 1000 rows checked |
Pattern observation: The work grows roughly in a straight line as the table gets bigger.
Time Complexity: O(n)
This means the time to get results grows directly with the number of rows in the orders table.
[X] Wrong: "Views store data separately, so querying a view is always fast regardless of table size."
[OK] Correct: Views do not store data themselves; they run the underlying query each time, so performance depends on the base tables.
Understanding how views work and their time cost helps you explain database design choices clearly and confidently.
"What if the view included a join with another large table? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand what a view is and check each option
A view is a virtual table that shows data from one or more base tables but does not store data itself. Views do not store data themselves; they show data from base tables. matches the definition. Options A, C, and D are incorrect because views do not always allow updates, cannot have indexes, and do not store backup data.Final Answer:
Views do not store data themselves; they show data from base tables. -> Option BQuick Check:
Views are virtual tables = B [OK]
- Thinking views store data like tables
- Assuming views always allow updates
- Believing views can have indexes
Solution
Step 1: Recall the syntax for creating a view and compare options
The correct syntax is: CREATE VIEW view_name AS SELECT ... CREATE VIEW view_name AS SELECT * FROM table_name; matches the correct syntax. CREATE TABLE view_name AS SELECT * FROM table_name; creates a table, not a view. CREATE VIEW view_name FROM table_name; has incorrect syntax.Final Answer:
CREATE VIEW view_name AS SELECT * FROM table_name; -> Option DQuick Check:
CREATE VIEW ... AS SELECT ... = D [OK]
- Using CREATE TABLE instead of CREATE VIEW
- Omitting AS keyword
- Incorrect order of clauses
CREATE VIEW v_emp AS SELECT id, name FROM employees;Which of the following queries will return the correct result?
Solution
Step 1: Understand the view and its limitations, then analyze each query
The view selects id and name from employees. Simple views on one table allow SELECT but may restrict UPDATE, INSERT, DELETE. SELECT * FROM v_emp WHERE id = 5; is a SELECT query and will work correctly. Options B, C, and D try to modify data through the view, which is often not allowed or restricted.Final Answer:
SELECT * FROM v_emp WHERE id = 5; -> Option CQuick Check:
Simple views allow SELECT only = A [OK]
- Trying to update or insert through views without triggers
- Assuming all views are updatable
- Ignoring view restrictions on DELETE
CREATE VIEW v_sales AS SELECT order_id, customer_id, total FROM orders WHERE total > 100;When you try to update the view with:
UPDATE v_sales SET total = 90 WHERE order_id = 10;You get an error. What is the likely cause?
Solution
Step 1: Identify view update restrictions and check the error cause
Views with WHERE clauses or filtering conditions are often non-updatable because the database cannot guarantee consistent updates. The view contains a WHERE clause filtering rows, making it non-updatable. explains the error correctly. You cannot update any view in MySQL. is false because some views are updatable. The view does not include the primary key column. is incorrect because order_id is included. The UPDATE syntax is incorrect for views. is wrong as syntax is valid.Final Answer:
The view contains a WHERE clause filtering rows, making it non-updatable. -> Option AQuick Check:
WHERE clause in view blocks updates = A [OK]
- Assuming all views are updatable
- Blaming syntax instead of view definition
- Ignoring WHERE clause effect on updates
salary column of employees but also shows the department name from a joined table. Which approach respects MySQL view limitations?Solution
Step 1: Understand update restrictions on views with joins and choose the approach that allows updates
Views that join multiple tables or use GROUP BY are generally non-updatable in MySQL. Create a simple view on employees only, then update salary directly on employees table. creates a simple view on one table, allowing updates on salary by updating the base table directly. Options A, B, and D involve joins, filters, or grouping, which block updates.Final Answer:
Create a simple view on employees only, then update salary directly on employees table. -> Option AQuick Check:
Simple single-table views allow updates = C [OK]
- Trying to update through joined or grouped views
- Assuming filters don't block updates
- Ignoring base table updates
Ready to prove it?
Pass the test and this pattern moves unknown
