3.8 An Index That Holds Only Qualifying Rows: Filtered Index
Suppose the orders table has 100 million rows, of which unprocessed pending (PENDING) orders are 0.1%, or 100,000 rows. The shipping system queries pending orders constantly. If you build an ordinary index on the status column, then to find 100,000 pending orders the index also carries 99.9 million completed (DONE) orders. Most of the index consists of entries that will never actually be looked up.
When you want to index only the rows you need, you use a filtered index. The name differs by database: SQL Server calls it a filtered index, and PostgreSQL calls it a partial index. If you define a predicate when creating the index, the database evaluates it for each row, puts only the rows for which it is true (Y) into the index, and stores no index information for rows for which it is false (N). In Azure SQL and SQL Server, you create one by appending a WHERE clause to CREATE INDEX.
-- Azure SQL / SQL Server
CREATE INDEX ix_orders_pending
ON orders (order_date)
WHERE status = 'PENDING';
Figure 3-9. Size comparison of a regular index and a filtered index
flowchart TB
T["orders table<br/>100 million rows"]
T -->|"All rows"| N["Regular index (status)<br/>100 million entries<br/>DONE 99.9 million + PENDING 100,000"]
T -->|"Only rows with status = 'PENDING'"| F["Filtered index<br/>(order_date) WHERE status = 'PENDING'<br/>100,000 entries"]
T -.->|"DONE rows are not added"| X["Excluded from the filtered index<br/>99.9 million rows"]
Q["Pending order lookup"] --> N
Q --> F
Compare the arrows from the orders table at the top to the two indexes. The regular index on the left creates one entry for every row in the table, so it has 100 million entries. Of these, 99.9 million are completed (DONE) orders and are not used by the pending order lookup. The filtered index on the right evaluates the predicate status = 'PENDING' defined at index creation for each row and holds only the rows for which it is true, so it has 100,000 entries. The dotted line means DONE rows, for which the predicate is false, do not enter the filtered index.
The "Pending order lookup" at the bottom can be answered by either index, but the filtered index has one-thousandth the entries, so its storage, tree height, and the number of pages to keep in the buffer pool are all smaller.
Because this index holds only the 100,000 pending orders, there is much to gain.
- It is small. Entries drop from 100 million to 100,000, which reduces storage, lowers the tree height, and makes it easy to keep the whole index in the buffer pool.
- Write cost is lower. Modifying or deleting a completed order does not touch this index. However, when an order's status changes from PENDING to DONE, its entry must be removed from the index, so the index is modified then as well.
- Statistics become more accurate. A filtered index gets its own statistics built only over the rows satisfying that predicate, so the optimizer estimates row counts more accurately (Chapter 9).
Frequently used patterns go beyond finding only pending work. They include indexing only live rows in a table that uses soft delete (WHERE is_deleted = 0), or preventing duplicates only among rows that have a value (CREATE UNIQUE INDEX ... WHERE email IS NOT NULL).
Cautions when using it
A filtered index is used only when the optimizer can be certain that the query's condition falls within the index's filter predicate. With a fixed value such as WHERE status = 'PENDING' there is no problem, but with a parameter such as WHERE status = @status, the execution plan may be reused for other values, so the filtered index may not be chosen. Also, in SQL Server the filter predicate can only use simple expressions such as comparison, IN, and IS NULL, and cannot use functions or computed columns.
Corresponding features by DBMS
- PostgreSQL: Partial index. The syntax is almost the same (
CREATE INDEX ... WHERE status = 'PENDING'), and the filter predicate can even use functions, so it is more expressive than SQL Server. - Oracle: Function-based index. It does not directly support a WHERE clause, but it exploits the property that Oracle's B-Tree index does not store rows whose keys are all NULL. Building it as
CREATE INDEX ix ON orders (CASE WHEN status = 'PENDING' THEN order_date END)puts only pending orders in the index. However, the query must be written with the same CASE expression for the index to be used. This is a way to get the same effect as a partial index by exploiting Oracle's behavior. - MySQL (InnoDB): Not supported. InnoDB's secondary indexes also store NULL, so the Oracle approach cannot achieve the same effect. It is common to separate pending orders into a different table or substitute a
(status, order_date)multi-column index. Note that the "prefix index" in the MySQL documentation is a feature that indexes only the beginning of a string, which differs from a filtered index that stores information only for a subset of rows.
| Item | Azure SQL / SQL Server | PostgreSQL | Oracle | MySQL (InnoDB) |
|---|---|---|---|---|
| Name | Filtered index | Partial index | Emulated with a function-based index | None |
| How to create | CREATE INDEX ... WHERE | CREATE INDEX ... WHERE | CASE WHEN ... END expression + NULL exclusion property | Replaced by table separation, etc. |
| Expressions usable in the filter | Simple comparison, IN, IS NULL | Most expressions, including functions | Expressed with a CASE expression | Not applicable |
| Writing the query | WHERE that includes the filter predicate | WHERE that includes the filter predicate | Same CASE expression as the index | Not applicable |
NOTE Constraints and support scope per database may differ by version.