Company
Date Published
Author
Aaron Francis
Word count
1788
Language
English
Hacker News points
25

Summary

MySQL gives us the JSON data type back in mid-2015 with the release of MySQL 5.7.8, allowing us to escape rigid column definitions and store JSON documents of various shapes and sizes. However, direct indexing of JSON columns is not possible in MySQL due to the lack of Generalized Inverted Index (GIN) support. Instead, we can use generated columns or functional indexes to indirectly index parts of our stored JSON documents. Generated columns are calculated columns that can be used like any other column, while functional indexes are implemented using a hidden generated column. To create a functional index on a JSON expression, we need to cast the value to a type that is not LONGTEXT, and explicitly set the collation of the cast to utf8mb4_bin. Functional indexes come with some pitfalls, but can be a powerful tool for indirect indexing of specific keys. While direct JSON indexing may not be available in MySQL, indirect indexing can cover most use cases, and generated columns and functional indexes can be used across various types of common patterns.