SQL Expression Index
Index the result of a repeated expression when queries filter on that same transformed value.
What & Why
A regular index on source does not directly index LOWER(source). An expression index stores the normalized expression so repeated case-insensitive lead-source filters can use that indexed form.
The query expression must match the indexed expression; an index on LOWER(source) does not support a different transformation such as UPPER(source).
See How It Works
BUSINESS QUESTION
Marketing repeatedly searches the existing google_ads lead source case-insensitively.
| id | campaign_id | created_at | qualified_at | converted_at | lead_score | source | country | |
|---|---|---|---|---|---|---|---|---|
| 301 | 1 | ana@example.com | 2024-01-21 09:10:00+00 | 2024-01-22 11:00:00+00 | 2024-02-02 10:00:00+00 | 86 | google_ads | US |
| 302 | 1 | ben@example.com | 2024-01-24 12:40:00+00 | NULL | NULL | 52 | google_ads | CA |
| 303 | 2 | chloe@example.com | 2024-02-16 08:30:00+00 | 2024-02-18 14:20:00+00 | NULL | 74 | GB | |
| 304 | 3 | dev@example.com | 2024-03-20 17:15:00+00 | 2024-03-21 09:00:00+00 | 2024-04-04 16:00:00+00 | 91 | US |
EXAMPLE QUERY
SELECT
id,
email,
source
FROM marketing.leads
WHERE LOWER(source) = 'google_ads'
ORDER BY id;RESULT — leads whose normalized source is google_ads
| id | source | |
|---|---|---|
| 301 | ana@example.com | google_ads |
| 302 | ben@example.com | google_ads |
LOWER(source) matches the two google_ads lead rows and preserves their ID order.
This lesson's practice is part of Pro.
Sign up free to try it on a real business scenario