Decimal and floating-point types in MySQL - Time & Space Complexity
When working with decimal and floating-point types in MySQL, it is important to understand how operations on these types scale as data grows.
We want to know how the time to process these numbers changes when we handle more rows or bigger numbers.
Analyze the time complexity of the following SQL query using decimal and floating-point columns.
SELECT AVG(price) AS avg_price
FROM products
WHERE price > 100.00;
This query calculates the average price of products where the price is greater than 100.00, using decimal or floating-point values.
Look for repeated steps in the query execution.
- Primary operation: Scanning each row in the products table to check the price condition and include it in the average calculation.
- How many times: Once for every row in the table.
As the number of rows increases, the database must check more prices and add more values to the average.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks and additions |
| 100 | 100 checks and additions |
| 1000 | 1000 checks and additions |
Pattern observation: The work grows directly with the number of rows; doubling rows doubles the work.
Time Complexity: O(n)
This means the time to compute the average grows in a straight line as the number of rows increases.
[X] Wrong: "Calculations on decimal or floating-point types take longer time per row, so the overall time grows faster than the number of rows."
[OK] Correct: Each calculation takes a small, fixed amount of time, so the total time still grows linearly with the number of rows, not faster.
Understanding how numeric data types affect query time helps you explain performance clearly and shows you know how databases handle data at scale.
What if we added an index on the price column? How would the time complexity change?
Practice
Solution
Step 1: Understand the purpose of DECIMAL
DECIMAL stores exact decimal numbers with fixed digits, ideal for money.Step 2: Compare with FLOAT and DOUBLE
FLOAT and DOUBLE store approximate values, which can cause rounding errors.Final Answer:
DECIMAL -> Option CQuick Check:
Exact decimal storage = DECIMAL [OK]
- Choosing FLOAT or DOUBLE for money causing rounding errors
- Using VARCHAR which stores text, not numbers
Solution
Step 1: Recall DECIMAL syntax
DECIMAL(M,D) means M total digits, D digits after decimal point.Step 2: Match given digits
We want 8 total digits and 2 after decimal, so DECIMAL(8,2) is correct.Final Answer:
DECIMAL(8,2) -> Option AQuick Check:
DECIMAL(total, decimals) = DECIMAL(8,2) [OK]
- Swapping M and D values
- Using DECIMAL(8) which lacks decimal precision
prices with a column cost FLOAT(7,2), what will be the result of inserting 12345.6789 and then selecting cost?Solution
Step 1: Understand FLOAT(7,2) meaning
FLOAT(7,2) means 7 digits total, 2 after decimal, but FLOAT is approximate.Step 2: Insert and round value
12345.6789 rounds to 12345.68 when stored with 2 decimals.Final Answer:
12345.68 -> Option BQuick Check:
FLOAT rounds to 2 decimals = 12345.68 [OK]
- Expecting exact storage like DECIMAL
- Ignoring rounding behavior of FLOAT
FLOAT(5,2) but notice values like 1234.567 are stored as 1234.56. What is the likely cause and how to fix it?Solution
Step 1: Recognize FLOAT behavior
FLOAT stores approximate values and rounds to specified decimals, causing truncation.Step 2: Choose correct type for exact decimals
DECIMAL stores exact decimals and preserves precision, so use DECIMAL(7,3).Final Answer:
FLOAT stores approximate values; use DECIMAL(7,3) for exact decimals -> Option DQuick Check:
Approximate FLOAT -> switch to DECIMAL for precision [OK]
- Thinking FLOAT(5,2) is invalid syntax
- Assuming DOUBLE fixes decimal truncation
- Expecting MySQL to round automatically without changing type
Solution
Step 1: Understand scientific measurement needs
Scientific data often requires wide range and floating precision, not exact fixed decimals.Step 2: Choose FLOAT or DOUBLE for wide range
FLOAT and DOUBLE support very large/small values with approximate precision, ideal for science.Final Answer:
FLOAT or DOUBLE, because they store approximate floating-point numbers with wide range -> Option AQuick Check:
Wide range + approximate decimals = FLOAT/DOUBLE [OK]
- Choosing DECIMAL which limits range
- Using VARCHAR which wastes space and loses numeric features
- Using INT which cannot store decimals
Ready to prove it?
Pass the test and this pattern moves unknown
