Creating views in MySQL - Performance & Efficiency
When we create a view in MySQL, it acts like a saved query. Understanding how long it takes to run this query matters.
We want to know how the time to get results grows as the data grows.
Analyze the time complexity of the following code snippet.
CREATE VIEW recent_orders AS
SELECT order_id, customer_id, order_date
FROM orders
WHERE order_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY);
This code creates a view showing orders from the last 30 days.
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 for each row in the orders table when the view is queried.
As the number of orders grows, the database checks more rows to find recent ones.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 row checks |
| 100 | About 100 row checks |
| 1000 | About 1000 row checks |
Pattern observation: The work grows roughly in direct proportion to the number of rows.
Time Complexity: O(n)
This means the time to get results grows linearly with the number of rows in the orders table.
[X] Wrong: "Creating a view makes the query run faster because it is saved."
[OK] Correct: The view just stores the query, but the database still runs it each time you ask for data, so the time depends on the data size.
Knowing how views work helps you explain query performance clearly and shows you understand how databases handle saved queries.
"What if we add an index on the order_date column? How would the time complexity change?"
Practice
Solution
Step 1: Understand what a view is
A view is a virtual table that stores a SELECT query, not data itself.Step 2: Identify the purpose of views
Views simplify complex queries and allow reuse without storing data physically.Final Answer:
To save a SELECT query as a virtual table for easy reuse -> Option BQuick Check:
Views = virtual tables for queries [OK]
- Thinking views store data physically
- Confusing views with backups
- Assuming views delete data
employee_view showing id and name from employees table?Solution
Step 1: Recall the correct CREATE VIEW syntax
The syntax is: CREATE VIEW view_name AS SELECT ...Step 2: Match the syntax with options
CREATE VIEW employee_view AS SELECT id, name FROM employees; matches the correct syntax exactly.Final Answer:
CREATE VIEW employee_view AS SELECT id, name FROM employees; -> Option CQuick Check:
CREATE VIEW ... AS SELECT ... [OK]
- Swapping keywords CREATE and VIEW
- Omitting AS keyword
- Incorrect keyword order
products with columns id, name, and price, what will be the output of SELECT * FROM cheap_products; if cheap_products is created as CREATE VIEW cheap_products AS SELECT id, name FROM products WHERE price < 50;?Solution
Step 1: Analyze the view definition
The view selects only id and name columns where price is less than 50.Step 2: Understand the SELECT * from the view
Since the view only has id and name, SELECT * returns only these columns for matching rows.Final Answer:
Only id and name columns of products with price less than 50 -> Option AQuick Check:
View columns = selected columns only [OK]
- Expecting all original table columns
- Confusing filter condition with columns selected
- Assuming syntax error if columns omitted
CREATE VIEW sales_view AS SELECT order_id, total FROM orders WHERE;Solution
Step 1: Check the syntax after WHERE
The WHERE clause must have a condition; here it is empty.Step 2: Confirm other parts are correct
AS keyword is present, view name is valid, SELECT is correct for views.Final Answer:
Missing condition after WHERE clause -> Option DQuick Check:
WHERE requires a condition [OK]
- Leaving WHERE empty
- Confusing AS placement
- Using wrong SQL command for views
active_customers that shows customer id, name, and their total orders from customers and orders tables. Which query correctly creates this view?Solution
Step 1: Understand the requirement
The view must show customer id, name, and total orders count, including customers with zero orders.Step 2: Analyze each option
CREATE VIEW active_customers AS SELECT c.id, c.name, COUNT(o.order_id) AS total_orders FROM customers c LEFT JOIN orders o ON c.id = o.customer_id GROUP BY c.id, c.name; uses LEFT JOIN to include all customers and counts orders grouped by customer, which is correct.
CREATE VIEW active_customers AS SELECT id, name, total_orders FROM customers; misses orders data.
CREATE VIEW active_customers AS SELECT c.id, c.name, SUM(o.price) FROM customers c JOIN orders o ON c.id = o.customer_id; sums price but misses GROUP BY and alias.
CREATE VIEW active_customers AS SELECT c.id, c.name, COUNT(o.order_id) FROM customers c, orders o WHERE c.id = o.customer_id; uses implicit join without GROUP BY, causing incorrect aggregation.Final Answer:
CREATE VIEW active_customers AS SELECT c.id, c.name, COUNT(o.order_id) AS total_orders FROM customers c LEFT JOIN orders o ON c.id = o.customer_id GROUP BY c.id, c.name; -> Option AQuick Check:
LEFT JOIN + GROUP BY + COUNT = correct aggregation [OK]
- Forgetting GROUP BY with aggregation
- Using INNER JOIN and missing customers without orders
- Not aliasing aggregated columns
Ready to prove it?
Pass the test and this pattern moves unknown
