Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Because JSON documents contain flexible, arbitrary schema structures, MySQL cannot directly create a standard B+Tree index on an entire JSON column.
If a query filters with WHERE attributes->>'$.brand' = 'Alienware', MySQL must perform a Full Table Scan (type: ALL), inspecting and parsing the JSON of every row.
To index JSON attributes with high-performance B+Tree lookups, MySQL provides Virtual Generated Columns (and Multi-Valued Indexes).
A Generated Column is a column whose value is calculated automatically from an expression:
VIRTUAL (Default): Column values are calculated on-the-fly when read. They consume zero disk space.STORED: Column values are calculated and written to disk when rows are inserted or updated.VIRTUAL generated column consumes zero table disk space, MySQL allows you to create a standard B+Tree index on it! The index physically stores the extracted keys, enabling $O(log N)$ point lookups!Now, when an application queries the table using either the generated column OR the original JSON path syntax:
EXPLAIN Result:type: refkey: idx_products_brandrows: 1The MySQL query optimizer automatically recognizes that attributes->>'$.brand' matches the virtual column expression and transparently redirects execution to the B+Tree index!
If your JSON document contains an array (e.g., tags: ["gaming", "vr", "portable"]), how do you index individual elements?
MySQL 8.0.17+ introduces Multi-Valued Indexes:
This query performs an index seek against idx_json_tags with zero table scans!
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Modifying JSON Documents (JSON_SET, JSON_INSERT, JSON_REMOVE) | Advanced Analytical JSON Queries |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.