This allows the index to remain small, even though the table may be rather large, and have extreme selectivity.
Suppose you have a transaction table where entries start out with STATUS = 'A' (active), and then may pass through other statuses ('P' for pending, 'W' for "being worked on") before reaching a final status, 'F', at which point it is no longer likely to be processed again.
In PostgreSQL, a useful partial index might be defined as:
CREATE INDEX partial_status ON txn_table (STATUS) WHERE STATUS IN ('A', 'P', 'W');
This index would not bother storing any of the millions of rows that have reached "final" status, 'F', and would allow queries looking for transactions that still "need work" to efficiently search via this index.
Similarly, a partial index can be used to index only those rows where a column is not null, which will be of benefit when the column usually is null.
CREATE INDEX partial_object_update ON object_table (updated_on) WHERE updated_on IS NOT NULL;
This index would allow the following query to read only the updated tuples:
SELECT * FROM object_table WHERE updated_on IS NOT NULL ORDER BY updated_on;
It is not necessary that the condition be the same as the index criterion; Stonebraker's paper below presents a number of examples with indexes similar to the following:
CREATE INDEX partial_salary ON employee(age) WHERE salary > 2100;
In SQL Server, this type of index is called a filtered index. Partial indexes have been supported in PostgreSQL since version 7.2, released in February 2002. SQLite supports partial indexes since version 3.8.0.
MySQL as of version 5.4 does not support partial indexes. In MySQL, the term "partial index" is sometimes used to refer to prefix indexes, where only a truncated prefix of each value is stored in the index. This is another technique for reducing index size.
- SQL Server 2008 Documentation: Filtered Index Design Guidelines. Microsoft TechNet.
- "PostgreSQL Documentation: Release Notes: Release 7.2". PostgreSQL. Retrieved 2009-10-09.
Enable partial indexes (Martijn van Oosterhout)
- "Partial Indexes". Retrieved 2014-02-04.
- Ronald Bradford (2009-07-22). "Understanding Different MySQL Index Implementations".