Why table design affects performance in MySQL - Performance Analysis
Good table design helps databases work faster. We want to see how design choices change the work needed when data grows.
How does the way we build tables affect the time to run queries?
Analyze the time complexity of this simple query on two different table designs.
-- Query to find a user by email
SELECT * FROM users WHERE email = 'example@example.com';
-- Table design 1: email is not indexed
-- Table design 2: email has an index
This query looks for a user by email. The difference is if the email column has an index or not.
Look at what repeats when searching for the email.
- Primary operation: Checking rows to find matching email.
- How many times: Without index, it checks every row one by one. With index, it uses a fast lookup.
As the number of rows grows, the work changes differently depending on design.
| Input Size (n) | Approx. Operations Without Index | Approx. Operations With Index |
|---|---|---|
| 10 | 10 checks | About 4 checks |
| 100 | 100 checks | About 7 checks |
| 1000 | 1000 checks | About 10 checks |
Pattern observation: Without index, checks grow directly with rows. With index, checks grow slowly even if rows grow a lot.
Time Complexity: O(n) without index, O(log n) with index
This means searching without an index takes longer as the table grows, but with an index it stays much faster.
[X] Wrong: "Adding more columns to a table always slows down queries equally."
[OK] Correct: Only columns used in search or joins affect query speed much. Extra columns stored don't slow down searches if not used.
Understanding how table design affects query speed shows you know how databases handle data. This skill helps you write faster queries and design better systems.
"What if we added a composite index on email and username? How would the time complexity change when searching by email only?"
Practice
Solution
Step 1: Understand data types and storage
Choosing the right data type means the database uses only the needed space for each value.Step 2: Impact on speed
Smaller data sizes mean faster reading and writing, improving performance.Final Answer:
It reduces the amount of storage space and speeds up data retrieval. -> Option AQuick Check:
Proper data types = less space + faster queries [OK]
- Thinking data types only affect appearance
- Believing data types control user connections
- Confusing data types with backup features
Solution
Step 1: Review MySQL primary key syntax
The correct syntax places PRIMARY KEY immediately after the column type.Step 2: Check each option
CREATE TABLE users (id INT PRIMARY KEY); uses 'id INT PRIMARY KEY' which is valid syntax.Final Answer:
CREATE TABLE users (id INT PRIMARY KEY); -> Option AQuick Check:
Primary key syntax = 'column type PRIMARY KEY' [OK]
- Separating PRIMARY and KEY keywords incorrectly
- Placing KEY before PRIMARY
- Using comma incorrectly between keywords
products(id INT PRIMARY KEY, name VARCHAR(50), price DECIMAL(5,2)), what will be the result of this query?SELECT * FROM products WHERE price < 10.00;Solution
Step 1: Understand the table schema
The price column is DECIMAL(5,2), a numeric type suitable for decimal comparisons.Step 2: Analyze the query condition
The query selects rows where price is less than 10.00, which is valid and will return matching rows.Final Answer:
All products with price less than 10.00 will be listed. -> Option CQuick Check:
DECIMAL supports numeric comparisons correctly [OK]
- Assuming DECIMAL is text type
- Thinking decimal comparisons cause syntax errors
- Confusing less than with equal to
Solution
Step 1: Identify the cause of slow search
Without indexes, MySQL must scan all rows, causing slow searches.Step 2: Apply indexing to improve speed
Adding an index on the searched column lets MySQL find rows faster.Final Answer:
Add an index on the column used in the search. -> Option DQuick Check:
Indexes speed up searches [OK]
- Changing data type without indexing
- Deleting data instead of optimizing
- Using SELECT * slows queries, but not main cause
Solution
Step 1: Identify the problem with duplicates
Duplicates cause extra storage and slow queries because more rows must be scanned.Step 2: Use UNIQUE constraint to fix duplicates
Adding UNIQUE prevents duplicates and cleaning data reduces size and speeds queries.Final Answer:
Add a UNIQUE constraint to prevent duplicates and clean existing data. -> Option BQuick Check:
Unique constraints remove duplicates and improve performance [OK]
- Thinking bigger columns improve speed
- Removing indexes slows queries
- Using TEXT instead of VARCHAR wastes space
Ready to prove it?
Pass the test and this pattern moves unknown
