Wednesday, 25 July 2018

MySQL 5.7: How to create an Index on JSON Data Type Column

In the previous two articles, I have shared basic detail about MySQL 5.7 JSON Data Type.
Our next question is, How we can create an Index on JSON Data Type in MySQL 5.7.
As per the official document, actually we cannot directly create an Index on JSON column in MySQL.
We have to add generated column in Table by extracting require JSON Key data.
Once require JSON key data extracted, we can apply index on that column like any other column.
Actually, this is not a good way, but as of now we have only this option to create an indirect Index on JSON Column in MySQL 5.7.
Create sample table using JSON data type:
Insert few sample JSON formatted record:
Create aditional generated column for JSON Field EmployeeDetails->firstName:
Create Index on generated column which indirectly work as Index on JSON EmployeeDetails->firstName:
Check the execution plan of below query, where you can find usage of newly created Index:

0 comments:

Post a Comment