You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

0 Días
6 Hrs
7 Min
19 Seg
Curso de SQL y MySQL

Curso de SQL y MySQL

Genaro Bernardino

Genaro Bernardino

Búsquedas Avanzadas con JSON en MySQL: Indexación y Optimización

12/19
Resources

JSON data manipulation in MySQL represents a powerful tool for managing semi-structured information within a relational database system. Mastering these techniques will allow you to combine the best of both worlds: the consistency and security of relational databases with the flexibility of JSON documents.

How to search for data in JSON columns?

When working with JSON columns in MySQL, one of the most common operations is searching for specific information within these documents. To illustrate this process, we can generate random data in our product table:

UPDATE productSET data = '{"brand": "per", "warranty": false, "hd_size": "30GB"}'WHERE rand() < 0.4;

This command updates approximately 40% of our records with a JSON object containing brand, warranty and hard disk size information. We can check how many records have JSON data:

SELECT COUNT(*) FROM products WHERE data IS NOT NULL;-- Result: 1512 records with JSON data-- 846 records without JSON data.

This is precisely the advantage of JSON: some records can contain certain information while others do not, without the need to create additional columns that would be empty in many cases. It is ideal for optional data or structures that may vary between records.

Extracting JSON data using different methods

To find all tuples that have the "per" flag, we can use several techniques:

  1. Using the JSON_EXTRACT function:
SELECT * FROM productsWHERE JSON_EXTRACT(data, '$.brand') = 'per'LIMIT 10;
  1. Using arrow notation (->):
SELECT data->'$.brand' FROM productsLIMIT 10;

This notation is equivalent to JSON_EXTRACT but more concise. The result will be a JSON value (with quotes).

  1. To get the value as a MySQL native type, we use the double arrow (->>) which removes the quotes:
SELECT data->>'$.brand' FROM productsLIMIT 10;

It is important to understand the difference: with the single arrow (->) we get a JSON value that still keeps its formatting, while with the double arrow (->>) we get a MySQL native value with which we can operate directly.

Working with nested JSON objects

When we have nested JSON objects, the extraction becomes more complex. For example, if we modify a record to have a nested object:

UPDATE productSET data = '{"brand": {"founder": "so-and-so", "year": 1950}, "warranty": false, "hd_size": "30GB"}'WHERE product_id = 12;

To access the nested data, we must specify the full path:

SELECT data->>'$.brand.founder' FROM productsWHERE product_id = 12;

In current versions of MySQL (family 8.0+), we must use the full path with a single arrow operation, as it does not allow chaining operations as in previous versions.

How to optimize searches in JSON columns?

JSON columns do not allow indexes directly due to their variable nature. However, we can implement an optimization strategy by creating generated columns based on specific JSON values:

ALTER TABLE productADD COLUMN json_brand VARCHAR(30)AS (data->>'$.brand');

This statement creates a virtual column that automatically extracts the brand value from the JSON. To handle null values, we can use the IFNULL function:

ALTER TABLE productADD COLUMN json_brand VARCHAR(30)AS (IFNULL(data->>'$.brand', 'empty'));

Once this column is created, we can index it to speed up searches:

CREATE INDEX json_brand_index ON products(json_brand);

Now queries that filter by brand will be much more efficient:

SELECT * FROM productWHERE json_brand = 'banana';

This technique is especially valuable when working with large volumes of data or in high workload environments, where optimization can make the difference between a smooth experience and a frustrating one for the user.

Benefits of this strategy

  1. Improved performance: Searches are performed on a conventional index rather than scanning and parsing JSON documents.
  2. Maintained flexibility: We continue to leverage the flexibility of JSON to store variable data.
  3. Improved user experience: Faster response times, especially on systems with high concurrency.

Using JSON in MySQL allows us to combine the structure and consistency of relational databases with the flexibility of documents. By using the right extraction and optimization techniques, we can take full advantage of this powerful feature without sacrificing performance.

Have you implemented JSON columns in your projects? What optimization strategies have you found most effective? Share your experience in the comments.

Contributions 0

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?