3.9 Index Write Cost
If indexes improve read performance, should we create as many indexes as possible? No, because an index must be modified every time a record changes.
Suppose we add one new customer. When indexes exist, it is not enough to create the customer record in a data page. The new record must be inserted into every index that has been created, such as the customer number index, the name index, and the region index. With four indexes, adding a customer creates a total of 5 records. Deleting and updating records work the same way. If a customer's name is changed, the name index must reflect it too.
An index also occupies storage and takes up space in the buffer pool. In exchange for making reads fast, an index makes writes slower and requires a lot of additional storage. It is good to use indexes on tables that are read more often. On tables with far more writes, however, creating an index calls for careful consideration.
| Item | PostgreSQL | MySQL (InnoDB) | Oracle | Azure SQL |
|---|---|---|---|---|
| Default index structure | B+Tree family | B+Tree | B+Tree family | B+Tree |
| Table storage | Heap | Clustered (by PK) | Heap (IOT optional) | Heap or clustered, selectable |
| What a secondary index points to | Row address | Primary key value | Row address | Clustering key or row address |
| Expression/conditional index | Expression index, partial index | Functional index since 8.0 | Function-based index | Filtered index |