Why views are needed in SQL - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how using views affects the time it takes to get data from a database.
Specifically, we ask: How does the work grow when we use views compared to direct queries?
Analyze the time complexity of this SQL view and query.
CREATE VIEW RecentOrders AS
SELECT OrderID, CustomerID, OrderDate
FROM Orders
WHERE OrderDate >= DATE_SUB(CURDATE(), INTERVAL 30 DAY);
SELECT * FROM RecentOrders WHERE CustomerID = 123;
This code creates a view showing orders from the last 30 days, then selects orders for one customer.
Look at what repeats when running the query.
- Primary operation: Scanning the Orders table to find recent orders.
- How many times: Once per query execution, filtering all rows from the last 30 days.
As the number of orders grows, the work to find recent orders grows too.
| Input Size (Orders in 30 days) | Approx. Operations |
|---|---|
| 10 | 10 rows checked |
| 100 | 100 rows checked |
| 1000 | 1000 rows checked |
Pattern observation: The work grows roughly in direct proportion to the number of recent orders.
Time Complexity: O(n)
This means the time to get results grows linearly with the number of recent orders.
[X] Wrong: "Using a view makes queries instant because it stores results like a table."
[OK] Correct: Views usually run the underlying query each time, so the work depends on the data size, not a stored snapshot.
Understanding how views work helps you explain query performance clearly and shows you know how databases handle data behind the scenes.
"What if the view was indexed or materialized? How would that change the time complexity?"
Practice
SELECT * FROM view_name; What is the main purpose of this?Solution
Step 1: Understand what a view does
A view is a saved query that shows data in a simpler way without storing data separately.Step 2: Identify the main purpose of views
Views help users see only the data they need, hiding complexity and sensitive info.Final Answer:
To simplify complex queries by showing only needed data -> Option DQuick Check:
Views simplify data = C [OK]
- Thinking views store data physically
- Confusing views with database hardware
- Believing views delete data
EmployeeView that shows only name and salary from Employees table?Solution
Step 1: Recall the syntax for creating a view
The correct syntax starts with CREATE VIEW, then the view name, followed by AS and the SELECT query.Step 2: Match the options with correct syntax
CREATE VIEW EmployeeView AS SELECT name, salary FROM Employees; matches the correct syntax exactly; others use wrong keywords or order.Final Answer:
CREATE VIEW EmployeeView AS SELECT name, salary FROM Employees; -> Option AQuick Check:
CREATE VIEW + AS + SELECT = B [OK]
- Using CREATE TABLE instead of CREATE VIEW
- Wrong keyword order like INSERT VIEW
- Omitting AS keyword
Sales with columns product, region, and amount, and the view:CREATE VIEW RegionalSales AS SELECT region, SUM(amount) AS total FROM Sales GROUP BY region;What will the query
SELECT * FROM RegionalSales WHERE total > 1000; return?Solution
Step 1: Understand the view definition
The view groups sales by region and sums amounts, creating a total per region.Step 2: Analyze the query on the view
The query filters regions where total sales exceed 1000, so it returns those regions and totals.Final Answer:
Rows showing regions where total sales amount is more than 1000 -> Option BQuick Check:
View groups and filters totals > 1000 = A [OK]
- Thinking views cannot use GROUP BY
- Confusing view columns with base table columns
- Assuming syntax error due to alias
CREATE VIEW MyView AS SELECT id, password FROM Users;But you want to hide the
password column for security. What is the best fix?Solution
Step 1: Identify the problem with the view
The view currently shows the password column, which exposes sensitive data.Step 2: Fix the view to hide sensitive data
Removing the password column from the SELECT statement prevents it from appearing in the view.Final Answer:
Remove password from the SELECT list in the view -> Option AQuick Check:
Exclude sensitive columns from view SELECT = D [OK]
- Trying to rename view to hide data
- Using WHERE clause to filter columns
- Deleting columns from base table instead of view
Orders table has millions of rows. Which approach best uses views to improve performance and security?Solution
Step 1: Understand the goal for the view
The view should show only active customers and their total orders to reduce data size and protect inactive data.Step 2: Choose the best approach for performance and security
Filtering and pre-aggregating in the view reduces data scanned and hides unnecessary info, improving speed and security.Final Answer:
Create a view filtering active customers and pre-aggregating order totals -> Option CQuick Check:
Filter and aggregate in view for speed and security = A [OK]
- Selecting all data without filters
- Trying to delete data via views
- Misunderstanding views store data physically
