3.10 Practical Tips
1. The query uses the index but is still slow. Looking up through an index is not always fast. If the rows matching the condition are a substantial fraction of the table, scanning all the table's pages sequentially from the start is faster than repeating the data page read through the index (Key Lookup) hundreds of thousands of times. In many cases the optimizer is right to skip the index and choose a full scan. The principle behind this decision is covered in detail in Chapter 9.
2. The index exists but is not used. Even if the name column has an index, it cannot be used when looking up "customers whose name, converted to all lowercase, is equal to a given value." The index is sorted only by the original name values and knows nothing about the order of the lowercased values, so the column value must be recomputed for every row. This problem occurs when you wrap a column in a function or compare it after a type conversion. In such cases, transform the comparison target rather than the column, or consider the expression index seen in the previous section.
Figure. When manipulating a column in a comparison prevents index use
flowchart TB
subgraph BAD ["Condition that manipulates the left-hand column"]
direction TB
B1["WHERE YEAR(order_date) = 2026"] --> B2["The index is sorted by order_date values<br/>It does not know where YEAR(order_date) values are"]
B2 --> B3["Compute YEAR(order_date) for every row"]
B3 --> B4["Full scan"]
end
subgraph GOOD ["Condition leaving the column as is and expressing the logic on the right"]
direction TB
G1["WHERE order_date on or after 2026-01-01<br/>AND before 2027-01-01"] --> G2["Jump straight to the start position<br/>in the order_date index"]
G2 --> G3["Read only that range along the leaves"]
end
The two paths are conditions that yield the same result (orders in 2026). The left is the case where the YEAR() function is applied to order_date on the left-hand side. The index holds order_date values sorted, but the computed result YEAR(order_date) is not stored, so the database cannot determine a starting position for the search. In the end it must compute the function on every row and compare, which becomes a full scan.
The right expresses the same meaning as a range condition (order_date >= '2026-01-01' AND order_date < '2027-01-01') without manipulating the column. The comparison target is order_date itself, the index's sort key, so the database descends the B+Tree to the leaf where 2026-01-01 begins and then reads sideways only up to just before 2027-01-01. A case where a character column is compared with a number and an implicit type conversion occurs ends up the same as the left path.
3. Do not manipulate the column; express the logic on the right-hand side. The index is sorted by the column's values, so leave the column unmodified on the left side of the condition and express a logically equivalent expression on the right. For example, YEAR(order_date) = 2026 requires computing the year of every row, so a full scan is needed. But if you rewrite it as order_date >= '2026-01-01' AND order_date < '2027-01-01', only that range is read from the index. When comparing a character column with a number, too, match the right-hand side to the column's type, as in phone = '01012345678' instead of phone = 01012345678. When the type is converted implicitly, the result is the same full scan as when the column is manipulated.
4. Unused indexes slow down writes. An index that was created because it was needed at first but is no longer used is still updated on every write. Periodically review and clean up unused indexes.
5. A random primary key makes writes gradually slower. With little data there is no problem, but as the table grows and buffer pool resources are exhausted, a random key must fetch a different page on every write, and performance drops sharply.
6. A LIKE search may or may not use the index depending on the position of %. The name index is sorted in the order obtained by comparing strings from the first character. name LIKE '홍%' means "names starting with 홍," so the database can descend to the leaf holding the first key starting with '홍' in the index and read only the range that follows. In contrast, name LIKE '%길동' means "names ending in 길동," so the first character is unknown, and 김길동, 박길동, and 홍길동 are scattered far apart in the index. Since there is no way to decide where to start, the whole index or the whole table must be scanned. The same applies when % is attached on both sides, as in '%길동%'.
-- Uses the index: reads only the range from '홍' (inclusive) to '홎' (exclusive)
SELECT * FROM customer WHERE name LIKE '홍%';
-- Cannot use the index: the start position cannot be determined
SELECT * FROM customer WHERE name LIKE '%길동';
If you often need to search by the trailing part or a middle substring, an ordinary B+Tree index will not solve it. For suffix searches, you can build an expression index on the reversed name to turn it into a prefix search, and for middle-substring searches you use a separate kind of index such as a full-text index or PostgreSQL's pg_trgm (trigram index). Note that in PostgreSQL, depending on the database's collation, even LIKE '홍%' may be unable to use an ordinary index; in that case, build an index with text_pattern_ops.