Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Indexes on expressions
Partial indexes
9. When partial indexes aren't used by the database
Clustered indexes
Summary

Instruction

Good! In the previous exercise, we created a partial index in the following way:

CREATE INDEX lname_sql
ON dev_award (last_name)
WHERE category = 'sql';

This index will be used by the database in queries like the one below:

SELECT *
FROM dev_award
WHERE category = 'sql'
  AND last_name = 'Smith';

The index, however, will NOT be used in the following query:

SELECT *
FROM dev_award
WHERE category = 'java'
  AND last_name = 'Smith';

In the first query, we're looking for Smiths in the category 'sql'. The category is included in the partial index. In the second query, however, the category is 'java', which is outside the partial index.

Note that the partial index will NOT be used in the following query:

SELECT *
FROM dev_award
WHERE last_name = 'Smith';

The query above doesn't specify the category, which means people with the last name 'Smith' can be found both in the category 'sql' (indexed) and in other categories (not indexed). The database will have to browse the whole table, which means the index will not be used.

Exercise

Click Next exercise to continue.