Lesson 2 of the SQL Optimization Course: The Art of MySQL Data Types:Part 2

Table of Contents
Lesson 2 of the SQL Optimization Course: The Art of MySQL Data Types - Large Object Field
Introduction
For relational databases, the design of tables and SQL is written are particularly crucial. It wouldn’t be an exaggeration to say that they account for 90% of performance. So this time, specifically targeting these two major knowledge areas, we’ll conduct a detailed analysis for you, peeling back the layers.
This Series uses plain and understandable language and selects a large number of examples to elaborate on the subtleties for you.
🧑💻 Target audience:
- DBA
- Database developers
- Students
We will use MySQL as the demonstration database.
Lesson 2: The Art of MySQL Data Types - Large Object Field
We will explain MySQL’s large object types in two parts: TEXT/BLOB and JSON.
1. TEXT/BLOB Types
The difference between TEXT and BLOB is very simple. TEXT stores data in plain text with corresponding character sets and collation rules; BLOB stores data in binary format without character sets and sorting rules, and all comparisons are performed in binary.
Example 1
Create a table named c1
with fields f1
and f2
of types tinytext
and tinyblob
respectively.
|
|
Insert sample data.
|
|
Sort by field f1
.
|
|
Sort by field f2
.
|
|
The sorting results of fields f1
and f2
are inconsistent. f1
uses a case - insensitive collation rule, while f2
uses direct binary checking.
I will explain MySQL’s large object types in detail from the following categories:
1.1 Disk Space Occupancy
Type | Maximum Data Occupancy | Description |
---|---|---|
tinytext/tinyblob | 255B | An additional 1B is used to store the total number of bytes |
text/blob | 65KB | An additional 2B is used to store the total number of bytes |
mediumtext/mediumblob | 16MB | An additional 3B is used to store the total number of bytes |
longtext/longblob | 4GB | An additional 4B is used to store the total number of bytes |
1.2 Table Storage Formats
1.2.1 redundant/compact
For the redundant
format, the first 768 bytes of a large object are stored in the InnoDB data page, and the remaining data is stored in overflow pages. If there are multiple TEXT/BLOB fields, the data page will become extremely bloated, significantly affecting performance. The data page will be filled with almost useless data, resulting in additional resource consumption. At the same time, in a master - slave architecture, all data will be synchronized to the slave, causing additional network consumption. Therefore, in this scenario, generally only the path of the large object is saved in the database, and the actual data is stored on the disk.
1.2.2 dynamic/compressed
For the dynamic
format, if the data size stored in a large object field is less than 40 bytes, all data is stored in the data page. In other scenarios, the data page only retains a 20 - byte pointer pointing to the overflow page. In this scenario, if the data stored in each large object field is less than 40 bytes, it has the same effect as varchar(40)
. Therefore, whether to use large object fields cannot be generalized.
1.3 Table Engine - related
It is not recommended to use large objects in the NDB engine. There are many details involved. For example, associated fields containing large objects do not support engine - related data pushdown; tables containing large objects require additional locking, etc.
1.4 Index - related
When creating an index on a large object field, it must be a prefix index. For example, if field f1
is of type text
, create an index idx_f1(f1(10))
for the first 10 characters.
|
|
1.5 Partitioned Table - related
Partition fields in partitioned tables do not support large objects.
|
|
1.6 Parameter - related
mysql_allowed_packet
: This parameter represents the upper limit of a single data packet transmitted between the MySQL server and the client. If there are text/blob
fields, this parameter should be set to the maximum value of 1GB. Of course, both the client and the server must be set simultaneously.
1.7 How to Insert/Retrieve
Example 2
Insert the contents of all files in the /var/lib/mysql-files
directory.
|
|
The table structure for storing file contents.
|
|
Insert file contents.
|
|
2. JSON Types
Most people are very familiar with the JSON type. It is a lightweight text - based interaction format that does not depend on any statements. For reference information, please see:
The JSON type is stored in a special binary format inside MySQL, similar to the JSONB type in PostgreSQL. The maximum space it occupies is the same as that of longtext
or longblob
. Although text
can also store JSON objects, it does not have the format validation of the JSON type or the many functions provided internally. For example:
Example 3
Variables @a
and @b
are in standard JSON format and non - JSON format respectively. Create the json1
table.
|
|
Inserting @a
into str1
succeeds, while inserting @b
fails; any characters can be inserted into str2
.
|
|
Another example is the retrieval part. Retrieving data from JSON is much easier than from text. Using the previous example, if you need to retrieve the value 1
corresponding to a
, it may be more difficult for @b
, but much easier for @a
. You can use the json_extract
function.
|
|
Fortunately, for a format like @b
, a single field can be split into multiple fields, so that the result can be easily retrieved using SQL statements. The advantages and disadvantages of splitting or not splitting are not discussed in this article and will be introduced in detail in the article on table design concepts.
Well, this is the end of the introduction to MySQL’s large object types. I hope it will be helpful to you.
👋 See you in the next lession.
Recommended reading
The SQL Optimization Course
- Lesson 1 of the SQL Optimization Course: The Art of MySQL Data Types:Part 1
- Lesson 2 of the SQL Optimization Course: The Art of MySQL Data Types:Part 2
- Lesson 3 of the SQL Optimization Course: Column Not Null & Auto Increment
- Lesson 4 of the SQL Optimization Course: Column - level CHECK Constraint
- Lesson 5 of the SQL Optimization Course: Can foreign keys be used?
The SQL optimization knowledge
👋 See you in the next lesson.
What is SQLFlash?
SQLFlash is your AI-powered SQL Optimization Partner.
Based on AI models, we accurately identify SQL performance bottlenecks and optimize query performance, freeing you from the cumbersome SQL tuning process so you can fully focus on developing and implementing business logic.