Learn SQL/Advanced/Query Performance/SQL Expression Index

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.

idcampaign_idemailcreated_atqualified_atconverted_atlead_scoresourcecountry
3011ana@example.com2024-01-21 09:10:00+002024-01-22 11:00:00+002024-02-02 10:00:00+0086google_adsUS
3021ben@example.com2024-01-24 12:40:00+00NULLNULL52google_adsCA
3032chloe@example.com2024-02-16 08:30:00+002024-02-18 14:20:00+00NULL74emailGB
3043dev@example.com2024-03-20 17:15:00+002024-03-21 09:00:00+002024-04-04 16:00:00+0091linkedinUS
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
idemailsource
301ana@example.comgoogle_ads
302ben@example.comgoogle_ads

LOWER(source) matches the two google_ads lead rows and preserves their ID order.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario