BLOB and binary types in MySQL - Time & Space Complexity
When working with BLOB and binary types in MySQL, it's important to understand how the time to read or write data grows as the data size increases.
We want to know how the operations scale when handling larger binary data.
Analyze the time complexity of inserting and retrieving binary data using BLOB types.
INSERT INTO images (id, data) VALUES (1, LOAD_FILE('photo.jpg'));
SELECT data FROM images WHERE id = 1;
This code inserts a binary file into a BLOB column and then retrieves it by id.
Look for repeated work inside these operations.
- Primary operation: Reading or writing the binary data byte by byte or in chunks.
- How many times: The operation repeats once for each byte of the binary data.
The time to insert or retrieve grows as the size of the binary data grows.
| Input Size (bytes) | Approx. Operations |
|---|---|
| 10 KB | 10,240 operations |
| 100 KB | 102,400 operations |
| 1 MB | 1,048,576 operations |
Pattern observation: The operations increase directly with the size of the data.
Time Complexity: O(n)
This means the time grows linearly with the size of the binary data being handled.
[X] Wrong: "Reading or writing BLOB data takes the same time no matter how big the file is."
[OK] Correct: The time depends on the size of the data because each byte must be processed, so bigger files take longer.
Understanding how binary data operations scale helps you explain performance in real applications that store images, videos, or files in databases.
What if we changed from BLOB to storing file paths as text? How would the time complexity of retrieval change?
Practice
Solution
Step 1: Understand data types for binary storage
BLOB is designed to store large binary data such as images or videos.Step 2: Compare with other types
VARCHAR and TEXT store text data, INT stores numbers, so they are not suitable for binary files.Final Answer:
BLOB -> Option AQuick Check:
Large binary data = BLOB [OK]
- Choosing VARCHAR or TEXT for binary data
- Using INT for storing files
- Confusing BLOB with text types
Solution
Step 1: Identify fixed-length binary type
BINARY(n) stores fixed-length binary data of n bytes.Step 2: Check other options
VARBINARY is variable-length, BLOB and TEXT are for large data, not fixed-length binary.Final Answer:
BINARY(16) -> Option CQuick Check:
Fixed-length binary = BINARY(n) [OK]
- Using VARBINARY for fixed-length data
- Trying to specify size with BLOB or TEXT
- Confusing TEXT with binary types
files(id INT, data BLOB), what will the following query return?SELECT LENGTH(data) FROM files WHERE id = 1;
Solution
Step 1: Understand LENGTH() on BLOB
LENGTH() returns the number of bytes in the binary data stored in a BLOB column.Step 2: Analyze the query
The query selects LENGTH(data) for the row where id = 1, so it returns the size in bytes of that binary data.Final Answer:
The size in bytes of the binary data stored in 'data' for id 1 -> Option BQuick Check:
LENGTH(BLOB) = byte size [OK]
- Thinking LENGTH() counts characters in binary
- Expecting an error from LENGTH() on BLOB
- Confusing LENGTH() with SUBSTRING()
CREATE TABLE images (id INT, img BINARY(255));
But when inserting images larger than 100 bytes, data gets truncated. What is the problem?
Solution
Step 1: Understand BINARY(n) behavior
BINARY(n) stores fixed-length binary data of exactly n bytes, padding or truncating as needed.Step 2: Identify truncation cause
If inserted data is larger than 255 bytes, it will be truncated to fit fixed length.Final Answer:
BINARY(255) is fixed-length and pads or truncates data to 255 bytes -> Option DQuick Check:
Fixed-length truncates data > size [OK]
- Thinking BINARY can store variable-length data
- Using VARCHAR for binary data
- Ignoring fixed-length padding/truncation
id INT and picture. You expect pictures up to 2MB. Which data type should you choose for picture and why?Solution
Step 1: Check size requirements
Pictures up to 2MB require a BLOB type that can hold at least 2 million bytes.Step 2: Choose suitable BLOB type
MEDIUMBLOB supports up to 16MB, enough for 2MB pictures. BINARY(2000000) is fixed-length and too large, VARCHAR and TEXT are for text, not binary.Final Answer:
MEDIUMBLOB, because it supports up to 16MB of binary data -> Option AQuick Check:
Large binary data > 64KB = MEDIUMBLOB [OK]
- Using fixed-length BINARY for large data
- Choosing VARCHAR or TEXT for binary files
- Underestimating BLOB size limits
Ready to prove it?
Pass the test and this pattern moves unknown
