Sub topic 9
Indexing JSON for Performance
Assume you know basic SELECT, WHERE, and CREATE INDEX statements. You should also know JSON objects, arrays, and scalar values. This chapter uses the orders table with a doc JSON column.
An index is a data structure that helps Oracle find rows without reading every row in a table. JSON indexing works best when the index matches the JSON path and data type used by your queries.
Choose an index for the query pattern
Suppose many queries find an order by its customer name:
SELECT id
FROM orders
WHERE JSON_VALUE(
doc,
'$.customer.name'
RETURNING VARCHAR2(100)
) = 'Ada Lovelace';Create a function-based index for this exact expression:
CREATE INDEX orders_customer_ix
ON orders (
JSON_VALUE(
doc,
'$.customer.name'
RETURNING VARCHAR2(100)
)
);A function-based index stores the result of an expression for each row. Oracle can use the stored value to find matching rows quickly.
The expression in the query should match the expression in the index. These details can matter:
- The JSON path must be the same.
- The return type should be the same.
- The query should use the same error-handling options.
- The indexed expression should be deterministic.
For numeric comparisons, use a numeric return type:
CREATE INDEX orders_id_ix
ON orders (
JSON_VALUE(
doc,
'$.orderId'
RETURNING NUMBER
)
);
SELECT id, doc
FROM orders
WHERE JSON_VALUE(
doc,
'$.orderId'
RETURNING NUMBER
) BETWEEN 1000 AND 2000;A numeric index supports equality and range predicates. Indexing the value as text could produce incorrect ordering for numbers such as 9, 10, and 100.
Index paths with missing or invalid values
A JSON path can be missing in some documents. JSON_VALUE normally returns NULL when the path does not produce one scalar value. Oracle does not store an index entry when every indexed expression is NULL.
Malformed documents create a different problem. Constrain text-based JSON columns when you create them:
CREATE TABLE event_log (
event_id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
payload CLOB CONSTRAINT event_payload_json CHECK (payload IS JSON)
);The IS JSON constraint prevents invalid JSON from entering the table. A native JSON column performs JSON validation through its data type.
Search indexes for flexible JSON queries
A function-based index is a focused choice. It is efficient when the application repeatedly filters on one known path.
Use a JSON search index when queries need several paths, array elements, or changing search conditions:
CREATE SEARCH INDEX orders_doc_si
ON orders (doc)
FOR JSON;A search index can support ad hoc JSON searches, including predicates such as:
SELECT id
FROM orders o
WHERE JSON_EXISTS(
o.doc,
'$.items[*]?(@.sku == "A-1")'
);Search indexes are broader than a function-based index. They can require more storage and maintenance. Test representative queries with the execution plan:
EXPLAIN PLAN FOR
SELECT id
FROM orders
WHERE JSON_VALUE(
doc,
'$.customer.name'
RETURNING VARCHAR2(100)
) = 'Ada Lovelace';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);The plan shows whether Oracle chooses the JSON index or another access path. Table size, selectivity, statistics, and the cost of the competing plans affect that decision.
Compare the choices
| Index type | Best use | Main trade-off |
|---|---|---|
| Function-based index | A stable path with equality or range filters | Must match the expression |
| JSON search index | Flexible paths, arrays, and ad hoc JSON searches | More storage and write maintenance |
Exercise
Which index is usually the better starting point for repeated equality searches on $.customer.name?
Show answer
A function-based index on JSON_VALUE(doc, '$.customer.name' RETURNING VARCHAR2(...)), with the query using the same path and return type.