Sub topic 7
LIKE and leading wildcards
This chapter assumes you can read a basic execution plan with DBMS_XPLAN and know how a B-tree index is searched. It also assumes you have seen why a function on an indexed column blocks the index. Here you will see which LIKE patterns can use an index, which cannot, and what to do when you need the second kind.
Why the first character matters
A B-tree index keeps its entries sorted from the first character of the key. To find names starting with Smi, Oracle descends the tree to the first entry at or after Smi. It then reads forward until entries no longer start with Smi. That is an index range scan, and the cost depends on the number of matches.
To find names ending in ith, the sorted order gives no starting point. Matching entries are scattered across the whole index. Oracle has to examine every entry or every row.
CREATE INDEX customers_last_name_ix
ON customers (last_name);
EXPLAIN PLAN FOR
SELECT customer_id, last_name
FROM customers
WHERE last_name LIKE 'Smi%';
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY);The predicate section of the plan typically looks like this. The exact text varies by version.
Predicate Information (identified by operation id):
---------------------------------------------------
2 - access("LAST_NAME" LIKE 'Smi%')
filter("LAST_NAME" LIKE 'Smi%')Oracle often lists a LIKE predicate under both access and filter. The access part limits the index range, and the filter part rechecks each entry. What matters is that the operation is INDEX RANGE SCAN and not INDEX FULL SCAN or TABLE ACCESS FULL.
Which patterns can use the index
Both % (any number of characters) and _ (exactly one character) are wildcards. Oracle uses only the literal characters before the first wildcard to bound the range.
| Pattern | Index range scan? | What limits the range |
|---|---|---|
| `'Smith'` | Yes | Treated like an equality comparison |
| `'Smi%'` | Yes | Prefix `Smi` |
| `'Sm_th'` | Yes | Prefix `Sm`; the rest is filtered |
| `'S%ith'` | Yes | Prefix `S` only; `ith` is filtered, so a short prefix may still read many entries |
| `'%ith'` | No | No prefix |
| `'%mit%'` | No | No prefix |
| `'_mith'` | No | The first character is a wildcard |
The S%ith row shows that the number of literal characters before the first wildcard decides how much of the index is read. Characters after the wildcard are checked against each entry in that range and do not narrow the range.
If you need a literal % or _ in the prefix, use ESCAPE. In the following query the prefix is AB_.
SELECT customer_id
FROM customers
WHERE last_name LIKE 'AB\_%' ESCAPE '\';A leading wildcard is not always a table scan
With a leading wildcard, Oracle has two choices. It can run a full table scan. If the index holds every column the query needs, it can instead use an INDEX FAST FULL SCAN, which reads the whole index with multiblock reads. The index is usually much smaller than the table, so this can be cheaper.
-- Only last_name is needed, and the pattern excludes NULLs,
-- so the index can serve as a smaller copy of the data.
SELECT last_name
FROM customers
WHERE last_name LIKE '%ith';This is still a scan of every entry, and its cost grows with table size. It is a fallback, not a fix.
Bind variables and patterns
With a bind variable, the optimizer does not know at parse time whether the value starts with a wildcard. Pass the prefix and add the wildcard in SQL, so the pattern shape is fixed in the statement text.
VARIABLE prefix VARCHAR2(30)
EXEC :prefix := 'Smi'
SELECT customer_id, last_name
FROM customers
WHERE last_name LIKE :prefix || '%';If an application passes the entire pattern as one bind value, a user who types a leading % forces a full scan. Because the plan for a bind-variable statement can be reused, the cached plan may not match the next value. Building the pattern from a prefix keeps the statement shape predictable.
Case-insensitive searches
UPPER(last_name) LIKE 'SMI%' applies a function to the column, so the plain index on last_name cannot serve it. Create a function-based index on the same expression. The query must then use that exact expression.
CREATE INDEX customers_upper_last_name_ix
ON customers (UPPER(last_name));
SELECT customer_id, last_name
FROM customers
WHERE UPPER(last_name) LIKE 'SMI%';The prefix rules above apply to the expression. UPPER(last_name) LIKE '%MIT%' still cannot use a range scan.
Pattern searches on non-character columns
Applying LIKE to a DATE or NUMBER column makes Oracle convert the column to a string first. This is the implicit conversion problem from the previous chapter, and it blocks a normal index. Compare dates with a half-open range instead of LIKE '2026-01%'.
When you really need a suffix or contains search
You have three practical options.
- Suffix search with a reversed index. Index the reversed string and reverse the search pattern. The suffix becomes a prefix.
```sql CREATE INDEX customersrevlastnameix ON customers (REVERSE(last_name));
-- Finds names ending in 'ith': the reversed pattern is 'hti%' SELECT customerid, lastname FROM customers WHERE REVERSE(last_name) LIKE 'hti%'; ```
REVERSE is not in the SQL reference documentation, although it works in practice. Check that your organization accepts that before relying on it.
- Oracle Text. A
CONTEXTindex (INDEXTYPE IS CTXSYS.CONTEXT) is queried withCONTAINSand supports word searches and wildcard terms. It is a separate feature with its own syntax and index maintenance behavior.
- Accept the scan. For a small table, or a report that runs rarely, a full scan is acceptable. An extra index costs space and slows every insert, update and delete on the column.
Common mistakes
- Expecting
LIKE '%text%'to be fast because the column is indexed. The index exists, but it cannot narrow the search. - Writing
LIKEwhere equality is meant, such asLIKE :namewhen the user supplies a full value. Use=unless wildcards are intended. - Wrapping the column in
UPPERorLOWERwithout a matching function-based index. - Judging a pattern by running it once. Check the plan and the predicate section, because a short prefix on a large table can still produce a scan.
Exercise
The table products has an index on product_code (VARCHAR2). Which of these queries can use an index range scan on that index, and why? (a) product_code LIKE 'AX-%' (b) product_code LIKE '%-99' (c) product_code LIKE 'AX-%-99' (d) UPPER(product_code) LIKE 'AX-%'. For the ones that cannot, name one change that would allow it.
Show answer
(a) Yes, because the literal prefix AX- bounds the range. (b) No, because the pattern starts with a wildcard. A reverse-expression index on REVERSE(product_code) with the predicate REVERSE(product_code) LIKE '99-%' would allow it. (c) Yes, but the range is bounded only by AX-. The -99 part is checked as a filter against each entry in that range. (d) No, because the function on the column prevents use of the plain index. A function-based index on UPPER(product_code) allows it.