Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Single-column indexes
4. Indexes on VARCHAR columns – explanation
Multi-column indexes
Additional options when creating indexes
Summary

Instruction

Well done! So, we created an index on a VARCHAR column:

CREATE INDEX player_country_index
ON player(country);

An index on this type of column has limited usage. It can be used with an equality sign in the WHERE clause:

SELECT
  *
FROM player
WHERE country = 'Portugal';

It can also be used with a LIKE operator that contains wildcards (%). However, you should remember the following rule: the index will only help narrow down the search range until the appearance of the first wildcard. Take a look at the image:

LEAD

In the first case, we use LIKE 'P%'. The index can quickly find rows where the country value starts with the letter 'P', since 'P' appears before the wildcard. All such rows where the country name begins with a P are returned because all of them match the criterion.

In the second case, we use LIKE 'P%d'. The index can only find rows with countries starting with 'P'. Then, the database needs to manually check these rows and return the ones that end with a 'd'.

In the last case, we use LIKE '%es'. Because the wildcard appears as the first letter, the index on country is of no use. The database has to check every row individually.

Exercise

Click Next exercise to continue.

Code editor
tableconsole